题目:给你一个数字n,用0~9,10个数字组成两个五位数,使得他们的商为n,按顺序输出所有结果。
分析:暴力。直接枚举第二个数字,范围(1000,100000),然后判断即可。
说明:直接搜索应该也可以(⊙_⊙)。
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int used[10];
int judge( int a, int b )
{
if ( b > 98765 ) return 0;
for ( int i = 0 ; i < 10 ; ++ i )
used[i] = 0;
if ( a < 10000 ) used[0] = 1;
while ( a ) {
used[a%10] = 1;
a /= 10;
}
while ( b ) {
used[b%10] = 1;
b /= 10;
}
int sum = 0;
for ( int i = 0 ; i < 10 ; ++ i )
sum += used[i];
return (sum == 10);
}
int main()
{
int n, T = 0;
while ( ~scanf("%d",&n) && n ) {
if ( T ++ ) printf("\n");
int count = 0;
for ( int i = 1234 ; i < 100000 ; ++ i ) {
if ( judge( i, i*n ) ) {
printf("%05d / %05d = %d\n",i*n,i,n);
count ++;
}
}
if ( !count )
printf("There are no solutions for %d.\n",n);
}
return 0;
}

本文介绍了一个使用枚举法解决特定数字组合问题的C++程序实例。该程序的目标是找出所有可能的五位数对,使得这两个五位数的商等于给定的整数n,并且这两个五位数恰好由0到9这十个数字组成。
335

被折叠的 条评论
为什么被折叠?



