码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 468A 24 Game 规律题

时间:2014-12-21 14:01:33      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:点击打开链接

题意:

给定一个n表示有n个数字[1,n]

问:

用+ - * 3种运算把n个数变成24是否可行,若可行输出任意解。

思路:

因为2*3*4 = 24;

所以当n>=4时一定有解。

若n是偶数则4个连续数一组消成0,若还多余2个则变成 (n+1)-n = 1.

若n是奇数则把5消掉: 5-1 = 4; 4-2 = 2;。




import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Main {
	static int N = 200050;
	int n;
	public void work(){
		n = cin.nextInt();
		if(n < 4){
			out.println("NO");
	        out.close();
			return ;
		}
		out.println("YES");
		int m = n;
		if(n%2 == 1){
			out.println("5 - 1 = 4");
			out.println("4 - 2 = 2");
			n = 6;
		}
		else {out.println("1 * 2 = 2"); n = 5;}
		for(; n+3 <= m; n+=4){
			out.printf("%d - %d = 1\n", n+1, n);
			out.printf("%d - %d = -1\n", n+2, n+3);
			out.printf("-1 + 1 = 0\n");
			out.println("0 + 2 = 2");
		}
		if(n < m){
			out.printf("%d - %d = 1\n", n+1, n);
			out.println("1 * 2 = 2");
		}
		out.println("3 * 4 = 12");
		out.println("12 * 2 = 24");
        out.close();
	}
	Main() {
        cin = new Scanner(System.in);  
        out = new PrintWriter(System.out);
    }  
    public static void main(String[] args) {
        Main e = new Main();  
        e.work();
    }
    public Scanner cin;
    public PrintWriter out;
}


CodeForces 468A 24 Game 规律题

标签:

原文地址:http://blog.csdn.net/qq574857122/article/details/42059715

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!