codeforces 394A Counting Sticks(题目虽简单,但是考虑的情况多,需仔细)

来源:互联网 发布:数据精灵9.8.2注册机 编辑:程序博客网 时间:2024/05/16 18:47

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、题目:

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 ifA + 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 likeA + 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<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;        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<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);        }    }    else        printf("Impossible\n");    return 0;}/*||+||=|||+|||=|||||+|=||*/


 

0 0
原创粉丝点击