CodeForces - 468A
Posted spongeb0b
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 468A相关的知识,希望对你有一定的参考价值。
Little X used to play a card game called "24 Game", but recently he has found it too easy. So he invented a new game.
Initially you have a sequence of n integers: 1,?2,?...,?n. In a single step, you can pick two of them, let‘s denote them a and b, erase them from the sequence, and append to the sequence either a?+?b, or a?-?b, or a?×?b.
After n?-?1 steps there is only one number left. Can you make this number equal to 24?
Input
The first line contains a single integer n (1?≤?n?≤?105).
Output
If it‘s possible, print "YES" in the first line. Otherwise, print "NO" (without the quotes).
If there is a way to obtain 24 as the result number, in the following n?-?1 lines print the required operations an operation per line. Each operation should be in form: "a op b = c". Where a and b are the numbers you‘ve picked at this operation; op is either "+", or "-", or "*"; c is the result of corresponding operation. Note, that the absolute value of c mustn‘t be greater than 1018. The result of the last operation must be equal to 24. Separate operator sign and equality sign from numbers with spaces.
If there are multiple valid answers, you may print any of them.
Examples
1
NO
8
YES
8 * 7 = 56
6 * 5 = 30
3 - 4 = -1
1 - 2 = -1
30 - -1 = 31
56 - 31 = 25
25 + -1 = 24
题目大意:给你一个n,1-n当中每次选两个数进行运算,进行n-1次运算后要为24,并且用过的数不能再用,新算出的数可以接着用,打印任意一种情况。
思路:n小于4的一定不可以,n等于4和5需要特判,其他情况都可以转化为3-2=1,1-1=0开头,然后用0*i=0(i!=4&&i!=6),最后加个4*6=24就行了。
代码:
1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 using namespace std; 5 int main() 6 { 7 int n; 8 cin>>n; 9 if(n<4) 10 cout<<"NO"<<endl; 11 else if(n==4) 12 { 13 cout<<"YES"<<endl; 14 printf("1 * 2 = 2 "); 15 printf("2 * 3 = 6 "); 16 printf("4 * 6 = 24 "); 17 } 18 else if(n==5) 19 { 20 cout<<"YES"<<endl; 21 printf("3 * 5 = 15 "); 22 printf("2 * 4 = 8 "); 23 printf("15 + 8 = 23 "); 24 printf("23 + 1 = 24 "); 25 } 26 else 27 { 28 cout<<"YES"<<endl; 29 printf("3 - 2 = 1 "); 30 printf("1 - 1 = 0 "); 31 printf("0 * 5 = 0 "); 32 for(int i=7;i<=n;i++) 33 if(i!=4&&i!=6) 34 printf("0 * %d = 0 ",i); 35 printf("4 * 6 = 24 "); 36 printf("24 + 0 = 24 "); 37 } 38 return 0; 39 }
以上是关于CodeForces - 468A的主要内容,如果未能解决你的问题,请参考以下文章
[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段
Codeforces 86C Genetic engineering(AC自动机+DP)