题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3327
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 T test 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
Author: CAO, Peng
Source: The 7th Zhejiang Provincial Collegiate Programming Contest
思路:摘自:http://blog.csdn.net/cnh294141800/article/details/23203947
1.首先判断下有几个0,有一个0的话,
一 。判断0后面有没有比9小的数,有的话那个数加1就可以直接输出了。
二 。如果0是最后一位,前面有比9小的数,那个数+1,后面全输出0即可
三 。如果0是最后一位,前面没有比9小的那直接在最前面加1,后面全输出0即可。
四 。如果0后面没有比9小的,那直接把0位置变成1,后面都变成0输出即可。
2.如果是有多个0的话
一。如果在最右0之后有比9小的数,那直接那个数+1 输出答案即可。
二。如果没有的话,那就直接把这个位置的0变成1,后面全部输出0即可。
3。就是比较一般的情况
这种是比较简单的。统计因子2 3 5 7 出现的次数。
因为4可以由 2×2 组成,6可以由 2×3 组成 8 9都可以组成,所以只要保留素数因子即可。
然后直接从后往前面遍历,用直接已经有的因子数,看能不能组成比这个大的,如果可以的话,高位直接不动,这个位置变成这个数。
然后从最后一位开始,把当前拥有因子能组成的最大数组合起来,一个一个往前放,没有因子的话,那都放1就可以了,然后输出答案即可。
如果都没有可能的话,那在这个数前面加1即可。!!!!!!!!!!
!!!!!!!!
比如111126
一开始 a[2]=a[3]=a[5]=a[7]=0;
然后6遍历到6,a[2]++,a[3]++;
因为没有能组成比6 大.
然后遍历2这个时候a[2]=2,a[3]=1;
能组成3那这个位置就放3此时a[2]=2,a[3]=0;
最后一个变成4输出答案即可。
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
int n;
char s[10047];
LL c[10];//保存大数
int x[4]= {2,3,5,7};
int len;
void add(int n)
{
if(n == 1)
return ;
for(int i = 0; i <= 3; i++)
{
while(n%x[i] == 0)
{
c[x[i]]++;
n/=x[i];
}
}
}
void Delete(int n)
{
if(n == 1)
return ;
for(int i = 0; i <= 3; i++)
{
while(n%x[i] == 0)
{
c[x[i]]--;
n/=x[i];
}
}
}
int judge(int n)//判断是否能组成某个数
{
if(n == 1)
return 1;
for(int i = 0; i <= 3; i++)
{
int k = 0;
while(n%x[i] == 0)
{
k++;
n/=x[i];
}
if(k > c[x[i]])//用的每个数字的次数不能大于已有的2 3 5 7 的个数
{
return 0;
}
}
return 1;
}
void update(int n)
{
for(int i = len-1; i >= n; i--)
{
for(int j = 9; j >= 1; j--)
{
if(judge(j))
{
Delete(j);
s[i] = j+'0';
break;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(c, 0,sizeof(c));
scanf("%s",s);
len = strlen(s);
int cont = 0;
for(int i = 0; i < len; i++)
{
if(s[i] == '0')
{
cont++;
}
}
int flag = 0;
if(cont == 1 && s[len-1] == '0')//只有一个零且是最后一个数字
{
flag = 0;
for(int i = len-2; i >= 0; i--)
{
s[i]++;
if(s[i] > '9')
{
s[i] = '0';
}
else
{
flag = 1;
break;
}
}
if(!flag)
{
printf("1");//如果全是9 就在首尾多数出一个1
}
}
else if(cont >= 1)//只有一个零但是不是最后一个 或者 不止一个零
{
//含有零 最后的乘积都是零 所以只需要从后开始寻找一个数字加1即可
for(int i = len-1; i >= 0; i--)
{
s[i]++;
if(s[i] > '9')
{
s[i] = '0';
}
else
{
break;
}
}
}
else
{
add(s[len-1]-'0');//统计2 3 5 7 的个数,因为4 6 8 9 可以有前面的数字组成
flag = 0;
for(int i = len-2; i >= 0; i--)
{
int tt = s[i] -'0';
add(tt);
for(int j = tt+1; j < 10; j++)//能否由已有的2 3 5 7组成比t大的数字
{
if(judge(j))
{
flag = 1;
Delete(j);
s[i] = j+'0';//更新为当前可以组成的数字
break;
}
}
if(flag)
{
update(i+1);
break;
}
}
if(!flag)//如果都没有可能的话,那在这个数前面加1即可
{
printf("1");
update(0);
}
}
printf("%s\n",s);
}
return 0;
}
/*
99
10
12
19
222
9099
90099
*/