1049. Counting Ones (30)
时间限制10 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12.
Input Specification:
Each input file contains one test case which gives the positive N (<=230).
Output Specification:
For each test case, print the number of 1's in one line.
Sample Input:12Sample Output:5给你一个数N,从0到N,数一下每个数有几个一,把每个一的个数加起来
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月11日 21:18 | 答案正确 | 30 | 1049 | C++ (g++ 4.7.2) | 1 | 184 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 180 | 12/12 |
1 | 答案正确 | 1 | 180 | 3/3 |
2 | 答案正确 | 1 | 180 | 3/3 |
3 | 答案正确 | 1 | 180 | 3/3 |
4 | 答案正确 | 1 | 184 | 4/4 |
5 | 答案正确 | 1 | 180 | 1/1 |
6 | 答案正确 | 1 | 180 | 4/4 |
#include<iostream> using namespace std; int main() { int N,sum,radix,index; cin >> N; sum = 0; radix = 1; do{ index = N/radix%10; if (index == 0)sum += N / (radix * 10)*radix; /*例如2032 当取到index=0时,此位为1时X1XX ,前面有01两种可能,后面有00~99radix=100种可能, PS:第一次做没有考虑到这种情况,借位有机会为1的情况*/ else if (index==1) sum += N / (radix * 10) *radix+N%radix + 1; /*例如24132,当取到1时,前面00~23种可能的时候,后面可以对应00~99radix=100种可能,而当取到24时,后面只有00~32=33种可能 PS:第一次做没有考虑到这种情况是只是最后一个的可能比较少*/ else if (index > 1)sum += (N / (radix * 10) + 1)*radix; /*例如4542,当取到5时,前面0~4五种可能的时候,后面对应00~99种可能*/ radix *= 10; } while (N / radix!=0); cout << sum<<endl; system("pause"); return 0; }