I Wanna Become A 24-Point Master

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 60    Accepted Submission(s): 16

Special Judge

Problem Description
Recently Rikka falls in love with an old but interesting game -- 24 points. She wants to become a master of this game, so she asks Yuta to give her some problems to practice.



Quickly, Rikka solved almost all of the problems but the remained one is really difficult:



In this problem, you need to write a program which can get 24 points with n numbers,
which are all equal to n.
 
Input
There are no more then 100 testcases and there are no more then 5 testcases with n≥100.
Each testcase contains only one integer n (1≤n≤105)
 
Output
For each testcase:



If there is not any way to get 24 points, print a single line with -1.



Otherwise, let A be
an array with 2n−1 numbers
and at firsrt Ai=n (1≤i≤n).
You need to print n−1 lines
and the ith
line contains one integer a,
one char b and
then one integer c, where 1≤a,c<n+i and b is
"+","-","*" or "/". This line means that you let Aa and Ac do
the operation b and
store the answer into An+i.



If your answer satisfies the following rule, we think your answer is right:



1. A2n−1=24



2. Each position of the array A is
used at most one tine.



3. The absolute value of the numerator and denominator of each element in array A is
no more than 109
 
Sample Input
4
 
Sample Output
1 * 2
5 + 3
6 + 4
 
Source

题意:

输入N。

用N个N通过加减乘除得到24.中间过程得到的数字能够用分数表示,不能截断小数。

分析:前几个数字打表吧。非常easy写的。可是我队员(GMY)也是写的非常辛苦啊。========(如今看到是改动过后的,所以打表的部分少了)

后面的数字这样处理:

选a个N相加  使得|a*N-24|最小,这样做的目的是为了降低用于构造24的N的使用量。

且令b = |24-a*N|  (a*N+b = 24 或者a*N-b = 24)构造这个等式就能够了。

那么用b+1个N就能构造 b了 。

方法:   (N *b)/N =  b

剩下left = N - a - b - 1个N。用left个N构造0就可以。

显然left >= 2 (7特判了,就不怕有bug了)

假设left 为奇数

构造  (N*(left/2) - N(left/2))/N = 0

否则  构造  N*(left/2) - N(left/2) = 0

所以left个N一定能够构造出0的

最后看b > 0 那么用a*N - b

否则用a*N + b

当然要特别考虑b = 0,的情况。

由于保证left > 0 了。

所以先构造出0.方便后面的运算。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int main(){
int n;
while(~scanf("%d",&n))
{
if( n == 1 || n == 2 || n == 3 ){
printf("-1\n");
}
else if( n == 4 )
printf("1 * 2\n5 + 3\n6 + 4\n");
else if( n == 5 )
printf("1 * 2\n6 * 3\n7 - 4\n8 / 5\n");
else if( n == 7 )
printf("1 + 2\n8 + 3\n4 + 5\n10 + 6\n11 / 7\n9 + 12\n"); //为了保证left > 1 n = 7 特判
else {
int i,j,k,q,p,left,need;
for(i = 0;;i++){
if(abs(i*n - 24) <= abs(i*n+n-24) )
break;
}
int a = i; //算出几个N相加会最接近于24
int fx = 24 - a*n; //算出 24 - a * N 关于0的大小
if( abs(24-a*n) > 0 )need = abs(24-a*n) + 1;
else need = 0; //算出须要need个N来构造 出|24 - a*N|
left = n - need - i; //用left个N构造出0 //用left个N构造0
printf("1 - 2\n");
p = n + 1;
i = 3;
j = 0;
for(;i <= (left - left% 2); ){
if(j & 1 ){
printf("%d - %d\n",p++,i++);
}
else printf("%d + %d\n",p++,i++);
j ^= 1;
}
//left 为奇数。多计算一次 0 / N
if(left % 2 == 1){
printf("%d / %d\n",p++,i++);
} //构造a个N相加的和
q = i;
for(i = 0;i < a; i++){
printf("%d + %d\n",p++,q++);
} int xp;
if(need > 0){
//need = 2 直接做除法,否则先做加法,最后做除法
if(need != 2){
printf("%d + %d\n",q,q+1);
q += 2;
xp = p + 1;
for(; q < n; ){
printf("%d + %d\n",xp++,q++);
}
printf("%d / %d\n",xp++,q++);
}
else {
printf("%d / %d\n",q,q+i);
xp = p + 1; }
//推断是加法还是减法
if(fx > 0)
printf("%d + %d\n",p,xp);
else
printf("%d - %d\n",p,xp);
}
}
}
return 0;
}

2015多校联合训练赛 hdu 5308 I Wanna Become A 24-Point Master 2015 Multi-University Training Contest 2 构造题的更多相关文章

  1. 2015多校联合训练赛hdu 5301 Buildings 2015 Multi-University Training Contest 2 简单题

    Buildings Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  2. hdu 5381 The sum of gcd 2015多校联合训练赛#8莫队算法

    The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  3. hdu 5358 First One 2015多校联合训练赛#6 枚举

    First One Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  4. hdu 5361 2015多校联合训练赛#6 最短路

    In Touch Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total ...

  5. HDU 5358(2015多校联合训练赛第六场1006) First One (区间合并+常数优化)

    pid=5358">HDU 5358 题意: 求∑​i=1​n​​∑​j=i​n​​(⌊log​2​​S(i,j)⌋+1)∗(i+j). 思路: S(i,j) < 10^10 & ...

  6. HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&#39;s problem(manacher+二分/枚举)

    pid=5371">HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分同样,第一部分与第二部分对称. 如今给你一个长为n(n<10^5)的序 ...

  7. 2015多校联合训练赛 Training Contest 4 1008

    构造题: 比赛的时候只想到:前面一样的数,后面 是类似1,2,3,4,5,6....t这 既是:t+1,t+1...,1,2,3,...t t+1的数目 可能 很多, 题解时YY出一个N 然后对N   ...

  8. HDU OJ 5317 RGCDQ( 2015多校联合训练第3场) 暴力打表+小技巧

    题目连接:Click here 题意:在一个[L,R]内找到最大的gcd(f[i],f[j])其中L<=i<j<=R,f[x]表示i分解质因数后因子的种类数.eg:f[10]=2(1 ...

  9. HDU OJ 5326 Work( 2015多校联合训练第3场) 并查集

    题目连接:戳ME #include <iostream> #include <cstdio> #include <cstring> using namespace ...

随机推荐

  1. PCB C# 连接MongoDB 数据库

    一.C# MongoDB 驱动下载 1.百度网盘:nuget下载地址(V2.7版本) https://pan.baidu.com/s/1VDsVcH1TMrXqhRCZVewZgA 2.VS 中NUg ...

  2. PCB 3D PCB 后续改进与扩展功能一些想法

    再次感受到WelGl实现3D效果的震撼, 一.目前功能: Gerber与钻孔 解析 并转为3D实景图,用户360度操控 二.后续改进扩展功能: 1.增加ODB++解析 2. 3D 尺寸标注(外形尺寸, ...

  3. [Apple开发者帐户帮助]六、配置应用服务(2)创建DeviceCheck私钥

    要验证与DeviceCheck服务的通信,您将使用启用了DeviceCheck的私钥. 首先创建并下载启用了DeviceCheck 的私钥.然后获取密钥标识符(kid)以创建JSON Web令牌(JW ...

  4. Servlet初始化与异步支持

    Shared libraries(共享库) / runtimes pluggability(运行时插件能力) 1.Servlet容器启动会扫描,当前应用里面每一个jar包的 ServletContai ...

  5. 使用logback实现http请求日志导入mongodb

    spring boot自带logback作为其日志新系统,但是在实际工作中,常常需要对日志进行管理或分析,如果只是单纯的将日志导入文本文件,则在查询时操作过于繁琐,如果将其导入mysql等关系型数据库 ...

  6. 树莓派-基于raspistill实现定时拍照

    raspistill 经过上一篇<<树莓派-安装摄像头模块>>之后 raspistill 是树莓派基于摄像头拍照命令 比如我要截取一张宽1024px,高768px,旋转180度 ...

  7. 使用TortoiseSVN碰到的几个问题(2)-冲突解决, 图标重载

    8)解决冲突 冲突分为两种:文件冲突---当两名(或更多)开发人员修改了同一个文件中相邻或相同的行时就会发生文件冲突.下面的属性冲突应该也属于文件冲突. 树冲突---当一名开发人员移动.重命名.删除一 ...

  8. MTD 门店统计

    DROP TABLE #MTD ' ,@endDate date = cast(getdate() as date) CREATE TABLE #MTD(bydate date) DECLARE @c ...

  9. PHP获取远程和本地文件信息(汇总)

    1.PHP filesize() 函数filesize() 函数返回指定文件的大小.若成功,则返回文件大小的字节数.若失败,则返回 false 并生成一条 E_WARNING 级的错误. 但是只能获取 ...

  10. vue-pdf的使用方法及解决在线打印预览乱码

    最近在用vue做项目的时候,页面中需要展示后端返回的PDF文件,于是便用到了vue-pdf,其使用方法为 : npm install --save vue-pdf 官网地址:https://www.n ...