德国人的数学题375=1
以下是德國人出的數學题
有點難,
但是可防老人痴呆,
您會幾題呢?
3 7 5 = 1
3 7 5 = 2
3 7 5 = 3
3 7 5 = 4
3 7 5 = 5
3 7 5 = 6
3 7 5 = 7
3 7 5 = 8
3 7 5 = 9
3 7 5 =10
做出一道,幼稚園畢業
做出三道,高中畢業
做出七道,可上大學
全部做出,清華北大
大家试试看,看看哪個人快!?不
难的,找出相互关联关系,很容易
做的,只是小学算术和初中代数基
础题。
总是有亲友会发莫名其妙的题目。
要是不去理他,好像咱们真的老年痴呆一样。
但是要是真去自己做,还是挺麻烦的
那么应该怎么解决呢?
一解
当然,答案有很多,这里提供一种
1=3&(7&5)
2=3^(5&7)
3=3|(5&7)
4=3|(5^7)
5=3^(5+7)
6=3&(5^7)
7=3|(5|7)
8=3&(5+7)
9=3^5+7
10=3|(5+7)
有人说不能用&,|,那就改一下也行,但是我懒得搞了。
源码如下,有点烦,但是思路清晰。
就是分为前两个先算和后两个先算讨论,分别算出前两个的所有可能,与后一个计算
再算出后两个所有的可能,与前一个计算。
可以把那些改成根号,加之类的,就能得到不同的结果。
需要注意,由于运算顺序,要求==之前加括号。
注意^是异或,|是与,&是和
#include<stdio.h>
int a[8] = { 0 };
int b[8] = { 0 };
int c[20][3];
int n = 0;
char s[8] = { '+','-','*','/','%','^','&','|' };
int main() {
a[0] = 3 + 7; a[1] = 3 - 7; a[2] = 3 * 7; a[3] = 3 / 7; a[4] = 3 % 7; a[5] = 3 ^ 7; a[6] = 3 & 7; a[7] = 3 | 7;
b[0] = 7 + 5; b[1] = 7 - 5; b[2] = 7 * 5; b[3] = 7 / 5; b[4] = 7 % 5; b[5] = 7 ^ 5; b[6] = 7 & 5; b[7] = 7 | 5;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 3; j++) {
c[i][j] = -1;
}
}
for (int j = 1; j < 11; j++) {
for (int i = 0; i < 8; i++) {
if (a[i] + 5 == j) { c[j][0] = 1; c[j][1] = i; c[j][2] = 0; }
if (a[i] - 5 == j) { c[j][0] = 1; c[j][1] = i; c[j][2] = 1; }
if (a[i] * 5 == j) { c[j][0] = 1; c[j][1] = i; c[j][2] = 2; }
if (a[i] / 5 == j) { c[j][0] = 1; c[j][1] = i; c[j][2] = 3; }
if (a[i] % 5 == j) { c[j][0] = 1; c[j][1] = i; c[j][2] = 4; }
if ((a[i] ^ 5) == j) { c[j][0] = 1; c[j][1] = i; c[j][2] = 5; }
if ((a[i] & 5) == j) { c[j][0] = 1; c[j][1] = i; c[j][2] = 6; }
if ((a[i] | 5) == j) { c[j][0] = 1; c[j][1] = i; c[j][2] = 7; }
}
}
for (int j = 1; j < 11; j++) {
for (int i = 0; i < 8; i++) {
if (3 + b[i] == j) { c[j][0] = 2; c[j][1] = i; c[j][2] = 0; }
if (3 - b[i] == j) { c[j][0] = 2; c[j][1] = i; c[j][2] = 1; }
if (3 * b[i] == j) { c[j][0] = 2; c[j][1] = i; c[j][2] = 2; }
if (3 / b[i] == j&&b[i]!=0) { c[j][0] = 2; c[j][1] = i; c[j][2] = 3; }
if ((3 % b[i]) == j) { c[j][0] = 2; c[j][1] = i; c[j][2] = 4; }
if ((3 ^ b[i]) == j) { c[j][0] = 2; c[j][1] = i; c[j][2] = 5; }
if ((3 & b[i]) == j) { c[j][0] = 2; c[j][1] = i; c[j][2] = 6; }
if ((3 | b[i]) == j) { c[j][0] = 2; c[j][1] = i; c[j][2] = 7; }
}
}
for (int i = 1; i < 11; i++) {
if (c[i][0] == -1) continue;
if (c[i][0] == 1) printf("%d=3%c7%c5\n", i, s[c[i][1]], s[c[i][2]]);
if (c[i][0] == 2) printf("%d=3%c(7%c5)\n", i, s[c[i][1]], s[c[i][2]]);
}
}
多解
后来,由于我们希望能有多组解吓吓他们,所以我改了一下程序
#include<stdio.h>
int a[8] = { 0 };
int b[8] = { 0 };
struct body{
int num;
int c[40][3];/*c[0]代表运算顺序,c[1],c[2]代表两个运算符*/
};
int n = 0;
char s[9] = { '+','-','*','/','%','^','&','|'};
int main() {
a[0] = 3 + 7; a[1] = 3 - 7; a[2] = 3 * 7; a[3] = 3 / 7; a[4] = 3 % 7; a[5] = 3 ^ 7; a[6] = 3 & 7; a[7] = 3 | 7;
b[0] = 7 + 5; b[1] = 7 - 5; b[2] = 7 * 5; b[3] = 7 / 5; b[4] = 7 % 5; b[5] = 7 ^ 5; b[6] = 7 & 5; b[7] = 7 | 5;
struct body* ans=(struct body*)malloc(sizeof(struct body)*11);
for (int i = 1; i < 11; i++) ans[i].num = 0;
for (int i = 0; i < 8; i++) {
int j;
j = a[i] + 5; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 1; ans[j].c[ans[j].num][1] = i; ans[j].c[ans[j].num][2] = 0; ans[j].num++; }
j = a[i] - 5; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 1; ans[j].c[ans[j].num][1] = i; ans[j].c[ans[j].num][2] = 1; ans[j].num++; }
j = a[i] * 5; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 1; ans[j].c[ans[j].num][1] = i; ans[j].c[ans[j].num][2] = 2; ans[j].num++; }
j = a[i] / 5; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 1; ans[j].c[ans[j].num][1] = i; ans[j].c[ans[j].num][2] = 3; ans[j].num++; }
j = a[i] % 5; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 1; ans[j].c[ans[j].num][1] = i; ans[j].c[ans[j].num][2] = 4; ans[j].num++; }
j = a[i] ^ 5; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 1; ans[j].c[ans[j].num][1] = i; ans[j].c[ans[j].num][2] = 5; ans[j].num++; }
j = a[i] & 5; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 1; ans[j].c[ans[j].num][1] = i; ans[j].c[ans[j].num][2] = 6; ans[j].num++; }
j = a[i] | 5; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 1; ans[j].c[ans[j].num][1] = i; ans[j].c[ans[j].num][2] = 7; ans[j].num++; }
}
for (int i = 0; i < 8; i++) {
int j;
j = 3 + b[i]; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 2; ans[j].c[ans[j].num][1] = 0; ans[j].c[ans[j].num][2] = i; ans[j].num++; }
j = 3 - b[i]; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 2; ans[j].c[ans[j].num][1] = 1; ans[j].c[ans[j].num][2] = i; ans[j].num++; }
j = 3 * b[i]; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 2; ans[j].c[ans[j].num][1] = 2; ans[j].c[ans[j].num][2] = i; ans[j].num++; }
j = 3 / b[i]; if (j >= 1 && j <= 10 && b[i] != 0) { ans[j].c[ans[j].num][0] = 2; ans[j].c[ans[j].num][1] = 3; ans[j].c[ans[j].num][2] = i; ans[j].num++; }
j = 3 % b[i]; if (j >= 1 && j <= 10 && b[i] != 0) { ans[j].c[ans[j].num][0] = 2; ans[j].c[ans[j].num][1] = 4; ans[j].c[ans[j].num][2] = i; ans[j].num++; }
j = 3 ^ b[i]; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 2; ans[j].c[ans[j].num][1] = 5; ans[j].c[ans[j].num][2] = i; ans[j].num++; }
j = 3 & b[i]; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 2; ans[j].c[ans[j].num][1] = 6; ans[j].c[ans[j].num][2] = i; ans[j].num++; }
j = 3 | b[i]; if (j >= 1 && j <= 10) { ans[j].c[ans[j].num][0] = 2; ans[j].c[ans[j].num][1] = 7; ans[j].c[ans[j].num][2] = i; ans[j].num++; }
}
for(int i=0;i<11;i++){
for(int j=0;j<ans[i].num;j++){
if (ans[i].c[j][0] == 1) printf("%d = (3%c7)%c5\n", i, s[ans[i].c[j][1]], s[ans[i].c[j][2]]);
if (ans[i].c[j][0] == 2) printf("%d = 3%c(7%c5)\n", i, s[ans[i].c[j][1]], s[ans[i].c[j][2]]);
}
}
}
答案是
1 = (3-7)+5
1 = (3* 7)%5
1 = (3%7)&5
1 = (3^7) ^5
1 = (3&7)&5
1 = (3|7)/5
1 = 3-(7-5)
1 = 3/(7-5)
1 = 3%(7-5)
1 = 3^(7-5)
1 = 3&(7/5)
1 = 3-(7%5)
1 = 3/(7%5)
1 = 3%(7%5)
1 = 3^(7%5)
1 = 3-(7^5)
1 = 3/(7^5)
1 = 3%(7^5)
1 = 3 ^ (7^5)
1 = 3&(7&5)
2 = (3+7)/5
2 = (3|7)-5
2 = (3|7)%5
2 = (3|7)^5
2 = 3&(7-5)
2 = 3-(7/5)
2 = 3^(7/5)
2 = 3&(7%5)
2 = 3&(7^5)
3 = (3%7)%5
3 = (3&7)%5
3 = 3%(7+5)
3 = 3|(7-5)
3 = 3%(7*5)
3 = 3&(7*5)
3 = 3*(7/5)
3 = 3/(7/5)
3 = 3|(7/5)
3 = 3|(7%5)
3 = 3|(7^5)
3 = 3%(7&5)
3 = 3%(7|5)
3 = 3&(7|5)
4 = (3-7)&5
4 = (3*7)/5
4 = (3^7)%5
4 = (3^7)&5
4 = 3+(7/5)
4 = 3^(7|5)
5 = (3+7)-5
5 = (3*7)&5
5 = (3/7)+5
5 = (3/7)^5
5 = (3/7)|5
5 = (3^7)|5
5 = (3|7)&5
5 = 3+(7-5)
5 = 3+(7%5)
5 = 3+(7^5)
6 = (3%7)^5
6 = (3&7)^5
6 = 3*(7-5)
6 = 3*(7%5)
6 = 3*(7^5)
6 = 3^(7&5)
7 = (3%7)|5
7 = (3&7)|5
7 = (3|7)|5
7 = 3|(7&5)
7 = 3|(7|5)
8 = (3%7)+5
8 = (3&7)+5
8 = 3+(7&5)
9 = (3^7)+5
10 = 3+(7|5)
总结
当然正确答案里有根号,但是这个我也懒得改了。
本来我以为这个程序难度不是很大,但是写的时候还是出了不少bug,花了挺长时间的。
一个大佬帮助我看过程序,并且熬夜写了一个用链表的,这里就不展示了。但是十分感谢他。
希望大家下次遇到这种亲友,可以顺利告诉他答案。