UVA1583-Digit Generator(紫书例题3.5)
For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M .
For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of 256.
Not surprisingly, some numbers do not have any generators and some numbers have more than one generator. For example, the generators of 216 are 198 and 207.
You are to write a program to find the smallest generator of the given integer.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case takes one line containing an integer N , 1N
100, 000 .
Output
Your program is to write to standard output. Print exactly one line for each test case. The line is to contain a generator of N for each test case. If N has multiple generators, print the smallest. If N does not have any generators, print 0.
The following shows sample input and output for three test cases.
Sample Input
3
216
121
2005
Sample Output
198
0
1979
思路:此题先打表,然后再查询即可,一次性枚举1到10000,其中a【i】的值是i的最小生成元
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 100005 //不妨将数组开大点
int a[maxn];
int main()
{
memset(a,0,sizeof(a));
for(int i=1;i<maxn;i++)
{
int m=i,n=i;
while(n)
{
m+=n%10;
n/=10;
}
if(a[m]==0||i<a[m])
a[m]=i;
}
int t,n;
cin>>t;
while(t--)
{
cin>>n;
cout<<a[n]<<endl;
}
return 0;
}
UVA1583-Digit Generator(紫书例题3.5)的更多相关文章
- 紫书 例题 11-13 UVa 10735(混合图的欧拉回路)(最大流)
这道题写了两个多小时-- 首先讲一下怎么建模 我们的目的是让所有点的出度等于入度 那么我们可以把点分为两部分, 一部分出度大于入度, 一部分入度大于出度 那么显然, 按照书里的思路,将边方向后,就相当 ...
- 紫书 例题8-3 UVa 1152(中途相遇法)
这道题要逆向思维, 就是求出答案的一部分, 然后反过去去寻找答案存不存在. 其实很多其他题都用了这道题目的方法, 自己以前都没有发现, 这道题专门考这个方法.这个方法可以没有一直往下求, 可以省去很多 ...
- 紫书 例题8-12 UVa 12627 (找规律 + 递归)
紫书上有很明显的笔误, 公式写错了.g(k, i)的那个公式应该加上c(k-1)而不是c(k).如果加上c(k-1)那就是这一次 所有的红气球的数目, 肯定大于最下面i行的红气球数 我用的是f的公式, ...
- 紫书 例题8-4 UVa 11134(问题分解 + 贪心)
这道题目可以把问题分解, 因为x坐标和y坐标的答案之间没有联系, 所以可以单独求两个坐标的答案 我一开始想的是按照左区间从小到大, 相同的时候从右区间从小到大排序, 然后WA 去uDebug找了数据 ...
- 紫书 例题8-17 UVa 1609 (构造法)(详细注释)
这道题用构造法, 就是自己依据题目想出一种可以得到解的方法, 没有什么规律可言, 只能根据题目本身来思考. 这道题的构造法比较复杂, 不知道刘汝佳是怎么想出来的, 我想的话肯定想不到. 具体思路紫书上 ...
- 紫书 例题 9-5 UVa 12563 ( 01背包变形)
总的来说就是价值为1,时间因物品而变,同时注意要刚好取到的01背包 (1)时间方面.按照题意,每首歌的时间最多为t + w - 1,这里要注意. 同时记得最后要加入时间为678的一首歌曲 (2)这里因 ...
- UVa1583 Digit Generator
#include <stdio.h> int main(){ int T, N, i, k, digitsum, generator; scanf("%d" ...
- UVA489 - Hangman Judge【紫书例题4.2】
题意:就是给出一个字符串,让你去一个一个猜测,相同字母算一次,如果是之前猜过的也算错,如果你在错7次前猜对就算你赢,文章中是LRJ的例题代码. #include<stdio.h> #inc ...
- 紫书 例题 10-2 UVa 12169 (暴力枚举)
就是暴力枚举a, b然后和题目给的数据比较就ok了. 刘汝佳这道题的讲解有点迷,书上讲有x1和a可以算出x2, 但是很明显x2 = (a * x1 +b) 没有b怎么算x2?然后我就思考了很久,最后去 ...
随机推荐
- 22 链表中倒数第k个节点(第3章 高质量的代码-代码的鲁棒性)
题目描述: 输入一个链表,输出该链表中倒数第k个结点. 尾节点是倒数第一个节点 测试用例: 功能测试(第k个节点在中间.是头节点.是尾节点) 特殊输入测试(链表头节点是nullptr指针.链表的头 ...
- UVA455 - Periodic Strings(紫书习题3.4)
如果一个字符串可以被某个长度为k的字符串重复多次得到,则称这个字符串的周期为k.例如,字符串"abcabcabcabc"以3为周期(当然,他也以6.12等等为周期). 现在请你编 ...
- 正确判断js数据类型 总结记录
正确判断js数据类型 总结记录 判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. 三方库. js六大数据类型 number: 数 ...
- python第四周:装饰器、迭代器、内置方法、数据序列化
1.装饰器 定义:本质是一个函数,(装饰其他函数)就是为其他函数添加附加功能 原则:不能修改被装饰函数的源代码,不能修改被装饰函数的调用方式 实现装饰器的知识储备: 函数即“变量”.每当定义一个函数时 ...
- 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) B】Shashlik Cooking
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 翻转一次最多影响2k+1个地方. 如果n<=k+1 那么放在1的位置就ok.因为能覆盖1..k+1 如果n<=2k+1 ...
- Java基础学习总结(64)——Java内存管理
本文介绍的Java虚拟机(JVM)的自动内存管理机制主要是参照<深入理解Java虚拟机>(第2版)一书中的内容,主要分为两个部分:Java内存区域和内存溢出异常.垃圾回收和内存分配策略.因 ...
- SpringMVC-HandlerMapping和HandlerAdapter
网上介绍HandlerMapping和HandlerAdapter的文章很多,今天我用自己的理解和语言来介绍下HandlerMapping和HandlerAdapter 一. HandlerMappi ...
- 警告: The APR based Apache Tomcat Native library failed to load.
警告: The APR based Apache Tomcat Native library failed to load. The error reported was [C:\apache-tom ...
- nodejs-mysql模块
安装mysql模块 1 npm install -g mysql node中使用Mysql模块来执行mysql命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 var ht ...
- CF899A Splitting in Teams
CF899A Splitting in Teams 题意翻译 n个数,只有1,2,把它们任意分组,和为3的组最多多少 题目描述 There were nn groups of students whi ...