Monday, October 8, 2007

Funny Encryption

I am seeing myself ACCEPTED more often nowadays...
____________________________________________________________________
Question: Funny Encryption Method
____________________________________________________________________

#include "iostream.h"

int countSetBits(int n) {
int count = 0;
while(n) {
n = n & (n - 1);
count++;
}
return count;
}

int main() {
int n, m, b1, b2;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> m;
b1 = countSetBits(m);
b2 = 0;
while(m) {
b2 += countSetBits(m % 10);
m = m / 10;
}
cout << b1 << " " << b2 << endl;
}
return 0;
}
____________________________________________________________________

No comments:

Contributors