现在的位置: 首页 > 综合 > 正文

UVa 1583打表

2017年11月16日 ⁄ 综合 ⁄ 共 557字 ⁄ 字号 评论关闭

背景: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; 
}
【上篇】
【下篇】

抱歉!评论已关闭.