Given a positive integer x, let P(x) denotes the product of all x's digits. Two integers x and y are friend numbers if P(x) = P(y). Here comes the problem: Given a positive integer x, of course it has a lot of friend numbers, find the smallest one which is greater than x.
Input
There are multiple test cases. The first line of input is an integer T (0 < T < 230) indicating the number of test cases. Then Ttest cases follow. Each case is an integer x (0 < x <= 101000). You may assume that x has no leading zero.
Output
For each test case, output the result integer in a single line. You should not output a number with leading zero.
Sample Input
3 12 19 222
Sample Output
21 33 241
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3752
第一次写解题报告。 首先要分0有几个的情况。 1.当0只有1个:先倒着遍历0后面的数字,如果有小于9的,那么直接+1输出,后面的数字全都改成0;如果后面全是9,那么把0改成1,后面的9都改成0即可;如果两者都不符合,即0是末位,那么倒着遍历0之前的数字,如个有小于9的,那么直接输出,后面的数字全都改成0;如果以上条件都不符合的话,就只能在最前面加个1然后所有位置的数字都改成0输出了。 2.当0有多个:同样先倒着遍历0后面的数字,如果有小于9的,那么直接+1输出,后面的数字全都改成0;否则就把这一位改成1,后面的数字全都改成0。 3.当没有一位是0:开个数组储存2、3、5、7的个数(4可以变成两个2,6可以变成1个2一个3,8可以变成三个2,9可以变成2个3),先存最后一位的,然后倒着从最后第二位遍历到第一位,设该位的数字为i,遍历从i+1到9的数字,如果可以用现有2357数组里的个数拼出这个数字,那么先减掉再break,锁定这一位。然后再倒着遍历从最后一位到当前这一位的后一位,尽可能的分完最大的数字(贪心),如果2357都被分完了,那么只要把这一位置1即可;如果没找到,那么直接在最前面补个1. 记住要细心细心再细心,这题很容易出错,用对拍调了一下午+晚上才AC( />< )/
#include<iostream> #include<algorithm> #include<string> #include<map> #include<vector> #include<cmath> #include<string.h> #include<stdlib.h> #include<cstdio> #define ll long long using namespace std; string x; int main(){ //freopen("E://3.out","r",stdin); //freopen("E://4.out","w",stdout); int t; cin>>t; while(t--){ cin>>x; int s=0,p,u=0,uu=0; for(int i=0;i<x.length();++i){ if(x[i]=='0'){ s++; p=i;} } if(s==1){ for(int i=x.length()-1;i>p;--i){ if(x[i]>'0'&&x[i]<'9'){ x[i]++; u=1; p=i; break; } else if(x[i]=='9') uu=1; } if(u==0&&uu==0){ for(int i=p-1;i>=0;--i){ if(x[i]>'0'&&x[i]<'9'){ x[i]++; u=2; p=i; break; } } } if(u==1){ for(int i=0;i<=p;++i) cout<<x[i]; for(int i=p+1;i<x.length();++i) cout<<0; cout<<endl; } else if(u==2){ for(int i=0;i<=p;++i) cout<<x[i]; for(int i=p+1;i<x.length();++i) cout<<0; cout<<endl; } else if(uu==1){ x[p]='1'; for(int i=0;i<=p;++i) cout<<x[i]; for(int i=p+1;i<x.length();++i) cout<<0; cout<<endl; } else{ cout<<1; for(int i=0;i<x.length();++i) cout<<0; cout<<endl; } } else if(s>1){ for(int i=x.length()-1;i>p;--i){ if(x[i]>'0'&&x[i]<'9'){ x[i]++; u=1; p=i; break; } } if(u==1){ for(int i=0;i<=p;++i) cout<<x[i]; for(int i=p+1;i<x.length();++i) cout<<0; cout<<endl; } else{ x[p]='1'; for(int i=0;i<=p;++i) cout<<x[i]; for(int i=p+1;i<x.length();++i) cout<<0; cout<<endl; } } else{ int w=0; int j=x.length()-1; int y[11]={0}; if(x[j]=='2') y[2]++; else if(x[j]=='3') y[3]++; else if(x[j]=='4') y[2]+=2; else if(x[j]=='5') y[5]++; else if(x[j]=='6'){ y[2]++;y[3]++; } else if(x[j]=='7') y[7]++; else if(x[j]=='8') y[2]+=3; else if(x[j]=='9') y[3]+=2; for(int i=x.length()-2;i>=0;--i){ j=i; if(x[j]=='2') y[2]++; else if(x[j]=='3') y[3]++; else if(x[j]=='4') y[2]+=2; else if(x[j]=='5') y[5]++; else if(x[j]=='6'){ y[2]++;y[3]++; } else if(x[j]=='7') y[7]++; else if(x[j]=='8') y[2]+=3; else if(x[j]=='9') y[3]+=2; int r=x[i]-'0'; for(int k=r+1;k<=9;++k){ if(k==2&&y[2]>0){ y[2]--; x[i]=k+'0'; w=1; } else if(k==3&&y[3]>0){ y[3]--; x[i]=k+'0'; w=1; } else if(k==4&&y[2]>1){ y[2]-=2; x[i]=k+'0'; w=1; } else if(k==5&&y[5]>0){ y[5]--; x[i]=k+'0'; w=1; } else if(k==6&&y[2]>0&&y[3]>0){ y[2]--; y[3]--; x[i]=k+'0'; w=1; } else if(k==7&&y[7]>0){ y[7]--; x[i]=k+'0'; w=1; } else if(k==8&&y[2]>2){ y[2]-=3; x[i]=k+'0'; w=1; } else if(k==9&&y[3]>1){ y[3]-=2; x[i]=k+'0'; w=1; } if(w==1) break; } if(w==1){ for(int l=x.length()-1;l>i;--l){ if(y[3]>=2){ x[l]='9'; y[3]-=2; } else if(y[2]>=3){ x[l]='8'; y[2]-=3; } else if(y[7]>=1){ x[l]='7'; y[7]-=1; } else if(y[2]>=1&&y[3]>=1){ x[l]='6'; y[2]-=1; y[3]-=1; } else if(y[5]>=1){ x[l]='5'; y[5]-=1; } else if(y[2]>=2){ x[l]='4'; y[2]-=2; } else if(y[3]>=1){ x[l]='3'; y[3]-=1; } else if(y[2]>=1){ x[l]='2'; y[2]-=1; } else x[l]='1'; } break; } else if(w==0&&i==0){ for(int l=x.length()-1;l>=0;--l){ if(y[3]>=2){ x[l]='9'; y[3]-=2; } else if(y[2]>=3){ x[l]='8'; y[2]-=3; } else if(y[7]>=1){ x[l]='7'; y[7]-=1; } else if(y[2]>=1&&y[3]>=1){ x[l]='6'; y[2]-=1; y[3]-=1; } else if(y[5]>=1){ x[l]='5'; y[5]-=1; } else if(y[2]>=2){ x[l]='4'; y[2]-=2; } else if(y[3]>=1){ x[l]='3'; y[3]-=1; } else if(y[2]>=1){ x[l]='2'; y[2]-=1; } else x[l]='1'; } } if(w==1) break; } if(w==0){ cout<<1; for(int i=0;i<x.length();++i) cout<<x[i]; cout<<endl; } else{ for(int i=0;i<x.length();++i) cout<<x[i]; cout<<endl; } } } return 0; }