超时的处理

来源:互联网 发布:淘宝上的衣服来自哪里 编辑:程序博客网 时间:2024/04/30 15:52

Description

Download as PDF

For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M .

For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of 256.

Not surprisingly, some numbers do not have any generators and some numbers have more than one generator. For example, the generators of 216 are 198 and 207.

You are to write a program to find the smallest generator of the given integer.


Input

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case takes one line containing an integer N , 1$ \le$N$ \le$100, 000 .


Output

Your program is to write to standard output. Print exactly one line for each test case. The line is to contain a generator of N for each test case. If N has multiple generators, print the smallest. If N does not have any generators, print 0.

The following shows sample input and output for three test cases.


Sample Input

3
216
121
2005



Sample Output

198
0
1979

题意:就是找一个最小的数使这个数加上它的各个数位上的数字和等于给定的数,不存在输出0
做法、;看见这个题好激动么,太水了,结果TLE了第一次是从这个数的一半开始查找的,后面从他的3/4开始查找,也是TLE了,后来仔细想想:应该是
很简单的么,这样来假设他是一个n位数,那他本身的各个数位上之和最大为9*n,那我直接从这个数减去9*n开始查找好了,这样果断AC.
代码:
#include<stdio.h>
int main()
{
int n,m,len;
int i,s,j,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
{
len=0;
scanf("%d",&m);
for(j=m;j>0;)
{
len++;
j=j/10;
}
for(j=m-9*len;j<m;j++)
{
s=j;
sum=j;
while(s!=0)//各个数位求和
{
sum+=s%10;
s=s/10;
if(sum>m)
{
break;
}
}
if(sum==m)
{
printf("%d\n",j);
break;
}
}
if(j==m)
{
printf("0\n");
}
}
return 0;
}
说实话,这种题看上去简单,一激动就会错了,自己还是菜鸟,对这种水题的感觉太差,不能有效的一次性把它给AC了,多练基本功,以后看见题就不会再继续没感觉了
0 0
原创粉丝点击