P1836 【数页码_NOI导刊2011提高(04)】

我的想法是这样的:

分别考虑个位,十位,百位,千位,万位...

可以发现

其中res(i)表示第i位数相加之和。

故代码如下:

 1 #include <cstdio>
 2 using namespace std;
 3 long long n;
 4 int sum[10]={0,1,3,6,10,15,21,28,36,45};
 5 
 6 int get(int n)
 7 {
 8     if(n<0)return 0;
 9     return n;
10 }
11 
12 long long get_ans(long long n)
13 {
14     long long res=0,a=1,b=0;    
15     while(n>0)
16     {
17         res=res+a*(45*(n/10)+sum[get(n%10-1)])+(n%10)*(b+1);
18         b=b+(n%10)*a;a*=10;
19         n/=10;
20     }
21     return res; 
22 }
23 
24 int main()
25 {
26     scanf("%lld",&n);
27     printf("%lld\n",get_ans(n));
28     return 0;
29 }
View Code

我的代码里对打了表。

觉得不错的话点个赞欧

-------------------------------------------------------------------------------------

自创文章,欢迎转载

猜你喜欢

转载自www.cnblogs.com/lzxzy-blog/p/10323444.html