给出N个数字N,要把全部数字用完。使用+-*/最后变为24。中间运算能够有有浮点数
对于1-14直接打表
15以上的能够通过13个同样数字得到24。然后使后面的数所有运算为0相加就可以
贴一发官方题解
#include "stdio.h" #include "string.h" int now; void pri(int l,int r) { int i; printf("%d - %d ",l,l+1); now++; for (i=l+2;i<=r;i++) { printf("%d * %d ",now,i); now++; } } void make(int l, int r) { printf("1 + 2 "); // now+1 printf("4 + 5 "); // now+2 printf("7 + 8 "); // now+3 printf("10 + 11 "); // now+4 printf("%d + 12 ",now+4); // now+5 printf("%d / 3 ",now+1); // now+6 printf("%d * %d ",now+6,now+2); // now+7 printf("%d / 6 ",now+7); // now+8 printf("%d * %d ",now+8,now+3); // now+9 printf("%d / 9 ",now+9); //now+10; printf("%d * %d ",now+10,now+5); // now+11; printf("%d / 13 ",now+11); // now+12; now+=12; } int main() { int n; while (scanf("%d",&n)!=EOF) { if (n<=3) printf("-1 "); if (n==4) { printf("1 * 2 "); printf("5 + 3 "); printf("6 + 4 "); } if (n==5) { printf("1 / 2 "); printf("6 / 3 "); printf("4 - 7 "); printf("5 * 8 "); } if (n==6) { printf("1 + 2 "); printf("7 + 3 "); printf("8 + 4 "); printf("9 + 5 "); printf("10 - 6 "); } if (n==7) { printf("1 / 2 "); printf("3 * 4 "); printf("9 - 8 "); printf("5 + 6 "); printf("10 / 11 "); printf("12 * 7 "); } if (n==8) { printf("1 - 2 "); printf("9 * 3 "); printf("10 * 4 "); printf("11 * 5 "); printf("12 + 6 "); printf("13 + 7 "); printf("14 + 8 "); } if (n==9) { printf("4 / 5 "); printf("6 / 7 "); printf("8 / 9 "); printf("1 + 2 "); printf("13 + 3 "); printf("14 - 10 "); printf("15 - 11 "); printf("16 - 12 "); } if (n==10) { printf("1 / 2 "); printf("3 / 4 "); printf("5 / 6 "); printf("7 / 8 "); printf("9 + 10 "); printf("11 + 12 "); printf("16 + 13 "); printf("17 + 14 "); printf("18 + 15 "); } if (n==11) { printf("1 + 2 "); printf("3 / 4 "); printf("5 / 6 "); printf("12 + 13 "); printf("15 + 14 "); printf("7 - 8 "); printf("17 * 9 "); printf("18 * 10 "); printf("19 * 11 "); printf("20 + 16 "); } if (n==12) { printf("1 + 2 "); printf("13 / 3 "); printf("14 * 4 "); now=15; pri(5,12); printf("15 + %d ",now); } if (n==14) { printf("1 / 2 "); printf("3 / 4 "); printf("5 - 15 "); printf("17 - 16 "); printf("6 + 7 "); printf("19 / 8 "); printf("20 * 18 "); now=21; pri(9,14); printf("21 + %d ",now); } if (n==13 || n>=15) { now=n; make(1,13); if (n==13) continue; else if (n==15) { printf("14 / 15 "); printf("%d * %d ",now,now+1); } else { int mark; mark=now; pri(14,n); printf("%d + %d ",mark,now); } } } return 0; }