CSES – Number Spiral

數學

#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
using namespace std;
//declare
int t;
//
signed main(){
//    fastio;
    cin>>t;
    while(t--){
        int row,col; cin>>row>>col;
        if(row >= col){
            if(row%2) cout<<(row-1)*(row-1)+1 + col - 1<<"\n";
            else cout<<row*row - col + 1<<"\n";
        }
        if(col > row){
            if(col%2) cout<<col*col - row + 1<<"\n";
            else cout<<(col-1)*(col-1)+1 + row - 1<<"\n";
        }
    }
    return 0;
}

相關文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *