周赛(1)——中国象棋

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1450


#include "iostream"
#include "cstdio"
#include "cstring"
using namespace std;
int main()
{
    int n,i,t;
    int red,black;
    int offence_power=0;
    cin>>t;
    while(t--)
    {
        cin>>n;
        char a[17];
        for(i=0;i<=n-1;i++)
    {scanf("%*c%c",&a[i]);}//第一点错误:cin读取到'\0'就停止,把它留给了scanf,所以碰到空格需要%*c
        offence_power=0;
        int house_red=0,canon_red=0,house_black=0,canon_black=0;
        for(i=0;i<=n-1;i++)
        {


            if(a[i]=='A') offence_power+=16;
            if(a[i]=='B') {offence_power+=7;house_red=1;}
            if(a[i]=='C') {offence_power+=8;canon_red=1;}
            if(a[i]=='D') offence_power+=1;
            if(a[i]=='E') offence_power+=1;
            if(a[i]=='F') offence_power+=2;
            if(a[i]=='G') offence_power+=3;


        }
    {
        if(!(house_red==1&&canon_red==1)&&offence_power>1) offence_power-=1;//第二点错误,判断条件失误了,他们同时存在的时候就不减一,所以所有的反面就都要减一。
        if(offence_power<=1) offence_power=1;
    }
    red=offence_power;
        /*cout<<offence_power;*/
    offence_power=0;
    memset(a,0,16);
    cin>>n;
    for(i=0;i<=n-1;i++)
    {scanf("%*c%c",&a[i]);}
    for(i=0;i<=n-1;i++)
    {
        if(a[i]=='A') offence_power+=16;
        if(a[i]=='B') {offence_power+=7;house_black=1;}
        if(a[i]=='C') {offence_power+=8;canon_black=1;}
        if(a[i]=='D') offence_power+=1;
        if(a[i]=='E') offence_power+=1;
        if(a[i]=='F') offence_power+=2;
        if(a[i]=='G') offence_power+=3;
    }
    {
        if(!(house_black==1&&canon_black==1)&&offence_power>1) offence_power-=1;
        else if(offence_power<=1) offence_power=1;
    }


        black=offence_power;
            /*cout<<offence_power;*/
        if(red>black) cout<<"red"<<endl;
        if(black>red) cout<<"black"<<endl;
        if(black==red) cout<<"tie"<<endl;
    }
}


做这个题的体会是,细节决定成败。比如,cin读到哪里会停止,读到了些什么;对题目条件的理解,尤其是关键性条件;针对这个题而言,那四个标记(house_black,house_red,canon_black,canon_red)是这个题目的关键,存在它就变量改变,这是值得学习的。

你可能感兴趣的:(周赛(1)——中国象棋)