京东神奇数,个人觉得比较好理解的方式。没有案例,求检查
#include <iostream> #include <vector> #include <string> using namespace std; string IntToStr(int i) { char c[256]; _itoa_s(i, c, 256, 10); return c; } bool isMagicNumber(int Num) { int sum = 0; string digit = IntToStr(Num); for (string::const_iterator i = digit.begin(); i != digit.end(); i++) { sum += (*i); } if (sum % 2 != 0) //神奇数各个位数之和必为偶数 { return false; } else { int record = 0; //位数相继做和差运算。 for (string::const_iterator j = digit.begin(); j != digit.end(); j++) { if (record > 0) record -= (*j); else record += (*j); } if (record == 0) return true; else return false; } } int main() { int l, r; cin >> l >> r; int countNum = 0; for (int i = l; i <= r; i++) { if (isMagicNumber(i)) countNum++; } cout << countNum << endl; system("pause"); return 0; }
#华为#