Vanya and Books
来源:互联网 发布:linux打开oracle服务 编辑:程序博客网 时间:2024/04/30 07:12
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.
The first line contains integer n (1 ≤ n ≤ 109) — the number of books in the library.
Print the number of digits needed to number all the books.
13
17
4
4
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;}
- B. Vanya and Books
- B. Vanya and Books
- Vanya and Books
- CF 552B Vanya and Books
- #308 (div.2) B. Vanya and Books
- CodeForces-552B - Vanya and Books
- CodeForces 552B-Vanya and Books
- 【33.33%】【codeforces 552B】Vanya and Books
- codeforces 552B Vanya and Books
- B. Vanya and Books(Codeforces Round #308 (Div. 2))
- Codeforces Round #308 (Div. 2) Vanya and Books
- CodeForces - 552B Vanya and Books (数学阶乘)水
- Codeforces Round #308 (Div. 2) B. Vanya and Books
- B. Vanya and Books( Codeforces Round #308 (Div. 2) 简单题)
- E-books and Traditional Books
- A. Vanya and Cards
- A. Vanya and Cards
- A. Vanya and Cards
- maven学习笔记(windows)一
- 斐波拉契数列
- 多线程访问共享数据的安全问题
- WPF中播放帧序列图
- Java中的wait()方法和sleep()方法的区别是什么?
- Vanya and Books
- Java JUC之Atomic系列12大类实例讲解和原理分解
- win2003提示“在系统启动时至少有一个服务或驱动程序产生错误”
- C语言基础学习——第2天(语句)
- final,finally,finalize区别
- kindeditor编辑器 被过滤解决方法
- MYSQL的高并发
- 数据库事务隔离级别
- Proactor 学习2