1、http://codeforces.com/problemset/problem/394/A
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、题目:
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:
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?
The single line contains the initial expression. It is guaranteed that the expression looks like A?+?B?=?C, where 1?≤?A,?B,?C?≤?100.
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.
||+|=|||||
|||+|=||||
|||||+||=||
Impossible
|+|=||||||
Impossible
||||+||=||||||
||||+||=||||||
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<stdio.h>
#include<string.h>
#include<algorithm>
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;elsei++;}int j=0;while(scanf("%c",&b[j])!=EOF){if(b[j]=='=')break;elsej++;}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<lc-1; k++)printf("%c",c[k]);printf("\n");}else if(la+lb-lc==2){if(la>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);}}elseprintf("Impossible\n");return 0;
}
/*
||+||=||
|+|||=||
|||+|=||
*/