UVa 1583 Digit Generator(枚举+打表)
来源:互联网 发布:js 选择日期 时间 编辑:程序博客网 时间:2024/05/11 03:23
原题地址
https://vjudge.net/problem/UVA-1583
定义数字x加上x的各位数字之和得到y,称x是y的生成元。一个数可能存在多个生成元,求给定数字n最小的生成元。
解题思路
这道题的题意很清楚,也很简单。
需要注意的是通过打表做预处理,事先计算好每个数字的最小生成元。
通过遍历1-100000内的所有数字,计算它作为生成元可能构成的大数并标记之。由于遍历是从小到大的,所以如果一个大数存在多个生成元,一定可以保证先存的是小的生成元,后面不要覆盖掉就可以啦。
AC代码
#include "stdio.h"#include "stdlib.h"#include "string.h"#define MAXN 100005int hasGen[MAXN];void preProcess(){ memset(hasGen, 0, sizeof(hasGen)); //0表示数字没有生成元 int tmp, sum; for (int i = 0; i<100000; ++i) //标记每个数字作为生成元能构成的数 { tmp = sum = i; while (tmp != 0) { sum += tmp%10; tmp /= 10; } if(!hasGen[sum]) hasGen[sum] = i; //有比i更小的生成元 } }int main(int argc, char const *argv[]){ preProcess(); int T, n; scanf("%d", &T); while (T--) { scanf("%d", &n); printf("%d\n", hasGen[n]); //不存在直接输出0 } return 0;}
0 0
- UVa 1583 Digit Generator(枚举+打表)
- UVA 1583 Digit Generator(最小生成元+打表)
- 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-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
- LintCode 平衡二叉树
- mac 下配置Latex 搭配sublime2 skim
- L1-029. 是不是太胖了 java
- 第四章 运算符和表达式
- 桶思想在Hangman Judge,UVa489的运用
- UVa 1583 Digit Generator(枚举+打表)
- 模型评估与选择----第一部分
- [BZOJ3601] 一个人的数论
- 学习笔记之《Android应用案例开发大全》(全部调试过代码)
- 一致性hash算法
- coreseek搜索系统建立指南
- Uva-1633 Dyslexic Gollum(状压DP)
- Python学习之列表和元组
- windows下GitHub的SSH key配置