zoj 3654

http://vjudge.net/contest/view.action?cid=46226#problem/K

Letty Whiterock is a seasonal youkai who's only seen during winter. She seems fittingly cold towards most of the humans she meets, and many humans fear her abilities. However, because she shares the same ability with Cirno, she is one of Cirno's friend.

zoj 3654_第1张图片

Letty Whiterock doesn't good at maths, so she asks Cirno to teach her - of course it's a bad idea - so that she can calculate quickly.

Cirno gives Letty some problems which have a expression with only '+' and '-' and two selection. Also, Cirno tells Letty how to calculate them:

  • If one of two selection is the number 9, then choose it. Cirno promises that there aren't two selections that are both the number 9.
  • Else, Letty should calculate the expression as usual, and then always choose the wrong selection. Cirno also promise that there aren't two wrong or right selection.

To make the problem more clear:

  • A number is a legal expression. For example, 123 is legal. We promise that each number's absolute value will not exceed 263-1.
  • [legal-expression]+[legal-expression] and [legal-expression]-[legal-expression] is legal, too. For example, 1+2+3 is legal, 1-2+3-4+5-6 is leagl, too.

Cirno thinks these problems can practise Letty's ability on maths. However, she doesn't sure about the answer of some questions. Please help Cirno to teach Letty!

Input

The input contains less than 1000 test cases. NOTICE that's no empty line between each test case.

For each test case, first line is a expression, whose length will not exceed 1000. The expression only contains numbers, '+' and '-'. And the expression is always legal.

The second line has two numbers, which mean the two selection. The two numbers' absolute value will not exceed 263-1.

Process to END_OF_FILE.

Output

For each test case, output "A" (without quotes) for choose the first selection and "B" (without quotes) for choose the second selection. The order of the selection is same as the order which appears in input.

Sample Input

1+2+3+4+5+6
9 21
1-2
-1 -2

Sample Output

A
B
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
char a[1005];
int b[10005];
int main()
{
    long long x,y,k;
    while(~scanf("%s",a))
    {
        memset(b,0,sizeof(b));
        scanf("%lld%lld",&x,&y);
        if(x==9)
             printf("A\n");
         else if(y==9)
             printf("B\n");
         else
        {
            int m=strlen(a);
            long long  sum=0;
            int j=0;
            long long count=0;
            for(int i=0; i<m; i++)
                if(a[i]=='+'||a[i]=='-')
                {
                    k=i+1;
                    if(a[i]=='+')
                        b[j++]=1;
                    else
                        b[j++]=-1;
                    count=sum;
                    //printf("[%lld,%lld]\n",sum,count);
                    sum=0;
                    break;
                }
                else
                {
                    sum=sum*10+(a[i]-'0');
                }
            for(int i=k; i<m; i++)
            {
                if(a[i]=='+')
                {
                    if(b[j-1]==1)
                        count+=sum;
                    else
                        count-=sum;
                    b[j++]=1;
                   // printf("[%lld,%lld]\n",sum,count);
                    sum=0;
                }
                else if(a[i]=='-')
                {
                    if(b[j-1]==1)
                        count+=sum;
                    else
                        count-=sum;
                    b[j++]=-1;
                   // printf("[%lld,%lld]\n",sum,count);
                    sum=0;
                }
                else
                {
                    sum=sum*10+(a[i]-'0');
                }
            }
            if(b[j-1]==1)
                count+=sum;
            else
                count-=sum;
           // printf("[%lld,%lld]\n",sum,count);
            if(count==x)
                printf("B\n");
            else
                printf("A\n");
        }
    }
    return 0;
}

方法二:
#include <stdio.h>
#include <string.h>


int main()
{
    int sy;
    long long n;
    while(scanf("%lld",&n)==1)
    {
        long long tmpn;
        char tmpc;
        while(1)
        {
            tmpc=getchar();
            if(tmpc=='-') sy=-1;
            else if(tmpc=='+') sy=1;
            else break;
            scanf("%lld",&tmpn);
            n+=tmpn*sy;
        }
        long long A,B;
        scanf("%lld %lld",&A,&B);
        if(A==9) puts("A");
        else if(B==9) puts("B");
        else if(n==A) puts("B");
        else if(n==B) puts("A");
    }
    return 0;
}


你可能感兴趣的:(zoj 3654)