本文共 633 字,大约阅读时间需要 2 分钟。
#include <iostream> using namespace std; unsigned int tongji(int m); unsigned int numOfone(unsigned int n); void main() { unsigned int i(0); cout << "Please input the number for calculate:"; cin >> i; cout << tongji(i) << endl; } unsigned int tongji(int m) { if (m == 1) { return 1; } else { return tongji(m - 1) + numOfone(m); } } unsigned int numOfone(unsigned int n) { unsigned int count = 0; if (n / 10000 == 1) { count ++; } n %= 10000; if (n / 1000 == 1) { count ++; } n %= 1000; if (n / 100 == 1) { count ++; } n %= 100; if (n / 10 == 1) { count ++; } n %= 10; if (n / 1 == 1) { count ++; } return count; } // 5 => 1 ; 13 => 6转载地址:http://mxdqi.baihongyu.com/