http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3780

题目大意:

给你一个正整数x,要求每个数字上的总和和x相同且比x大的最小整数。

如x=12 答案为21  x=10 答案为100

思路:

因为要比x大的最小,我们很自然的想到个位-1十位+1不就可以了。但是要注意如果是0的话,0-1变为9那么是不行的!而9+1的话变为0也是不行的。

最个位开始查找,找第一个不为0的数-1(不为0的下标为not_zero),在not_zero往高位找到第一个不为9的个数+1,(这样保证了比x大)然后在9+1的这里往后排个序保证最小。

为什么要排序?如x=520那么按照上面的就会变为610 而答案应该为601

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1024;
char s[MAXN];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
s[0]='0';
scanf("%s",s+1);
int len=strlen(s);
int not_zero=len-1;
while(s[not_zero]=='0')
not_zero--;
s[not_zero]--; int not_nine=not_zero-1;
while(s[not_nine]=='9')
not_nine--;
s[not_nine]++; sort(s+not_nine+1,s+len); if(s[0]=='0')
printf("%s\n",s+1);
else
printf("%s\n",s); }
return 0;
}

ZOJ 3336 Friend Number II的更多相关文章

  1. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

  2. 【LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  3. 【题解】【位操作】【Leetcode】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  4. LightOJ 1245 Harmonic Number (II)(找规律)

    http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS    ...

  5. [OJ] Single Number II

    LintCode 83. Single Number II (Medium) LeetCode 137. Single Number II (Medium) 以下算法的复杂度都是: 时间复杂度: O( ...

  6. [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III

    Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...

  7. Single Number,Single Number II

    Single Number Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium Given an array of ...

  8. Ugly Number,Ugly Number II,Super Ugly Number

    一.Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are po ...

  9. 1245 - Harmonic Number (II)(规律题)

    1245 - Harmonic Number (II)   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 3 ...

随机推荐

  1. Kinect 开发 —— 深度信息

    转自:http://www.cnblogs.com/yangecnu/archive/2012/04/04/KinectSDK_Depth_Image_Processing_Part1.html 深度 ...

  2. 阿里云 django的一次web维护记录

    首先, 丢给我一个阿里云的server的账号/password,之前没有玩过阿里云,想想应该也是ssh服务来远程登陆. 环境: centos+nginx+uwsgi+python2.7+django. ...

  3. ubuntu网络重启后或主机重启后,/etc/resolv.conf恢复原样的解决办法

    ubuntu网络重启后或主机重启后,/etc/resolv.conf恢复原样的解决办法 /etc/resolv.conf中设置dns之后每次重启该文件会被覆盖,针对这种情况找了一些个解决方法 防止/e ...

  4. jquery constructor

    function F(){ this.a = "aaa"; alert(111); } F.prototype = { constructor:F, } var f = new F ...

  5. CSRF Failed: Referer checking failed - no Referer

    postman模拟登录出了这个错误,其实看标题就知道大概是怎么回事,网上大概找了办法,也没说到位,所以干脆自己找源码了. 问题很明显就是出在 CSRF 上,理所当然去查看 CsrfViewMiddle ...

  6. x64系统下,InpOutx64有数字签名而WinIO3.0无数字签名

    参考文档 http://www.highrez.co.uk/Downloads/InpOut32/ //可以下载InpOutx64的驱动程序及DLL,还有驱动主板硬件IO的例程 https://www ...

  7. arcgis webapp builder 安装试用

    ArcGIS WebApp Builder 是针对开发者的,用于高速构建基于HTML5/Javascript 技术的美观的 Web应用的一个工具. 用过Flex版本号的AppBuilder应该非常清楚 ...

  8. RocketMQ(八):消息发送

    匠心零度 转载请注明原创出处,谢谢! RocketMQ网络部署图 NameServer:在系统中是做命名服务,更新和发现 broker服务. Broker-Master:broker 消息主机服务器. ...

  9. 关于React中,map出来的元素添加事件问题

    用es6 map 的写法 直接绑定一个onTouchStart 事件不会报错. 用es5的map写法  如果不加上this  会报这个错误 无法读取未定义的属性 解决的方法是 绑定this  就可以了

  10. nuxt.js配置BASE_URL(基本域名)和NODE_ENV(环境变量)

    一直以来,开发环境和生产环境的数据接口域名不一样总是困扰着我 每次打测试包或者线上包,我都得手动切换域名,我相信很多人的做法跟这差不多,类似下面这样: (你已经注意到,这个文件已经被我无情的删除了,因 ...