题目很简单暴力枚举就可以,不过认真分析问题可以使程序更加简单,提高运算速率。不过通过这道题学到了sprintf的一个用法:将数字转化成字符串。
int main()
{
int i = 1234,j = 678;
char s[12];
sprintf(s,"%05d%05d",i,j);
printf("%s\n",s);
return 0;
}
下面是题目代码,如何判断是否重复的思路值得学习。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
using namespace std;
bool different(int i,int j)
{
char s[12];
sprintf(s,"%05d%05d",i,j);
int l = strlen(s);
if(l>10)
return false;
sort(s,s+l);
int len = unique(s,s+l) - s;
return l==len? true: false;
}
int main()
{
int n,kase = 0;
while(cin>>n&&n)
{
int i,j;
bool sign = false;
if(kase++)
cout<<endl;
for(j=1234; j<=98765; j++)
{
i = j * n;
if(different(i,j))
{
sign = true;
printf("%05d / %05d = %d\n",i,j,n);
}
}
if(!sign)
printf("There are no solutions for %d.\n",n);
}
return 0;
}