Vanya and Books

来源:互联网 发布:linux打开oracle服务 编辑:程序博客网 时间:2024/04/30 07:12
B. Vanya and Books
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vanya got an important task — he should enumerate books in the library and label each book with its number. Each of the n books should be assigned with a number from 1 to n. Naturally, distinct books should be assigned distinct numbers.

Vanya wants to know how many digits he will have to write down as he labels the books.

Input

The first line contains integer n (1 ≤ n ≤ 109) — the number of books in the library.

Output

Print the number of digits needed to number all the books.

Sample test(s)
input
13
output
17
input
4
output
4
Note

Note to the first test. The books get numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, which totals to 17 digits.

Note to the second sample. The books get numbers 1, 2, 3, 4, which totals to 4 digits.

思路:

     就是输入一个数,求1到这个数总共有多少位数,这题因为数据范围wa了4次,原本我只写了累加变量是long long类型,结果在运算中其它的是int类型,所以一直超int,之后就换了全部long long就AC了。经过这次对int类型有了更深的了解了。

AC代码:

#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>using namespace std;typedef __int64 ll;int weishu(int n){int cnt=0;while(n)cnt++,n/=10;return cnt;}int s[15]={{0},{9},{90},{900},{9000},{90000},{900000},           {9000000},{90000000},{900000000}};int main(){   /* freopen("input.txt","r",stdin);*/     ll n,i,k,t,f;ll j;while(~scanf("%lld",&n)){if(n<10){printf("%lld\n",n);continue;}f=k=weishu(n);for(j=0,i=1;i<k;++i)j+=s[i]*i;//应该就是这里如果不设long long,就要强制类型转换,不然就会超intt=0;while(--k){t=t*10+9;}if(n-t>0)j+=(n-t)*f;printf("%I64d\n",j);}    return 0;}

优秀代码:

#include<stdio.h>int num[9]={999999999,99999999,9999999,999999,99999,9999,999,99,9};int main(){    int n,i;    long long ans;    scanf("%d",&n);    ans=n;    for(i=0;i<9;i++)if(ans>num[i])ans+=n-num[i];    printf("%I64d\n",ans);    return 0;}


0 0
原创粉丝点击