首页 > 代码库 > CodeForces 468A 24 Game 规律题
CodeForces 468A 24 Game 规律题
给定一个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;。
[java] view plaincopy
- 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 规律题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。