zoukankan      html  css  js  c++  java
  • 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
    ");
            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
    ");
    
    
        return 0;
    }
  • 相关阅读:
    如何使用log4net记录日志
    js鼠标左右键,键盘值
    MagicAjax的内部原理初探(一)
    关于VS2005内置web服务器和IIS的区别问题(讨论,收集)
    在Linux中使用C#
    方便你的测试(TestDriven.NET)
    转载:数据库sharding(scale up to scale out)
    单元测试--爱你不容易
    你期待已久的ASP.NET Atlas(一)[翻]
    Ajax底层代码简析(可直接用的框架)
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5400692.html
Copyright © 2011-2022 走看看