标签:
/** * 求从1到参数n中的各个数字中,1出现的次数和 * * @param n * @return */ public static int countOne(int n) { int factor = 1; int cur; int low; int high; int cnt = 0; while ((n / factor) != 0) { low = n - (n / factor) * factor;// 低位数字 cur = (n / factor) % 10;// 当前位数字 high = n / (factor * 10);// 高位数字 switch (cur) { case 0: cnt += high * factor;//由高位决定 break; case 1: cnt += high * factor + low + 1; break; default: cnt += (high + 1) * factor; } factor *= 10; } return cnt; }
标签:
原文地址:http://blog.csdn.net/u010786672/article/details/45286337