超时的处理
2015-07-20 12:05
183 查看
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了,多练基本功,以后看见题就不会再继续没感觉了
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了,多练基本功,以后看见题就不会再继续没感觉了
相关文章推荐
- hdu 4818 RP problem
- (翻译)Android中的定时任务
- Nginx配置文件详细说明
- 开关转换问题
- TCP/IP详解学习笔记(2)--IP协议
- 简单介绍Python的Django框架加载模版的方式
- Heartbeat+DRBD+MFS高可用
- Windows服务器的性能监控
- 第四天实习内容与收获
- android 在子线程中操作UI 导致Fragment显示空白问题
- 3.1 、3.2 数据成员的绑定与布局
- PHP 数据库基本操作
- 刘宇:我如何5分钟拿到李书福的投资? - 企业 - 国际在线
- JAVA的包
- swift 恢复 滑动返回 手势
- Mac中的快捷截屏方式
- HashMap的原理,HashMap怎样保证key的唯一性
- (翻译)又一个Android Sqlite库: Cupboard
- outlook 当关闭时最小化到任务栏完美的解决方案
- 对try-catch-finally异常处理的最新理解