码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 1583打表

时间:2015-01-20 23:58:03      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:acm   c++   uva   

背景:1--TLE:超时,没有考虑到时间复杂度,开始对每一个数都从1开始到99999,这样就是O(t*key)这样20组大数就可以超时。2--WA:3--WA都是把数字误以为最多4位了,其实是五位!!!。

思路:找出i(从1到100000)产生的数n,i是n的生成元,由于最多5位数字相加,所以n-i<50.对于每个要找生成元的数t,如果t大于50,只需搜索(t-50,t)。

学习:1.对于所有情况最多10万级别的可以打表。

#include<stdio.h>
int str[99999];
int main(void){
	  str[0]=0;
	  for(int i=1;i<99999;i++){
	  	int temp=i;
	  	str[i]=i;
	  	for(int j=0;j<5;j++){
	  		str[i]+=temp%10;
	  		temp/=10;
	  	}
	  }
	  int t;
	  scanf("%d",&t);
	  while(t--){
	  	int key,judege=0;
	  	scanf("%d",&key);
	  	for(int i=1;i<99999;i++){
	  		if(i==1){
	  			if(key>50) i=key-50;
	  		} 
	  		if(str[i]==key){
	  			judege=1;
	  			printf("%d\n",i);
	  			break;
	  		}
	  	}
	  	if(!judege) printf("0\n");
	  }
		return 0; 
}


UVa 1583打表

标签:acm   c++   uva   

原文地址:http://blog.csdn.net/jibancanyang/article/details/42934579

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!