ZOJ.3336 Friend Number II【有点水】 2015/10/09
来源:互联网 发布:td_lte是什么网络 编辑:程序博客网 时间:2024/06/05 09:47
Given a positive integer x, let S(x) denotes the sum of all x's digits. Two integersx and y are friend numbers if S(x)=S(y). Here comes the problem: Given a positive integerx, of course it has a lot of friend numbers, find the smallest one which is greater thanx,please.
Input
There are multiple test cases. The first line of input is an integer T (0<T<230) indicating the number of test cases. ThenT test cases follow. Each case is an integer x (0<x<=101000).
Output
For each test case, output the result integer in a single line.
Sample Input
31219222
Sample Output
2128231
Note: No input data start with digit 0 and you should not output a number starts with 0.
Author: CAO, Peng
Source: ZOJ Monthly, May 2010
刚开始的时候想错了,以为直接加9就可以了,后来想想不对,仔细想想才明白,从后往前找,找到第一个不为0的减1,再继续往前找,找到第一个不为9的加1(如果找到最前面也没找到一个不为9的,就先输出个1),然后对此位置之后的串排序输出;
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int main(){ int t,len,i; char s[1010]; scanf("%d",&t); while(t--){ scanf("%s",s); //if( s[0] == '0' ) continue; len = strlen(s);//printf("%d\n",len); for( i = len-1 ; i >= 0 ; --i ){ if( s[i] != '0' ){ s[i]--; break; } } for( --i ; i >= 0 ; --i ){ if( s[i] != '9' ){ s[i]++; break; } } if( i < 0 ) printf("1"); sort(s+i+1,s+len); for( i = 0 ; i < len ; ++i ) printf("%c",s[i]); printf("\n"); } return 0;}
- ZOJ.3336 Friend Number II【有点水】 2015/10/09
- zoj 3336 Friend Number II(水~)
- ZOJ 3336 Friend Number II
- ZOJ 3336 Friend Number II
- zoj 3336 Friend Number II(数论)
- zoj 3327 Friend Number
- ZOJ 3327Friend Number
- Friend Number ZOJ 解题报告
- 【瞎搞】 ZOJ 3327 Friend Number
- zoj 3327 Friend Number 贪心
- zoj 3327 Friend Number 模拟题
- ZOJ 3327 Friend Number(数学啊 )
- ZOJ 3327Friend Number(模拟题)
- ZOJ Friend Number 3327 这个题绝对够味
- zoj 3327 Friend Number(有难度的模拟题)
- ZOJ-3327-Friend Number【7th浙江省赛】【模拟】【数学】
- zoj 3327 Friend Number 枚举 分类讨论 模拟
- ZOJ.2829 Beautiful Number【水】 2015/09/22
- 编程基础之二分查找 06:月度开销 (二分,使区间和最小)
- CYC - 强制转成横屏的方式
- 【状态压缩dp】bzoj1087 互不侵犯
- Dubbo框架应用之(四)--Dubbo基于Zookeeper实现分布式实例
- 0 java连接数据库
- ZOJ.3336 Friend Number II【有点水】 2015/10/09
- 杭电 acm 2015(偶数求和)
- 使用BeanUtils.populate方法转化Map为Javabean时get与set方法的命名规则
- SIGGRAPH 2013 Course: Physically Based Shading in Theory and Practice
- CoreData:数据持久化
- IEEE browse top 10
- Photoshop脚本代码一键生成所有IOS图标
- SharePoint 中修改域用户密码的WebPart文件配置
- 提高您CSS开发能力的技巧集