• hdu5308 I Wanna Become A 24-Point Master(构造)


    题目: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+1
    	printf("4 + 5
    ");//n+2
    	printf("7 + 8
    ");//n+3
    	printf("10 + 11
    ");//n+4
    	printf("%d + 12
    ",n+4);//n+5
    	printf("%d / 3
    ",n+1); // n+6 2
    	printf("%d / 6
    ",n+2);//n+7   2
    	printf("%d / 9
    ",n+3);//n+8   2
    	printf("%d / 13
    ",n+5);//n+9  3
    	printf("14 - 15
    ");//n+10     0
    	int c=n+10;
    	for(int i=16;i<=n;i++)
    	{
    		printf("%d * %d
    ",c,i);
    		c++;
    	}
    	printf("%d * %d
    ",n+6,n+7); //c+1
    	printf("%d * %d
    ",n+8,c+1); //c+1
    	printf("%d * %d
    ",c+2,n+9);
    	printf("%d + %d
    ",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
    ",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
    ",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
    ",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
    ",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
    ",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
    ",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
    ",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
    ",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
    ",s1[i],op[i],s2[i]);
    }
    void _5()
    {
    	printf("1 * 2
    ");
    	printf("6 * 3
    ");
    	printf("7 - 4
    ");
    	printf("8 / 5
    ");
    }
    void _4()
    {
    	printf("1 * 2
    ");
    	printf("5 + 3
    ");
    	printf("6 + 4
    ");
    }
    
    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
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    Mysql第八天 分区与分表
    ios—项目开发需求文档
    spark资料下载
    网络爬虫:使用多线程爬取网页链接
    调用微信高级群发接口--视频群发接口出问题(微信官方文档错误纠正)
    享元模式
    在JAVA中怎样跳出当前的多重嵌套循环?
    获取个人借阅信息---图书馆client
    IOS 以随意点为圆心 旋转UIView
    ESP8266学习笔记4:ESP8266的SmartConfig
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7122481.html
Copyright © 2020-2023  润新知