codeforces 808A

原题链接:

  http://codeforces.com/problemset/problem/808/A

题意:
  给一个数n,求n与比n大的最小的只有一个非零为数的差(看不懂就自己翻译一下原题吧0.0);

思路:

  找出所有只有一个非零为数,遍历求解;

代码:

 1 #include<cstdio>
 2 #include<string>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     long long a[11][10]={0};
12     int n,i,j,x,d=0,fg=1;
13     a[0][1]=0;
14     for(i=1;i<10;i++)
15     {
16         a[1][i]=i;
17     }
18     for(i=2;i<=10;i++)
19     {
20         for(j=1;j<10;j++)
21         {
22             a[i][j]=a[i-1][j]*10;
23         }
24     }
25     cin>>n;
26     x=n;
27     while(x)
28     {
29         d++;
30         x/=10;
31     }
32     for(i=1;i<10;i++)
33     {
34         if(a[d][i]>n)
35         {
36             cout<<a[d][i]-n<<endl;
37             fg=0;
38             break;
39         }
40     }
41     if(fg)                         //防止n为某位最大的数,而导致没有输出;如:99,因为两位数的只有一个非零位的数最大是90;
42         cout<<a[d+1][1]-n<<endl;
43     return 0;
44 }

---------------------欢迎评论------------------------------

原文地址:https://www.cnblogs.com/x-x-y/p/6915425.html