uva1583
来源:互联网 发布:ping ip加端口怎么写 编辑:程序博客网 时间:2024/05/29 03:47
如果x加上x的各个数字之和得到y,就说x是y的生成元。给出n(1≤n≤100000),求最小
生成元。无解输出0。例如,n=216,121,2005时的解分别为198,0,1979。
Sample Input
3
216
121
2005
Sample Output
198
0
1979
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=829&page=show_problem&problem=4458
//先算出所有的生成元x的原数y,以s[y]=x的形式将其存起,在求y的解x的时候,直接输出是s[y]即可//所以为了求最小生成元也就是x,就要从尾到头的循环算y。#include<iostream>#include<cstdio>#include<string.h>using namespace std;int const N = 100000;int sn[N+5]={0};int main(){ memset(sn,0,sizeof(sn)); for(int i=N;i>=0;i--) { int n=i; int s=i; while(n>0) { s+=n%10; n/=10; } sn[s]=i; } //for(int i=0;i<N;i++) // cout<<sn[i]<<" "; // cout<<endl; int n,T; cin>>T; while(T--) { int i; cin>>n; cout<<sn[n]<<endl; } return 0;}//AC at 2017/12/7
以下算法虽然一样可以算出正确答案,但是超时,oj不通过。
//Time limit exceeded//先算出所有的原数y,将其以s[x]=y;的形式存起,然后循环对比:要求的数和s中哪个元素相等,输出此元素的下标即可#include<iostream>#include<cstdio>using namespace std;int const N = 100000;int main(){ int sn[N+5]={0}; for(int i=1;i<=N;i++) { int n=i; sn[i]=i; while(n>0) { sn[i]+=n%10; n/=10; } } //for(int i=0;i<N;i++) // cout<<sn[i]<<" "; // cout<<endl; int n,T; cin>>T; while(T--) { int i; cin>>n; for(i=1;i<=N;i++) { if(n==sn[i]) { cout<<i<<endl; break; } } if(i>N) cout<<0<<endl; } return 0;}
总结:
第一种还是刘汝佳给的思路,只循环了N次,而后者循环2*N次。思维不能太死板,就如同那句警言:
KEEP IT SIMPLE AND STUPID
阅读全文
0 0
- uva1583
- UVa1583
- UVA1583
- UVa1583
- uva1583
- UVa1583 - Digit Generator
- UVA1583 - Digit Generator
- 生成元-uva1583
- uva1583 - Digit Generator
- uva1583-digit generator
- UVA1583 UVALive3355 Digit Generator
- UVA1583 - Digit Generator
- UVa1583-Digit Generator
- Digit Generator—UVa1583
- UVa1583——Digit Generator
- UVa1583 Digit Generator另一种解法
- UVA1583 - Digit Generator(生成元,打表)
- Digit Generator, ACM/ICPC Seoul 2005, UVa1583
- b reset 和 ldr pc ,reset 的区别?
- spring-boot-mybatis-cache-thymeleaf学习
- Week03_day05 集合(下)
- Ubuntu系统环境变量详解
- mysql5.7官网直译数据结构优化--数据大小优化
- uva1583
- leetcode 454. 4Sum II 使用Map降低运算复杂的
- android开发实现下载网页图片到本地并显示
- recovery&linux系统升级数据更新分析总结
- 算法基本概念
- 图--分类及存储结构(数据结构)
- Linux基本命令的使用及目录权限问题
- Object 类中的 equals方法
- 阿里面试问题(转牛客网上分享的题目)