hdu 5308 I Wanna Become A 24-Point Master

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5308

解题思路:


尽可能的凑成24,多余的用(n-n)*n*n*·····*n=0消掉

AC代码:

#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    int i,n;
    while(~scanf("%d",&n)){
        if(n <= 3)
            printf("-1\n");
        else if(n == 4){
            printf("1 * 2\n");// 5 n*n
            printf("5 + 3\n");// 6 5*n
            printf("6 + 4\n");// 7 6*n
        }
        else if(n == 5){
            printf("1 / 2\n");// 6 1
            printf("6 / 3\n");// 7 1/5
            printf("4 - 7\n");// 8 24/5
            printf("5 * 8\n");// 9 24
        }
        else if(n == 6){
            printf("1 + 2\n");
            printf("7 + 3\n");
            printf("8 + 4\n");
            printf("9 + 5\n");
            printf("10 - 6\n");
        }
        else if(n == 7){
            printf("1 / 2\n");// 8 1
            printf("3 * 4\n");// 9 n*n
            printf("9 - 8\n");// 10 48
            printf("5 + 6\n");// 11 2*n
            printf("10 / 11\n");// 12 48/14
            printf("12 * 7\n");// 13 24
        }
        else if(n == 8){
            printf("1 - 2\n");// 9 0
            printf("9 * 3\n");// 10 0
            printf("10 * 4\n");// 11 0
            printf("11 * 5\n");// 12 0
            printf("12 + 6\n");// 13 n
            printf("13 + 7\n");// 14 2*n
            printf("14 + 8\n");// 15 3*n
        }
        else if(n == 9){
            printf("1 / 2\n");// 10 1
            printf("3 / 4\n");// 11 1
            printf("5 / 6\n");// 12 1
            printf("7 + 8\n");// 13 2*n
            printf("13 + 9\n");// 14 3*n
            printf("14 - 10\n");// 15 26
            printf("15 - 11\n");// 16 25
            printf("16 - 12\n");// 17 24
        }
        else if(n == 10){
            printf("1 / 2\n");// 11 1
            printf("3 / 4\n");// 12 1
            printf("5 / 6\n");// 13 1
            printf("7 / 8\n");// 14 1
            printf("9 + 10\n");// 15 2*n
            printf("11 + 12\n");// 16 2
            printf("16 + 13\n");// 17 3
            printf("17 + 14\n");// 18 4
            printf("18 + 15\n");// 19 24
        }
        else if(n == 11){
            printf("1 + 2\n");// 12 2*n
            printf("3 / 4\n");// 13 1
            printf("5 / 6\n");// 14 1
            printf("12 + 13\n");// 15 23
            printf("15 + 14\n");// 16 24
            printf("7 - 8\n");// 17 0
            for(int i = 9; i < 12; i++)
                printf("%d * %d\n",i,i+8);
            printf("16 + 20\n");
        }
        else if(n == 12){
            printf("1 + 2\n");// 13 24
            printf("3 - 4\n");// 14 0
            for(i = 5; i < 13; i++)
                printf("%d * %d\n",i,i+9);
            printf("13 + 22\n");
        }
        else if(n == 13){
            printf("1 + 2\n");// 14 26
            printf("14 / 3\n");// 15 2
            printf("4 / 5\n");// 16 1
            printf("6 - 16\n");// 17 12
            printf("15 * 17\n");// 18 24
            printf("7 - 8\n");// 19 0
            for(i = 9; i < 14; i++)
                printf("%d * %d\n",i,i+10);
            printf("18 + 24\n");
        }
        else if(n == 14){
            printf("1 / 2\n");// 15 1
            printf("3 / 4\n");// 16 1
            printf("5 - 15\n");// 17 13
            printf("17 - 16\n");// 18 12
            printf("6 + 7\n");// 19 28
            printf("19 / 8\n");// 20 2
            printf("20 * 18\n");// 21 24
            printf("9 - 10\n");// 22 0
            for(i = 11; i < 15; i++)
                printf("%d * %d\n",i,i+11);
            printf("21 + 26\n");
        }
        else{
            printf("1 + 2\n");// n+1 2*n
            printf("3 + 4\n");// n+2 2*n
            printf("5 + 6\n");// n+3 2*n
            printf("7 + 8\n");// n+4 2*n
            printf("%d + 9\n",n+1);// n+5 3*n
            printf("%d / 10\n",n+2);// n+6 2
            printf("%d / 11\n",n+3);// n+7 2
            printf("%d / 12\n",n+4);// n+8 2
            printf("%d / 13\n",n+5);// n+9 3
            printf("%d * %d\n",n+6,n+7);// n+10 4
            printf("%d * %d\n",n+8,n+9);// n+11 8
            printf("%d * %d\n",n+10,n+11);// n+12 24
            printf("14 - 15\n");
            int now = n+13;
            for(i = 16; i <= n; i++,now++)
                printf("%d * %d\n",i,now);
            printf("%d + %d\n",n+12,now);
        }
    }
    return 0;
}


你可能感兴趣的:(模拟)