UVa 1583 Digit Generator
2015-08-27 21:00
417 查看
Description
Download as PDF
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 , 1≤ \leN≤ \le100, 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
题意就是求求一个最小的数字x,是的x加上x的各个位置上的和是y。完全可以暴力做,但是有一点要注意,如果直接从0枚举到这个数的话肯定会超时的,但是有一点可以肯定的是,这些书做多不超过1e7,也就是说,各个位置上的数字和最多不会超过70,所以只需要枚举max(0,y-70)到y-1就行。
AC代码:
Download as PDF
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 , 1≤ \leN≤ \le100, 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
题意就是求求一个最小的数字x,是的x加上x的各个位置上的和是y。完全可以暴力做,但是有一点要注意,如果直接从0枚举到这个数的话肯定会超时的,但是有一点可以肯定的是,这些书做多不超过1e7,也就是说,各个位置上的数字和最多不会超过70,所以只需要枚举max(0,y-70)到y-1就行。
AC代码:
[code]// // Created by CQUWEL // Copyright (c) 2015年 CQUWEL. All rights reserved. // #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <string> #include <vector> #include <list> #include <map> #include <queue> #include <stack> #include <set> #include <algorithm> #include <numeric> #include <functional> #define cir(i,a,b) for (int i=a;i<=b;i++) #define CIR(j,a,b) for (int j=a;j>=b;j--) #define CLR(x) memset(x,0,sizeof(x)) #define root 1,n,1 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define filetest freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin) #define BUG() printf("In here\n") #define seevalue(x) printf("x=%d\n",x) #define INF 0x3f3f3f3f const double eps=1e-9; typedef long long ll; using namespace std; string s; ll digsum(ll a){ int sum=0; ll x=a; while (x){ sum+=x%10; x/=10; } return sum+a; } int main(){ int n; cin >> n; for (int i=1;i<=n;i++){ ll a; cin >> a; int flag=0; for (ll i=max((ll)0,a-70);i<a;i++){ if (digsum(i)==a) { printf("%lld\n",i); flag=1; break; } } if (!flag) printf("0\n"); } return 0; }
相关文章推荐
- Hbase coprocessor获取数据
- 设计一个聊天服务器
- Session与Cookie
- Word Search
- UNIX网络编程8 从图中了解TCP协议在Linux内核中的实现
- 埃及分数 把一个分数分解成n个 m分之一的方式
- 游标
- SAP BAPI资产过账问题随笔
- 之前UI的小练习
- web服务器apache理论、实践详解 ,TCP/IP
- HDOJ 4508 湫湫系列故事——减肥记I(完全背包)
- 静态库打包教程
- UI中的界面之间的值传递 <二>
- POJ 2828 Buy Tickets
- [leetcode]Contains Duplicate C语言
- 3 Longest Substring Without Repeating Characters
- chart控件基本使用——C#
- hdu 5037 Frog(贪心)
- 快速排序
- OpenGL照相机