Sunday, 10 September 2017

UVa Problem 594 - One Little, Two Little, Three Little Endians Solution (C++)


 Problem:

Please find the problem here.


Solution:

This is the simplest problem I ever had, just implement the given formulas! However, it does take me some time to write.

Hint: swaping the character pointer for each bits reverses the entire byte. And displaying the value after reversal.

Code:

#include<bits/stdc++.h>
using namespace std;
#define swp(a,b) a=a^b, b=a^b, a=a^b
main(){
int n,reverse;
while(cin>>n){

reverse =n;
char* bits =(char*) &reverse;


swp(bits[0],bits[3]);
swp(bits[1],bits[2]);


cout<<n<<" converts to "<<reverse<<endl;
}


}

No comments:

Post a Comment

Spoj Problem ACMCEG2C - Pick the candies (C++)

  The problem link may be found here.       Explanation: Use Deque to keep track of elements of the variety of candies. If i is gre...