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- 题意:24点游戏,给出了你一个数字n,你需要在1-n的数字序列中选择两个数进行加,减,或乘法运算,然后运算出的结果也加入序列中,再选择两个数,进行同样的操作,问你能不能使得最后只剩下一个数,且那个数是24。若可以,输出YES以及每一步的运算公式。否则输出NO。
思路:一开始被这题给吓到了,第一眼看起来很难,实际上却很简单。。。只要确保了一个24和一个0就行了。- 比如8,可以是4*6 = 24,1+2=3,3-3=0,这样就确保了 24 和 0 了,然后剩下的数都可以和 0 做乘法运算等到0(5*0=0,7*0=0,8*0=0),最后24+0=24就行了。
- 所以,若n大于等于6,那一定可以通过上面的方法得出24,因为最小需要4*6=24。然后特判一下1-5就行了。
- 1-3肯定是凑不出24的,然而4和5却可以,具体怎么凑的看代码:
- #include<iostream>
- #include<cstring>
- #include<cstdio>
- #include<string>
- #include<cmath>
- #include<algorithm>
- #include<stack>
- #include<queue>
- #define eps 1e-7
- #define ll long long
- #define inf 0x3f3f3f3f
- #define pi 3.141592653589793238462643383279
- using namespace std;
- int main()
- {
- int n;
- while(cin>>n)
- {
- if(n >= )
- {
- cout<<"YES"<<endl;
- cout<<"1 + 2 = 3"<<endl;
- cout<<"3 - 3 = 0"<<endl;
- for(int i = ; i<=n; ++i)
- {
- if(i== || i== || i== || i== || i==)
- continue;
- else
- cout<<i<<" * 0 = 0"<<endl;
- }
- cout<<"0 + 4 = 4"<<endl;
- cout<<"4 * 6 = 24"<<endl;
- }
- else if(n == )
- {
- cout<<"YES"<<endl;
- cout<<"1 * 2 = 2"<<endl;
- cout<<"2 * 3 = 6"<<endl;
- cout<<"6 * 4 = 24"<<endl;
- }
- else if(n == )
- {
- cout<<"YES"<<endl;
- cout<<"3 * 5 = 15"<<endl;
- cout<<"2 * 4 = 8"<<endl;
- cout<<"1 + 8 = 9"<<endl;
- cout<<"9 + 15 = 24"<<endl;
- }
- else cout<<"NO"<<endl;
- }
- return ;
- }
CodeForces - 468A ——(思维题)的更多相关文章
- Codeforces 424A (思维题)
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- Vova and Trophies CodeForces - 1082B(思维题)
Vova has won nn trophies in different competitions. Each trophy is either golden or silver. The trop ...
- CodeForces - 417B (思维题)
Crash Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
- CodeForces - 417A(思维题)
Elimination Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- B - Sonya and Exhibition CodeForces - 1004B (思维题)
B. Sonya and Exhibition time limit per test 1 second memory limit per test 256 megabytes input stand ...
- codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题
http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- CodeForces - 631C ——(思维题)
Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...
- CodeForces - 1102A(思维题)
https://vjudge.net/problem/2135388/origin Describe You are given an integer sequence 1,2,-,n. You ha ...
随机推荐
- vim之vundle
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle,下载到本地 gvim ~/.vimrc set nocompat ...
- TCP heart
http://blog.csdn.net/lisonglisonglisong/article/details/51327695
- 根据给定文件编写Makefile文件 两种方法编译
实例一 1.分析源文件代码依赖关系 mian.c #include "test1.h" #include "test2.h" #include <stdi ...
- Bootstrap-CL:页面标题
ylbtech-Bootstrap-CL:页面标题 1.返回顶部 1. Bootstrap 页面标题(Page Header) 页面标题(Page Header)是个不错的功能,它会在网页标题四周添加 ...
- [转]Docker 为什么这么火
本文来自:Docker为什么这么火 以及此文:http://cloud.51cto.com/art/201408/447966_1.htm Docker 我的理解是,相对于 VMware 的一个分支. ...
- Hive插入数据的几种常用方法
Hive的几种常见的数据导入方式这里介绍四种:(1).从本地文件系统中导入数据到Hive表:(2).从HDFS上导入数据到Hive表:(3).从别的表中查询出相应的数据并导入到Hive表中:(4).在 ...
- Keras函数式 API
用Keras定义网络模型有两种方式, Sequential 顺序模型 Keras 函数式 API模型 之前我们介绍了Sequential顺序模型,今天我们来接触一下 Keras 的函数式API模型. ...
- 一些linux知识和http知识
1 yum安装比源码编译安装 有的模块不能自定义安装 只能安装默认的模块进行安装 2 关于php的fastcgi 如果使用fastcgi 那么需要启动服务 如果不使用fastcgi 那么不需要启 ...
- YUM CentOS 7 64位下mysql5.7安装配置
配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/ #下载mysql源安装包 # wget http://dev ...
- IOS AudioServicesPlaySystemSound 后台锁屏播放
AudioServicesPlaySystemSound 想在锁屏后台播放报警提示音. 添加了UIBackgroundModes,audio,官方审核不通过! IOS的闹钟是怎么实现的,锁屏不能播放声 ...