程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVA11538(Chess Queen )

UVA11538(Chess Queen )

編輯:C++入門知識

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;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved