Digit Generator UVA
来源:互联网 发布:我知你好 未再91baby 编辑:程序博客网 时间:2024/06/05 04:10
Digit Generator UVA - 1583
题目传送门:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4458
题目大意:生成元,一个数x的值为另一个数y的值加上y的各位数之和;
216 = 198 + 1 + 9 + 8;
2005 = 1979 + 1 + 9 + 7 + 9;
……..
分析:难道我们要枚举小于x的所有数字,挨个比较看哪个是它的生成元吗?
这样未免太过复杂,费时费力;
另一种方法就是打表,没错先让后台编译器去预处理一批数据,然后我们直接查表就好了;
怎么打表?
我们就先按照题意打一个大小为10086的表;
int ans[10086];for(int i = 0; i < 10086; i++)//打满{ int x = i, y = i; while(x > 0){y += x%10; x /= 10}//当前数字x拆开加和,注意是在y的基础上加,因为我们直接就要达到“ans[y] = y的生成元”的目的; if(ans[y] == 0 || i < ans[y])//赋值,并且根据题意要小的(我们i从小到大加上去,所以也没必要判断要小的这一项,不过加上去完整些) ans[y] = i;}
这个时候,ans[]就是符合我们要求的表单了;
还有很多数据量很大,但是又需要一次次查询比较的题目,也都可以先预处理出一个表格来解决,会节省很多时间,效率显著上升;
下面奉上AC Code:
#include<bits/stdc++.h>using namespace std;int ans[100086];void makelist()//打表{ for(int i = 0; i < 100086; i++) { int x = i,y = i; while(x > 0) { y += x%10; x /= 10; } if(ans[y] == 0 || ans[y] > i) ans[y] = i; }}int main(){ int m,n; memset(ans,0,sizeof(ans)); makelist(); cin>>n; while(n--) { cin>>m; cout<<ans[m]<<endl; } return 0;}
阅读全文
0 0
- UVA-1583 - Digit Generator
- UVa 1583 Digit Generator
- UVa 1583 - Digit Generator
- UVA 1583 - Digit Generator
- \UVa - 3355 - Digit Generator
- UVa 1583 Digit Generator
- UVa 1583 - Digit Generator
- UVa-1583Digit Generator
- 【Uva 1583】 Digit Generator
- UVA - 1583 Digit Generator
- Uva - 1583 - Digit Generator
- UVA - 1583 Digit Generator
- UVa 1583 - Digit Generator
- UVA 1583 Digit Generator
- UVa 1583 Digit Generator
- UVA - 1583 Digit Generator
- UVa 1583 - Digit Generator
- UVa 1583 - Digit Generator
- GitChat·前端 | 从软件工程角度看大前端技术栈
- 便 加权并查集
- hdu6129
- linux 下C++实现 ARP发布,和ARP监听
- HDU
- Digit Generator UVA
- 新手上路系列11:多线程编程
- 【并查集入门专题1】F
- 传递与转换Json值
- H
- P3818_小A和uim之大逃离 II
- Git的点点滴滴,结合了部分Android Studio自带的版本控制功能
- 分布式版本控制系统之gitlab-简单安装
- Ubuntu 系统远程设置