codeforces 664B B. Rebus(乱搞题)

题目链接:

B. 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

题意:

给一个式子,让你选[1,n]中间的数把问号替换掉,使这个式子成立;

思路:

分分情况乱搞乱搞就行了,一次A不掉多搞几次就A了;

AC代码:

/*2014300227    664B - 14    GNU C++11    Accepted    15 ms    2036 KB*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+5;
typedef long long ll;
const int mod=1e9+7;
char str[110];
int ans[110],an[110];
int main()
{
    int cnt=0;
    int num1=0,num2=0,n;
    char s;
    while(1)
    {
        scanf("%c",&s);
        if(s=='+'){num1++;str[cnt++]=s;}
        else if(s=='-'){num2++;str[cnt++]=s;}
        else if(s=='=')break;
    }
    num1++;
    scanf("%d",&n);
    int l=num1-num2*n;
    int r=num1*n-num2;
    if(n>=l&&n<=r)
    {
        printf("Possible\n");
        if(num1-num2>n)
        {
            for(int i=0;i<num1;i++)
            {
                ans[i]=1;
            }
            int number=(num1-n)/num2;
            for(int i=0;i<num2;i++)
            {
                if(i<(num1-n)%num2)
                an[i]=number+1;
                else an[i]=number;
            }


        }
        else if(num2-num1>n)
        {
            for(int i=0;i<num2;i++)
            {
                an[i]=1;
            }
            int number=(num2+n)/num1;
            for(int i=0;i<num1;i++)
            {
                if(i<(num2+n)%num1)ans[i]=number+1;
                else ans[i]=number;
            }
        }
        else
        {

                int number=(n+num2)/num1;
                for(int i=0;i<num1;i++)
                {
                    if(i<(n+num2)%num1)ans[i]=number+1;
                    else ans[i]=number;
                }
                for(int i=0;i<num2;i++)
                {
                    an[i]=1;
                }

        }
             printf("%d ",ans[0]);
            int cut=1,cu=0;
            for(int i=0;i<cnt;i++)
            {
                printf("%c ",str[i]);
                if(str[i]=='+')
                {
                    printf("%d ",ans[cut++]);
                }
                else printf("%d ",an[cu++]);
            }
            printf("= %d",n);


    }
    else printf("Impossible\n");


    return 0;
}

 

posted @ 2016-04-17 12:01  LittlePointer  阅读(785)  评论(0编辑  收藏  举报