The Archaeologist's Trouble II ZOJ - 2058

找规律的一道题

1.每一行中@和*总是交替出现,单独对每一行进行计算就可以

学长的代码在对j-1和j+1的处理值得学习

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 110;
char s[maxn][maxn],str[maxn][maxn];
int main()
{
    int n;
    while(cin>>n&&n>0)
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%s",s[i]+1);
            for(int j=1; j<=i; j++)
                str[i][j] = s[i][j];
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=i; j++)
            {
                if(s[i][j]=='?')
                    if((s[i][j-1]!='@'||j-1<1)&&(s[i][j+1]!='@'||j+1>n))
                        s[i][j] = '@';
                    else
                        s[i][j] = '*';
            }
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=i; j++)
            {
                if(str[i][j]=='?')
                    if((str[i][j-1]!='*'||j-1<1)&&(str[i][j+1]!='*'||j+1>n))
                        str[i][j] = '*';
                    else
                        str[i][j] = '@';
            }
        }
        int mmax = 0,mmin = 0;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=i; j++)
            {
                if(s[i][j]=='@')
                    mmax ++;
                if(str[i][j]=='@')
                    mmin++;
            }
        }
        cout<<mmax<<" "<<mmin<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sgsyacm/article/details/79808858
ZOJ