雨水淋湿算术书的一道题。。 求算式中未知数

题目描述:

     雨水淋湿了算术书的一道题,8个数字只 能看清3个,第一个数字虽然看不清,但可看出不是1。编程求其余数字?

                 算式: [ □×(□3+□)]2 = 8□□9      (注2表示是平方)

 输入描述:

       无 (注意此题不能硬编码)

 输出描述:

        对应位置上的数字

  样例输入:

      无

   样例输出:

       1 9 0 6 4 

       3 2 8 6 4

算法思想:

      首先设未知的5个整数为a、b、c、d、e,则满足[a*(b3+c)]2=8de9式子就是所求数字。 由于等式右边不为0,所以a、b不可能为零取值范围1~9,c、d、e的取值范围为0~9;用嵌套循环分别找符合条件的数值。

完整代码:

#include<stdio.h>
#include<stdlib.h>
#include<string>
int main(){
	int a, b, c, d, e; //5个未知数
	int temp,left,right;
	char ch[4];
	gets_s(ch);
	if (!strcmp(ch,"无")){   //当输入“无”时,程序开始运行
		for (a = 1; a <= 9; a++){
			for (b = 1; b <= 9; b++){
				for (c = 0; c <= 9; c++){
					for (d = 0; d <= 9; d++){
						for (e = 0; e <= 9; e++){
							temp = a*(b * 10 + 3 + c);       //a*(b3+c)
							left = temp*temp;           //  [a*(b3+c)]2
							right = 8000 + d * 100 + e * 10 + 9;   //8de9
							if (left == right)
								printf_s("%d %d %d %d %d\n", a, b, c, d, e);
						}
					}
				}
			}
		}
	}
	system("pause");
    return 0;
}

结果表示:

猜你喜欢

转载自blog.csdn.net/weixin_42070473/article/details/103805029