博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 394A Counting Sticks(题目虽简单,但是考虑的情况多,需仔细)
阅读量:4035 次
发布时间:2019-05-24

本文共 3659 字,大约阅读时间需要 12 分钟。

1、

2、题目大意:

有一个由筷子摆成的加法算式,现在要移动其中的一根,但是不能移动‘+’和‘=’,使得算式成立,如果可以输出正确的算式,如果不成立输出‘Impossible’,

思路:

假设两个数分别是la,lb,他们的和是lc

那么将分成以下四种情况

1、if(la+lb-2==lc) 就将la或lb中的一根移到右边即可,但是这有一个问题,移动的时候注意如果la就一根,只能移动lb

2、if(la+lb==lc-2),就将lc的一根移到la或者lb那,

3、if(la+lb==lc)直接输出

4、其他情况都不成立,输出Impossible

3、题目:

A. Counting Sticks
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

When new students come to the Specialized Educational and Scientific Centre (SESC) they need to start many things from the beginning. Sometimes the teachers say (not always unfairly) that we cannot even count. So our teachers decided to teach us arithmetics from the start. And what is the best way to teach students add and subtract? — That's right, using counting sticks! An here's our new task:

An expression of counting sticks is an expression of type:

[
A sticks][sign
+][
B sticks][sign
=][
C sticks]
(1 ≤ A, B, C)

Sign + consists of two crossed sticks: one vertical and one horizontal. Sign = consists of two horizontal sticks. The expression is arithmetically correct if A + B = C.

We've got an expression that looks like A + B = C given by counting sticks. Our task is to shift at most one stick (or we can shift nothing) so that the expression became arithmetically correct. Note that we cannot remove the sticks from the expression, also we cannot shift the sticks from the signs + and =.

We really aren't fabulous at arithmetics. Can you help us?

Input

The single line contains the initial expression. It is guaranteed that the expression looks like A + B = C, where 1 ≤ A, B, C ≤ 100.

Output

If there isn't a way to shift the stick so the expression becomes correct, print on a single line "Impossible" (without the quotes). If there is a way, print the resulting expression. Follow the format of the output from the test samples. Don't print extra space characters.

If there are multiple correct answers, print any of them. For clarifications, you are recommended to see the test samples.

Sample test(s)
Input
||+|=|||||
Output
|||+|=||||
Input
|||||+||=||
Output
Impossible
Input
|+|=||||||
Output
Impossible
Input
||||+||=||||||
Output
||||+||=||||||
Note

In the first sample we can shift stick from the third group of sticks to the first one.

In the second sample we cannot shift vertical stick from + sign to the second group of sticks. So we cannot make a - sign.

There is no answer in the third sample because we cannot remove sticks from the expression.

In the forth sample the initial expression is already arithmetically correct and that is why we don't have to shift sticks.

 

4、AC代码:

#include
#include
#include
using namespace std;char a[305],b[105],c[105];int main(){ int i=0; while(scanf("%c",&a[i])!=EOF) { if(a[i]=='+') break; else i++; } int j=0; while(scanf("%c",&b[j])!=EOF) { if(b[j]=='=') break; else j++; } scanf("%s",c); int lc=strlen(c); int la=i; int lb=j; //printf("%d %d %d\n",la,lb,lc); if(la+lb==lc) printf("%s%s%s\n",a,b,c); else if(la+lb-lc==-2) { a[i]='|'; a[i+1]='+'; printf("%s%s",a,b); for(int k=0; k
1) { for(int k=1; k<=la; k++) printf("%c",a[k]); printf("%s%s",b,c); printf("|"); printf("\n"); } else { printf("%s",a); for(int i=1;i<=lb;i++) printf("%c",b[i]); printf("%s|\n",c); } } else printf("Impossible\n"); return 0;}/*||+||=|||+|||=|||||+|=||*/

 

转载地址:http://cwcdi.baihongyu.com/

你可能感兴趣的文章
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>
Maven跳过单元测试的两种方式
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
[leetcode BY python]1两数之和
查看>>
微信小程序开发全线记录
查看>>
Centos import torchvision 出现 No module named ‘_lzma‘
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
Django 的Error: [Errno 10013]错误
查看>>
机器学习实战之决策树(一)
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[leetCode By Python]111. Minimum Depth of Binary Tree
查看>>