当前位置:编程学习 > C/C++ >>

UVA11538(Chess Queen )

Chess Queen
Input: Standard Input

Output: StandardOutput

You probably know how the game ofchess is played and how chess queen operates. Two chess queens are in attackingposition when they are on same row, column or diagonal of a chess board.Suppose two such chess queens (one black and the other white) are placed on(2x2) chess board. They can be in attacking positions in 12 ways, these areshown in the picture below:

 

\

 
  

 
Figure: in a (2x2) chessboard 2 queens can be in attacking position in 12 ways
 

Given an (NxM)board you will have to decide in how many ways 2 queens can be in attackingposition in that.

Input
Input file can contain up to 5000lines of inputs. Each line contains two non-negative integers which denote thevalue of M and N (0< M, N£106) respectively.

Input is terminated by a line containing two zeroes.These two zeroes need not be processed.

Output
For each line of input produceone line of output. This line contains an integer which denotes in how manyways two queens can be in attacking position in  an (MxN) board, wherethe values of M and N came from the input. All output values willfit in 64-bit signed integer.

   

Problemsetter: Shahriar Manzoor

Special Thanks to: Mohammad Mahmudur Rahman

思路:用加法原理,找规律!

 

[cpp]
#include <iostream>  
#include <algorithm>  
#include <cstdio>  
//11538 Chess Queen Accepted    C++ 0.024   2013-04-22 08:15:00  
using namespace std; 
 
int main() 

    unsigned long long n, m; 
    while(cin >> n >> m)  { 
        if(!m&&!n) break; 
        if(n>m) swap(n, m); 
        long long res = n*m*(m+n-2)+2*n*(n-1)*(3*m-n-1)/3 ; 
        cout << res << endl; 
    } 
    return 0; 

#include <iostream>
#include <algorithm>
#include <cstdio>
//11538 Chess Queen Accepted  C++ 0.024  2013-04-22 08:15:00
using namespace std;

int main()
{
    unsigned long long n, m;
    while(cin >> n >> m)  {
        if(!m&&!n) break;
        if(n>m) swap(n, m);
        long long res = n*m*(m+n-2)+2*n*(n-1)*(3*m-n-1)/3 ;
        cout << res << endl;
    }
    return 0;
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,