1049. Counting Ones (30)

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:
12
Sample Output:
5
给你一个数N,从0到N,数一下每个数有几个一,把每个一的个数加起来

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
8月11日 21:18答案正确301049C++ (g++ 4.7.2)1184datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确118012/12
1答案正确11803/3
2答案正确11803/3
3答案正确11803/3
4答案正确11844/4
5答案正确11801/1
6答案正确11804/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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值