Description
一天,wm和zyf想比比谁比较正气,但正气这种东西无法量化难以比较,为此,他们想出了一个方法,两人各写一个数字,然后转化为二进制,谁的数字中二进制1多谁就比较正气!
#include <iostream> #include <string.h> using namespace std; int Ones(char *str); int main() { char wm[101]; char zyf[101]; int len1, len2; string result; while(cin >> wm >> zyf){ len1 = Ones(wm); len2 = Ones(zyf); result = len1 > len2? "wm": len1 == len2? "neither" : "zyf"; cout << result <<endl; } } int Ones(char *str){ int sum = 0, pos = 0; int length = strlen(str); for(int i = 0; i < length; i++) str[i] -= ‘0‘; while(pos != length){ for(int i = pos; i < length -1; i++) { str[i + 1] += str[i] % 2 *10; str[i] /= 2; } sum += str[length - 1] % 2; str[length - 1] /= 2; while(str[pos] == 0 && pos < length) pos++; } return sum; }
西电Online Judge 1007,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/rocky526/p/3820395.html