标签:构造
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5308
题意:给定N个N,求出将这N个N构造成24的方法。
分析:要是想到用尽量少的N构造出24点((N+N)/N * (N+N)/N * (N+N)/N * (N*N+N)/N),然后把多的N构造成0就好办了((N-N)*N*.....)。
代码:
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; void _15(int n) { printf("1 + 2\n");//n+1 printf("4 + 5\n");//n+2 printf("7 + 8\n");//n+3 printf("10 + 11\n");//n+4 printf("%d + 12\n",n+4);//n+5 printf("%d / 3\n",n+1); // n+6 2 printf("%d / 6\n",n+2);//n+7 2 printf("%d / 9\n",n+3);//n+8 2 printf("%d / 13\n",n+5);//n+9 3 printf("14 - 15\n");//n+10 0 int c=n+10; for(int i=16;i<=n;i++) { printf("%d * %d\n",c,i); c++; } printf("%d * %d\n",n+6,n+7); //c+1 printf("%d * %d\n",n+8,c+1); //c+1 printf("%d * %d\n",c+2,n+9); printf("%d + %d\n",c+3,c); } void _14() { int s1[100]={1,15,16,17,18,7,9,11,20,21,22,25,26}; int s2[100]={2,3,4,5,6,8,10,12,13,14,19,23,24}; char op[100]="-****+++//+--"; for(int i=0;op[i];i++) printf("%d %c %d\n",s1[i],op[i],s2[i]); } void _13() { int s1[100]={1,4,7,10,17,14,15,16,18,19,21,23}; int s2[100]={2,5,8,11,12,3,6,9,13,20,22,24}; char op[100]="+++++////***"; for(int i=0;op[i];i++) printf("%d %c %d\n",s1[i],op[i],s2[i]); } void _12() { int s1[100]={1,3,5,7,9,11,13,15,17,19,21}; int s2[100]={2,4,6,8,10,12,14,16,18,20,22}; char op[100]="-----++++++"; for(int i=0;op[i];i++) printf("%d %c %d\n",s1[i],op[i],s2[i]); } void _11() { int s1[100]={1,12,13,14,15,7,17,9,18,19}; int s2[100]={2,3,4,5,6,8,16,10,11,20}; char op[100]="-****+++/+"; for(int i=0;op[i];i++) printf("%d %c %d\n",s1[i],op[i],s2[i]); } void _10() { int s1[100]={1,11,4,6,8,13,14,15,17}; int s2[100]={2,3,5,7,9,10,12,16,18}; char op[100]="-++++//++"; for(int i=0;op[i];i++) printf("%d %c %d\n",s1[i],op[i],s2[i]); } void _9() { int s1[100]={1,3,4,11,6,8,15,13}; int s2[100]={2,10,5,12,7,14,9,16}; char op[100]="++-+++/-"; for(int i=0;op[i];i++) printf("%d %c %d\n",s1[i],op[i],s2[i]); } void _8() { int s1[100]={1,9,10,11,12,13,14}; int s2[100]={2,3,4,5,6,7,8}; char op[100]="-***+++"; for(int i=0;op[i];i++) printf("%d %c %d\n",s1[i],op[i],s2[i]); } void _7() { int s1[100]={1,3,4,6,11,12}; int s2[100]={2,8,5,10,7,9}; char op[100]="++++/+"; for(int i=0;op[i];i++) printf("%d %c %d\n",s1[i],op[i],s2[i]); } void _6() { int s1[100]={1,3,7,5,9}; int s2[100]={2,4,8,6,10}; char op[100]="+++-+"; for(int i=0;op[i];i++) printf("%d %c %d\n",s1[i],op[i],s2[i]); } void _5() { printf("1 * 2\n"); printf("6 * 3\n"); printf("7 - 4\n"); printf("8 / 5\n"); } void _4() { printf("1 * 2\n"); printf("5 + 3\n"); printf("6 + 4\n"); } int main() { int n; while(scanf("%d",&n)!=EOF) { if(n>=15) _15(n); else if(n==14) _14(); else if(n==13) _13(); else if(n==12) _12(); else if(n==11) _11(); else if(n==10) _10(); else if(n==9) _9(); else if(n==8) _8(); else if(n==7) _7(); else if(n==6) _6(); else if(n==5) _5(); else if(n==4) _4(); else printf("-1\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
hdu5308 I Wanna Become A 24-Point Master(构造)
标签:构造
原文地址:http://blog.csdn.net/w20810/article/details/47047893