CodeForces 663A Rebus
A. Rebus
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
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.
Examples
input
? + ? - ? + ? + ? = 42
output
Possible
9 + 13 - 39 + 28 + 31 = 42
input
? - ? = 1
output
Impossible
input
? = 1000000
output
Possible
1000000 = 1000000
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
char a[1000];
int n;
int num1,num2;
int b[1000];
int main()
{
gets(a);
int len=strlen(a);
num1=0;num2=0;
int cnt=1;int pos;int now=1;b[0]=1;
for(int i=0;i<len;i++)
{
if(a[i]=='+') {b[cnt++]=1;now++;}
if(a[i]=='-') {b[cnt++]=-1;now--;}
if(a[i]=='=')
{
pos=i;
break;
}
}
n=0;
for(int i=pos+1;i<len;i++)
{
if(a[i]<='9'&&a[i]>='0')
n=n*10+a[i]-'0';
}
for(int i=0;i<cnt;i++)
{
while(now<n&&b[i]<n&&b[i]>0)
now++,b[i]++;
while(now>n&&b[i]>-n&&b[i]<0)
now--,b[i]--;
}
if(now!=n)
{
printf("Impossiblen");
return 0;
}
printf("Possiblen");
int j=0;
for(int i=0;i<len;i++)
{
if(a[i]!='?')
printf("%c",a[i]);
else
{
printf("%d",abs(b[j++]));
}
}
cout<<endl;
return 0;
}
- Python 里 and、or 的计算规则
- Python中赋值、浅拷贝与深拷贝
- git 简易使用说明
- 开发篇-MySQL分区(一)
- Establishing SSL connection without server's identity verification is not recommended. According to
- Django-认证系统
- javascript:双链表-插入排序
- javascript:二叉搜索树 实现
- 自然语言处理 语言模型介绍
- 口水先擦干!从大数据看外卖如何拯救“忙”与“宅”
- javascript:巧用eval函数组装表单输入项为json对象
- Django——model基础
- java学习:日期的运算
- ORACLE:写Function时,传入参数变量名的注意事项
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法