Anyone can solve this problem?

Pages: 12
In the definition of BIG, I am subtracting one from unsigned zero, producing the highest possible unsigned int value.
In the line labeled SHOOP DA WOOP! I am first comparing either u or o, depending on whether x is even or odd, to x. If x is smaller, then I am assigning x to e or o. The part (x%2?&e:&o) gets either the address of e or o, depending on if x is even or odd. Then I derefernce it and assign x to either e or o.
EDIT: Also this should not inspire you. It is an example of how to write code that wlll make your coworkers try to eat your soul.
EDIT2: You can further simplify this by using scanf:
1
2
3
4
5
6
7
8
#include "stdio.h"
#define BIG (0U-1)
int main(){
	unsigned o=BIG,e=BIG,x,i=0;
	while(i++<6&&scanf("%d",&x)>0)
		(x%2?e:o)>x&&(*(x%2?&e:&o)=x);//SHOOP DA WOOP!
	printf("min even: %u\nmin odd: %u\n",e,o);
}
Last edited on
It is an example of how to write code that wlll make your coworkers try to eat your soul.

true words! :D
@rocketboy:
I love you.
ah fine.

if (cantBeatEm(CplusPlus.com))joinem();

1
2
3
4
5
6
7
8
9
10
11
#include <iostream> //I am ashamed
#include <limits>
int main(){
std::cout<<"argh matey, enter ye 6 numbers evenly distributed amongst evens and odds eh :";
int num[8]= {std::numeric_limits<int>::max(),std::numeric_limits<int>::max()};
for (int a=0;a<6;a++){
std::cin >> (a+2)[num];
if (num[a+2]<num[a%2])(a%2)[num]=(a+2)[num];
}
std::cout<< "\nSmallest\tODD : " << num[false] << "Smallest\tEVEN : " << num[true];
}


edit: Guess It could Use soME forM4tting....
Last edited on
LOL.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <climits>
using namespace std;

#define C( D ) ((D<0)?(-D):D)
#define p( u ) const char* u
#define l( o ) (C(o)%2)
#define u( a , b) ((a<b)?a:b)
#define s( s ) int s
#define P      for
#define L      cout
#define U(dot) S(dot)=u(S(dot),dot)
#define S(com) SS[l(com)]

int main()
  {
  p(VV[2])={"even","odd"};s(SS[2])={INT_MAX,INT_MAX};
  P(s(n)=0;n<6;n++) {s(z);cin>>z;U(z);}
  P(s(n)=0;n<2;n++) {L<<"The smallest "<<VV[n]<<" number is "<<SS[n]<<"\n";}
  }

For a valid homework solution that your professor will fail you for turning in (because it obviously isn't your work):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <climits>
using namespace std;

inline int  abs( int n        ) { return (n < 0) ? -n : n; }
inline int  min( int a, int b ) { return (a < b) ?  a : b; }
inline bool odd( int n        ) { return abs( n ) % 2;     }

int main()
  {
  int smallest_odd  = INT_MAX;
  int smallest_even = INT_MAX;

  for (unsigned n = 0; n < 6; n++)
    {
    int z;
    cin >> z;
    if (odd( z )) smallest_odd  = min( smallest_odd,  z );
    else          smallest_even = min( smallest_even, z );
    }

  cout << "The smallest even number is " << smallest_even << endl;
  cout << "The smallest odd  number is " << smallest_odd  << endl;

  return 0;
  }

I notice that a lot of the solutions here don't strictly conform with the assignment:
  - avoid arrays or other (unnamed) things not yet taught
  - accept negative numbers as input
  - format output as given
iCPP, turn in rocketboys lmao... your prof envious of you will kick you out of class lmaoo

i learned a lot from this...
Last edited on
closed account (D80DSL3A)
Another solution into the mix here...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using  namespace std;

int main(void)
{
	int minEven=0, minOdd=0, x=0, *pi;// 0 is an illegal value for entry
	for(int i=1; i<=6; i++)
	{
		cout << "Enter #" << i << " : "; cin >> x;
		pi = x%2 ? &minOdd : &minEven;
		if(*pi==0)// no value stored yet
			*pi = x;// snag 1st value as min
		else if(x<*pi)// subsequent values handled
			*pi = x;
	}
	cout << "minOdd = " << minOdd << "  minEven = " << minEven << endl;
	return 0;	
}

iCPP, turn in rocketboys lmao... your prof envious of you

His prof would deduct points for failing to accept negative numbers as input.
My entry into this:
Not as good as some people's
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <limits>
#include <iostream>
#define MINT std::numeric_limits<int>::max()
struct { void operator() (int &a, int b) { a = ((a < b) ? a: b); } } min ;
struct { bool operator() (int a) { return (a % 2); } } odd ;
struct { void operator() (int a, int b) { std::cout << "odd  min: " << a << "\neven min: " << b; } } out ;
int main() {
  int a = MINT, b = MINT, x, i = 0;
  std::cout << "Enter Values\n";
  while(i++ < 6) {
    std::cout << " >"; std::cin >> x;
    odd(x) ? min(a, x) : min(b, x);
  } out(a, b); return 0; }
The modification to accept negatives is trivial:
1
2
3
4
5
6
7
8
#include "stdio.h"
#define BIG (~0U>>1)
int main(){
	signed o=BIG,e=BIG,x,i=0;
	while(i++<6&&scanf("%d",&x)>0)
		(x&1?o:e)>x&&(*(x&1?&o:&e)=x);//SHOOP DA WOOP!
	printf("min even: %d\nmin odd: %d\n",e,o);
}
Topic archived. No new replies allowed.
Pages: 12