英文原题,捆扰我好几天了,还是没头绪

yihan7h 2006-09-26 10:37:08
Write a program that finds and dispalys all pairs of 5-digit numbers that between them use the digits 0 through 9 once each,such that the first number divied by the second is equal to an integer N, where 2=<N<=79.That is: abcde/fghij=N

Where each letter represents a different digit.The first digit
of one of the numerals allowed to be zero.

Input:
Each line of the input file consists of a valid integer N. An input of zero is to terminate the program.

Output:
Your program have to dispaly All qualifying pairs of numerals, sorted by increasing numerals,sorted by increasing numerator(and of course, denominator).
Your output should be in the following general form:
xxxxx / xxxxx = N
xxxxx / xxxxx = N
In case there are no pairs of numerals satisfying the condition, you must write "There are no solutions for N."Separate the output for two different values of N by a blank line.

Sample Input:
61
62
0

Sample Output:
There are no solutions for 61.

79546 / 01283 = 62
94736 / 01528 = 62

只求个算法,谢谢!
...全文
237 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yofl88 2006-09-27
  • 打赏
  • 举报
回复
。。。。。。你的程序循环也太多了吧!
一般循环最多欠套3呈!
yihan7h 2006-09-26
  • 打赏
  • 举报
回复
哎,还是中文翻译下题意先,省的大家还得啃E文
要求:输入一个整数N(2=<N<=79)

给出两个5位数满足 abcde / fghij = N ,其中abcde和fghij分别代表0到9其中一个数字,且每个数字只能出现一次,0可以在第一位. 并要求输出所有可能的结果,没有的话输出no solution

如:给定N为62,则输出: 79546 / 01283 = 62 94736 / 01528 = 62

yihan7h 2006-09-26
  • 打赏
  • 举报
回复
恩,本人也写出了,主要就是循环嵌套和将字符串转化为数字的atoi()函数

#include <iostream>
#include <cstdlib>

using namespace std; //b 1234~5000

void Division(int result)
{
int i,j,k,l,m,n,o,front,back,mark,f=0;

char a[6],b[6];

for(i=0;i<5;i++)
{
for(j=0;j<10;j++)
{
if(j==i) continue;
for(k=0;k<10;k++)
{
if(k==j||k==i) continue;
for(l=0;l<10;l++)
{
if(l==i||l==j||l==k) continue;
for(m=0;m<10;m++)

{

mark=0;
if(m==i||m==j||m==k||m==l) continue;
a[0]=i+48;
a[1]=j+48;
a[2]=k+48;
a[3]=l+48;
a[4]=m+48;
a[5]=0;
back=atoi(a);
front=back*result;

if(front>98765)
continue;

b[0]=front/10000+48;
b[1]=front/1000-(b[0]-48)*10+48;
b[2]=front/100-(b[0]-48)*100-(b[1]-48)*10+48;
b[3]=front/10-(b[0]-48)*1000-(b[1]-48)*100-(b[2]-48)*10+48;
b[4]=front-(b[0]-48)*10000-(b[1]-48)*1000-(b[2]-48)*100-(b[3]-48)*10+48;
b[5]=0;


for(o=0;o<5;o++)

for(n=0;n<5;n++)

{
if(a[n]==b[o]||(b[n]==b[o]&&n!=o)) {
mark=1;
break;
}
}
if(mark==0) {
cout<<b<<'/'<<a<<'='<<result<<endl;
f=1;
}
}
}
}
}
}
if(f==0) cout<<"There are no solutions for "<<result<<'.'<<endl;
}

main()
{
int result;

cin>>result;
Division(result);
system("pause");
return 0;
}
yofl88 2006-09-26
  • 打赏
  • 举报
回复
思路基本理清了,晚上回家写写,明天给你回复!
SmallNumber=BigNumber/N
这题关键就是利用循环并判断BigNumber能否整除N,以及每次所得到的BigNumber,SmallNumber是否符合题意(及每个位上的数都不能重复)
blh 2006-09-26
  • 打赏
  • 举报
回复
一个错误
flag[i] = 1; ==> flag[a] = 1;
blh 2006-09-26
  • 打赏
  • 举报
回复
int find(int N)
{
int a, b, c, d, e, f, g, h, i, j;
unsigned int n1, n2;
char flag[10];

memset(a, 0, sizeof(flag));

for (a = 0; a < 9; a++)
{
flag[i] = 1;
for (b = 0; b < 9; b++)
{
if (flag[b]) continue;
flag[b] = 1;

for (c = 0; c < 9; c++)
{
if (flag[b]) continue;
flag[b] = 1;

..... ....

for (j = 0; j < 9; j++)
{
if (flag[j]) continue;
flag[j] = 1;
n1 = a * 10000 + b * 1000 + c * 100 + d * 10 + e;
n2 = f * 10000 + g * 1000 + h * 100 + i * 10 + j;
if (n1 % n2) continue;
if ((n1 / n2) == N)
printf("%d%d%d%d%d / %d%d%5d%d%d = %d\n", a, b, c, d, e, f, g, h, i, j, N);
flag[j] = 0;
}
flag[c] = 0;
}
flag[b] = 0;
}
flag[a] = 0;
}
小兽 2006-09-26
  • 打赏
  • 举报
回复
没考虑效率,凑活用吧。
执行方法:find2Number 62
-------------------

/*
要求:输入一个整数N(2=<N<=79)

给出两个5位数满足 abcde / fghij = N ,其中abcde和fghij分别代表0到9其中一个数字,
且每个数字只能出现一次,0可以在第一位. 并要求输出所有可能的结果,
没有的话输出no solution

如:给定N为62,则输出: 79546 / 01283 = 62 94736 / 01528 = 62
*/

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

int main(int argc, char* args[])
{

int ia = 98765;/*大的五位数*/
int ib = 1234;/*小的五位数*/

int N = atoi(args[1]);/*输入的整数*/

int A=ib * N;/*ia的最小可能值*/
int B=ia / N;/*ib的最大可能值*/
int iflag = 0;

printf("输入的N值为:%d\n", N);

for( ; ia >= A; ia--)
{
if(compareNum1(ia) == 1)
continue;
if((ia % N) != 0)
continue;

ib = ia / N;

if(compareNum2(ia, ib) == 1)
continue;
else
{
printf("第%d组结果: %d, %d\n", iflag + 1, ia, ib);
iflag ++;
}
}

if(iflag == 0)
printf("no solution!\n");
else
printf("找到了%d组结果.\n", iflag);

return 0;
}

int compareNum1(int i_a)
{
int size = sizeof(i_a) + 1;
int tmparray[size];
int j;
int ii;

for(ii = 0; ii < size; ii++)
{
tmparray[ii] = (i_a / pow10(ii)) % 10;

for( j = 0; j < ii; j++)
if( tmparray[ii] == tmparray[j])
return 1;
}

return 0;
}

int compareNum2(int i_a, int i_b)
{
int tmparray[10];
int i;
int j;

for(i = 0; i < 10; i++)
{
if(i < 5)
tmparray[i] = (i_a / pow10(i)) % 10;
else
tmparray[i] = (i_b / pow10(i - 5)) % 10;

for( j = 0; j < i; j++)
if( tmparray[i] == tmparray[j])
return 1;
}

return 0;
}

int pow10(int i)
{
int i_10 = 1;
int ii;

for(ii = 0; ii < i; ii ++)
i_10 *= 10;
return i_10;
}

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧