1000位的数字!!看来只能输入字符串了。。。
下面是某大大的代码,思路就是从2到9中找出能被N整除的数,然后打印出最小的组合
大数相那一段很值得学习!
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
#define MAXCHAR 1002
void Div(char *ch1, char *ch2, char *ch3) {
char temp[MAXCHAR];
int i;
memset(ch3,0,sizeof(ch3));
int num=ch2[0]-'0';
int len=strlen(ch1);
int s=0;
for(i=0;i<len;i++){ //大数相除
s=s*10+ch1[i]-'0';
ch3[i]=s/num+'0';
s%=num;
}
int flag=0;
int j=0;
for(i=0;i<len;i++){
if(flag==0&&ch3[i]=='0')
continue;
flag=1;
temp[j]=ch3[i];
j++;
}
temp[j]='\0';
strcpy(ch3,temp);
}
bool op(char *num,int t){
int len=strlen(num);
int i,j,k;
k=0;
for(i=0;i<len;i++){
k=k*10+num[i]-'0';
k=k%t;
}
if(k==0)
return true;
else
return false;
}
int main() {
char num[MAXCHAR], s[300];
char temp[MAXCHAR];
char aaa[MAXCHAR];
int i, j;
int ans[10][2];
while (1) {
scanf("%s", &num);
if(num[1]=='\0'){
printf("1%c\n",num[0]);
continue;
}
memset(ans, 0, sizeof (ans));
if (num[0] == '-')
break;
for (i = 9; i >= 2; i--) {
s[0]=i+'0';
s[1]='\0';
strcpy(temp, num);
while (1) {
if (op(temp,i) ) {
Div(temp,s,aaa);
strcpy(num,aaa);
ans[i][0]=1;
ans[i][1]++;
strcpy(temp,num);
}
else
break;
}
if(num[1]=='\0')
break;
}
if(num[1]!='\0'){
printf("There is no such number.\n");
continue;
}
ans[num[0]-'0'][0]=1;
ans[num[0]-'0'][1]++;
for (i = 2; i < 10; i++) {
if (ans[i][0] = 1) {
for (j = 0; j < ans[i][1]; j++)
printf("%d", i);
}
}
printf("\n");
}
return 0;
}