Codeforces 394A Counting Sticks

题目链接:Codeforces 394A Counting Sticks

简单题,注意下abc的范围。

#include <iostream>
#include <cstring>

using namespace std;

char s[1000];

int main()
{
    while(cin >> s)
    {
        int a,b,c;
        int len = strlen(s);
        int cnt = 0;
        for(int i = 0;i < len;i++)
        {
            if(s[i] == '+')
            {
                a = cnt;
                cnt = 0;
            }
            else if(s[i] == '=')
            {
                b = cnt;
                c = len - a - b - 2;
            }
            else
                cnt++;
        }
        if(a + b == c)
        {
            for(int i = 0;i < a;i++)
                cout<<"|";
            cout<<"+";
            for(int i = 0;i < b;i++)
                cout<<"|";
            cout<<"=";
            for(int i = 0;i < c;i++)
                cout<<"|";
            cout<<endl;
        }
        else if(a + 1 + b == c - 1 && c > 1)
        {
            a = a + 1;
            c = c - 1;
            for(int i = 0;i < a;i++)
                cout<<"|";
            cout<<"+";
            for(int i = 0;i < b;i++)
                cout<<"|";
            cout<<"=";
            for(int i = 0;i < c;i++)
                cout<<"|";
            cout<<endl;
        }
        else if(a - 1 + b == c + 1 && a > 1)
        {
            a = a - 1;
            c = c + 1;
             for(int i = 0;i < a;i++)
                cout<<"|";
            cout<<"+";
            for(int i = 0;i < b;i++)
                cout<<"|";
            cout<<"=";
            for(int i = 0;i < c;i++)
                cout<<"|";
            cout<<endl;
        }
        else if(a + b - 1== c + 1 && b > 1)
        {
            b = b - 1;
            c = c + 1;
             for(int i = 0;i < a;i++)
                cout<<"|";
            cout<<"+";
            for(int i = 0;i < b;i++)
                cout<<"|";
            cout<<"=";
            for(int i = 0;i < c;i++)
                cout<<"|";
            cout<<endl;
        }
        else
            cout<<"Impossible"<<endl;
    }
    return 0;
}


你可能感兴趣的:(Codeforces 394A Counting Sticks)