HDU 4608 I-number (数学&字符串处理)
2013-08-22 18:05
330 查看
I-number
http://acm.hdu.edu.cn/showproblem.php?pid=4608Time Limit: 10000/5000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Problem Description
The I-number of x is defined to be an integer y, which satisfied the the conditions below:
1. y>x;
2. the sum of each digit of y(under base 10) is the multiple of 10;
3. among all integers that satisfy the two conditions above, y shouble be the minimum.
Given x, you're required to calculate the I-number of x.
Input
An integer T(T≤100) will exist in the first line of input, indicating the number of test cases.
The following T lines describe all the queries, each with a positive integer x. The length of x will not exceed 105.
Output
Output the I-number of x for each query.
Sample Input
1 202
Sample Output
208
题意:求一个大于x的且最接近x的数y,y所有数位上的数字和sum是10的倍数。
思路:由于x的长度可达 10^5,采用字符串处理即可。
关键是判断能不能在不进位(只改变最后一个数)的时候找到y,此时把最后一位加上10 - sum%10就行。(需满足条件:最后一位<sum%10)
若最后一位进位,就先判断仅为之后所有位之和,比如57999进位后变58000,sum%10=3,那么最后一位加上10 - sum%10就行
特判:所有位数为9,那直接先输出一个1,然后把x最后一位变成9,剩下变成0就行。
完整代码:
/*171ms,332KB*/ #include<cstdio> #include<cstring> char x[100002]; int main() { int t; scanf("%d", &t); getchar(); while (t--) { memset(x, 0, sizeof(x)); gets(x); int len = strlen(x), sum = 0, i; for (int i = 0; i < len; ++i) sum += x[i] - '0'; sum %= 10; if (x[len - 1] - '0' < sum) x[len - 1] += 10 - sum; else { x[len - 1] = '9'; sum = 0; for (i = len - 1; i >= 0; --i) if (x[i] != '9') break; else x[i] = '0'; if (i == -1) { x[len - 1] = '9'; putchar('1'); printf("%s\n", x); continue; } ++x[i];///进位 for (int j = 0; j <= i; ++j) sum += x[j] - '0'; sum %= 10; if (sum) x[len - 1] += 10 - sum; } printf("%s\n", x); } return 0; }
相关文章推荐
- hdu 3973 AC's String 字符串哈希处理 查询子串是否是模式串 可以修改
- HDU 4119 Isabella's Message(字符串处理+矩阵旋转)
- 2017广东工业大学程序设计竞赛初赛 题解&源码(A,水 B,数学 C,二分 D,枚举 E,dp F,思维题 G,字符串处理 H,枚举)
- hdu 2206IP的计算(sscanf的使用 & 字符串判断处理)
- hdu 4119 Isabella's Message 【字符串处理】
- hdu 1063 poj 1001 字符串处理 数学相关
- HDU 2895 字符串处理(看懂就简单)
- HDU 2852 KiKi's K-Number(动态过程求第K小数)
- hdu 1020 简单的字符串处理
- 杭州电子科技大学(HDU)ACM 刷题---------字符串处理之1062
- hdu - 4608 - I-number
- HDU1894——字符串处理
- HDU 1088 Write a simple HTML Browser && 字符串模拟
- 【hpu oj 1286: Number of numbers && 南阳 oj 85 有趣的数 && HDU 1597】 [思维]
- Shell正则表达式 & Grep正则表达式 & shell字符串处理
- HDU 1164 Eddy's research I (数学)
- HDU 3652 B-number(数位dp&记忆化搜索)
- hdu 2852 KiKi's K-Number 二分+树状数组
- Shell正则表达式 & Grep正则表达式 & shell字符串处理(转)
- DOS下处理含特殊字符[如:&]的字符串