4168: Same Digits
Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByte
Total Submit: 115 Accepted:62
Total Submit: 115 Accepted:62
Description
Your program will be given an integer X. Find the smallest number larger than X consisting of the same digits as X.
Input
The first line of input contains the integer X (1 ≤ X ≤ 999 999).
The first digit in X will not be a zero.
Output
Output the result on a single line. If there is no such number, output 0.
Sample Input
156
Sample Output
165
这个前缀为0没有什么卵用
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { char s[10]; gets(s); int n=strlen(s),i,j; for(i=n-1; i>0; i--) if (s[i]>s[i-1])break; if(!i)puts("0"); else { int x=s[i-1],f=i; for (j=i+1; j<n; j++) if(s[j]>x&&s[j]<s[f])f=j; swap(s[f], s[i-1]); sort(s+i,s+n); puts(s); } return 0; }