ZOJ 3336 Friend Number II
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的更多相关文章
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 【题解】【位操作】【Leetcode】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
- [OJ] Single Number II
LintCode 83. Single Number II (Medium) LeetCode 137. Single Number II (Medium) 以下算法的复杂度都是: 时间复杂度: O( ...
- [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 ...
- Single Number,Single Number II
Single Number Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium Given an array of ...
- 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 ...
- 1245 - Harmonic Number (II)(规律题)
1245 - Harmonic Number (II) PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 3 ...
随机推荐
- nginx配置静态文件服务器的一个特殊需求的探索和分享, nginx处理不同路径返回统一文件,nginx改写,跳转请求.
最近在做一个前后端分离的个人博客,在做自己博客的时候有个想法,本来是打算用nginx作为静态文件服务器使用,django做后端程序. 我的前端页面用vue写的,结果用组件用嗨了,发现页面列表和 详情都 ...
- 用了Redis里面的map和set
map的操作用 hset,hget等 set的操作有 sadd sismember等 参考下面: http://blog.csdn.net/kwsy2008/article/details/48467 ...
- 码农的救赎:使用Github Pages搭建博客
人生若仅仅如初见,都恨太晚 据说有博客的人比没博客人的薪水要高非常多,相传写博客也是一个高手的标配,尽管之前一直有在写(在这里).可是孤既不是高手.薪水还比别人少.之前一直在CSDN上面写博客,那是一 ...
- 火狐—火狐浏览器中的“HttpWatch”
在IE下通过HttpWatch能够查看HTTP请求的相关细节.这对我们分析程序的运行效率很有帮助,但是在火狐浏览器中的难道就没有相似的工具了吗?答案是否定的--火狐浏览器中也有.在火狐浏览器中该工具叫 ...
- 关于结构体COORD介绍
COORD是windows API中定义的一种结构,表示一个字符在控制台屏幕上的坐标.其定义为: typedef struct _COORD { SHORT X; // horizontal coor ...
- cf1051F. The Shortest Statement(最短路/dfs树)
You are given a weighed undirected connected graph, consisting of nn vertices and mm edges. You shou ...
- HDU4630-No Pain No Game(离线,线段树)
Problem Description Life is a game,and you lose it,so you suicide. But you can not kill yourself bef ...
- Android提示版本号更新操作流程
Android提示版本号更新操作流程 2014年5月8日: andorid的app应用中都会有版本号更新的操作,今天空暇的时候就花了点心思弄了一下.主要技术方面用到了AsyncTask异步载入.htt ...
- xcode6.3 模版位置
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templ ...
- hello word-python 入门
今天正式开始学习python,先写一个最今经典的例子 helloword #!/usr/bin/python3.2 print("hello work!") 知识点: #!usr/ ...