Vanya and Books

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,就要强制类型转换,不然就会超int
		t=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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值