codeforces 1027 B - Numbers on the Chessboard(打表找规律)

题目描述:http://codeforces.com/contest/1027/my

c++ 代码:



#include <iostream>

using namespace std;

typedef unsigned long long LL;
int a[1000][1000];
int main() {
  LL n,q;
   cin >> n >> q;


  while(q--) {
    LL x,y;
    cin >> x >> y;
    LL cnt=x+y;
    LL ans=0;
    if(cnt%2) ans=(n*n+1)/2;
    //cout << ans <<endl;
    //cout << (x/2)*n+(y/2)+1 <<endl;
    //cout <<(n+1)/2<<endl;
    if(x%2) {
        if(y%2)
        ans+=(x/2)*n+(y+1)/2;
        else ans+=(x/2)*n+(y)/2;
    }
    else {
        if(y%2)
        ans+=(x-1)/2*n+(n)/2+(y+1)/2;
        else ans+=(x-1)/2*n+(n+1)/2+y/2;
    }
    cout << ans <<endl;
  }
  //注释掉的是找规律的代码
/*
  while(cin >> n) {
    int cnt=1,flag=0,cnt1=(n*n+1)/2+1;
  for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
  {
      if((i+j)%2==0){a[i][j]=cnt++;}
      else {a[i][j]=cnt1++;}

  }
  for(int i=1;i<=n;i++) {
    for(int j=1;j<=n;j++)
       cout << a[i][j] <<" ";
     cout << endl;
  }
  */




  return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39475280/article/details/81980746