Codeforces Round #347 (Div.2)_B. Rebus
题目链接:http://codeforces.com/contest/664/problem/B
1 second
256 megabytes
standard input
standard output
You are given a rebus of form ? + ? - ? + ? = n, consisting of only question marks, separated by arithmetic operation '+' and '-', equality and positive integer n. The goal is to replace each question mark with some positive integer from 1 to n, such that equality holds.
The only line of the input contains a rebus. It's guaranteed that it contains no more than 100 question marks, integer n is positive and doesn't exceed 1 000 000, all letters and integers are separated by spaces, arithmetic operations are located only between question marks.
The first line of the output should contain "Possible" (without quotes) if rebus has a solution and "Impossible" (without quotes) otherwise.
If the answer exists, the second line should contain any valid rebus with question marks replaced by integers from 1 to n. Follow the format given in the samples.
? + ? - ? + ? + ? = 42
Possible
9 + 13 - 39 + 28 + 31 = 42
? - ? = 1
Impossible
? = 1000000
Possible
1000000 = 1000000
解题报告:
看到大神们的代码,简直碉堡了。
1、这里的输入要掌控好。
2、思路:先置每个位置为1,记录下加上的数的个数sa,减去的个数sb,这样,p(离目标结果)只差p-n了。
3、根据每个问号前面的符号,自增,达到结果。
#include <cstdio> using namespace std; int a[]; ///结果
int v[]; ///记录每个符号 char c[]; ///符号命令 int main()
{
scanf("%s",c); ///输入问号
scanf("%s",c); ///输入符号
int m=; ///?的个数,即要填的数字的个数
int sa=; ///加数的个数
int sb=; ///减数的个数
v[]=; while(c[]!='=')
{
if(c[]=='+')
{
v[++m]=;
sa++;
}
else {
v[++m]=-;
sb++;
}
scanf("%s",c);///输入问号
scanf("%s",c);///输入符号
} int n;
scanf("%d",&n); ///目标结果 for(int i=;i<=m;i++) ///全部填上1
a[i]=; int p=sa-sb; for(int i=;i<=m;i++) ///一个一个处理各个数
{
while((p<n)&&(v[i]==)&&(a[i]<n))
a[i]++,p++;
while((p>n)&&(v[i]==-)&&(a[i]<n))
a[i]++,p--;
} if(p!=n)
{
printf("Impossible\n");
return ;
}
printf("Possible\n");
printf("%d ",a[]); for(int i=;i<=m;i++)
{
if(v[i]==) printf("+ ");
else printf("- ");
printf("%d ",a[i]);
}
printf("= %d\n",n);
}
Codeforces Round #347 (Div.2)_B. Rebus的更多相关文章
- Codeforces Round #347 (Div. 2) B. Rebus
题目链接: http://codeforces.com/contest/664/problem/B 题意: 给你一个等式,把等式左边的问号用1到n(n为等式右边的数)的数填好,使得等式成立 题解: 贪 ...
- Codeforces Round #347 (Div. 2)
unrating的一场CF A - Complicated GCD #include <bits/stdc++.h> const int N = 1e5 + 5; char a[105], ...
- Codeforces Round #347 (Div. 2) C. International Olympiad 找规律
题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...
- Codeforces Round #347 (Div.2)_A. Complicated GCD
题目链接:http://codeforces.com/contest/664/problem/A A. Complicated GCD time limit per test 1 second mem ...
- Codeforces Round #290 (Div. 2) _B找矩形环的三种写法
http://codeforces.com/contest/510/status/B 题目大意 给一个n*m 找有没有相同字母连起来的矩形串 第一种并查集 瞎搞一下 第一次的时候把val开成字符串了 ...
- Codeforces Round #347 (Div. 2) (练习)
A: 题意:找到[a, b]的最大公约数: 思路:相同时为本身,不同时为1. 套路:碰到水题别想太多: 猜想:两个相邻数,必有一奇一偶,如果偶数有因子3或者其他,奇数可不可能有相同的呢? 枚举一些数后 ...
- codeforces Round #347 (Div. 2) C - International Olympiad
思路:从后往前一位一位的模拟,每次判断一下当前枚举的数是否之间枚举过了.或者当前枚举数过小,小于1989. #include<cstdio> #include<cstring> ...
- Codeforces Round #366 (Div. 2)_B. Spider Man
B. Spider Man time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #332 (Div. 2)_B. Spongebob and Joke
B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
随机推荐
- 简述wcf应用
一.新建wcf 如下图:wcf可以简历俩种形式 1.库文件,就是一个类库文件,可以用windows服务或控制台开启. 2.服务应用程序,可以直接IIS上面发布. 二.库文件自动生成的类 接口类 usi ...
- 16-----BBS论坛
BBS论坛(十六) 16.登录功能完成 (1)front/forms.py class SigninForm(BaseForm): telephone = StringField(validators ...
- JavaSE---Collections
1.简介: Collections是一个工具类 1.1 排序 a,正序 sort是其静态方法,有2种参数形式: public static <T extends Comparable<? ...
- jQuery自动完成插件flexselect
项目中使用flexselect自动完成插件时遇到一个问题 刚开始以为是js的引用顺序有问题,但是查看后发现不是js引用顺序问题 js引用顺序如下 最后查找资料也没有解决问题,一直提示这个错误 后来在f ...
- line-height 150%和1.5的区别
好惨啊,九点多了没事做还不能下班,坑爹的测试啊~~~ 刚才拿这个问题调戏了下部门一位资深前端开发,结果发现,他居然不会,呵呵~~~ 这里就说下吧 html: <div class="p ...
- 局部安装webpack时,使用webpack命令时提示webpack不是内部命令解决方法
现在js发展太快了,根本看不懂啊.于是乎想做做功课,于是乎看到了这些“奇怪”的写法,原来好多都是遵循了 ECMASCRIPT6,好吧,在本地看看怎么用的吧.写在本地的环境下, 发现各种报错,根本不能用 ...
- rail 怎样在已有数据库上继续开发
今天刷贴看到了这篇文章http://ruby-china.org/topics/16493,老大回复的很有意义,在这里备份一个 而要把现有的数据库纳入 Migration,一个简单方法: 创建一个空 ...
- .NET平台下使用C#连接各种数据库
在.NET平台下,通常我们需要连接不同的数据库,这就需要我们配置连接字符串以及提供常用的class进行数据存取. 1.C#连接Access @"Provider=Microsoft.ACE. ...
- 《JavaScript高级程序设计》第5章 Object类型---Array---学习心得
1.创建数组的方法: 使用Array构造函数: var colors=new Array() var colors=new Array(20)//l创建一个包含20项的数组 var colors=ne ...
- .NET面试题3
1.什么是拆箱和装箱? 2.什么是箱子? 3.箱子放在哪里? 4.装箱和拆箱有什么性能影响? 5.如何避免隐身装箱? 6.箱子的基本结构? 7.装箱的过程? 8.拆箱的过程? 9.下面这段代码输出什么 ...