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 ...
随机推荐
- Codefroces 766D Mahmoud and a Dictionary
D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...
- 【例题 8-15 UVA - 12174】Shuffle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举第一段的范围[0..i] (0<=i<s) 然后看看[i+1..i+s-1],[i+s,i+s+s-1]..这些区间 ...
- 洛谷——P3384 【模板】树链剖分
https://www.luogu.org/problem/show?pid=3384#sub 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作 ...
- MVC—实现ajax+mvc异步获取数据
之前写过ajax和一般处理程序的结合实现前后台的数据交换的博客,如今做系统用到了MVC,同一时候也用到了异步获取数据. ajax+一般处理程序与MVC+ajax原理是一样的在"URL&quo ...
- badblocks 检查硬盘是否有坏道
硬盘是比較easy坏掉的设备,使用一段时间后可能会出现坏道等物理故障. 当硬盘出现坏道后,若不及时更换或者进行技术上的处理,磁盘的坏道就会越来越多,并会造成频繁死机和数据丢失. 最好的处理方法是更换新 ...
- apache 使用 mod_fcgid.so模块时 配置指令
FcgidBusyScanInterval指令 说明:扫描繁忙超时进程的间隔 语法: FcgidBusyScanInterval seconds 默认:FcgidBusyScanInterval 12 ...
- Problem C: Celebrity Split
题目描写叙述 Problem C: Celebrity Split Jack and Jill have decided to separate and divide their property e ...
- actionbar-去掉背景的阴影
今天发现一个问题,就是actionbar跟界面的交界处,会有一个阴影,通过调查发现,这个阴影是actionbar的.然后通过在网上找资料,完美解决了问题.解决方法如下 1.在这个actionbar所在 ...
- 记号(notation)的学习
数学的记号(notation) 记号具体代表什么含义,取决于你的定义: 比如这样的 d⃗ 一个向量,每个分量 d(i) 表示的是从初始结点 v 到当前节点 vi 的最短路径:也即这样的一个向量的每一 ...
- 1.Node.js
转自:http://www.runoob.com/nodejs/nodejs-tutorial.html 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基 ...