A Mathematical Curiosity

Problem Description
Given two integers n and m, count the number of pairs of integers (a,b) such that 0 < a < b < n and (a2+b2 +m)/(ab) is an integer.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Input
You will be given a number of cases in the input. Each case is specified by a line containing the integers n and m. The end of input is indicated by a case in which n = m = 0. You may assume that 0 < n <= 100.

Output
For each case, print the case number as well as the number of pairs (a,b) satisfying the given property. Print the output for each case on one line in the format as shown below.

Sample Input
1

10 1
20 3
30 4
0 0

Sample Output
Case 1: 2
Case 2: 4
Case 3: 5

只要讀懂題就行,輸入會分成幾個塊,每一個塊以0 0結束,輸出的時候塊與塊之間要有一個空行,具體我會在代碼中標註

C

#include <stdio.h>
#pragma warning(disable:4996)
int main(void)
{
	int N,m,n,count,a,b,Case;
	scanf("%d",&N);  //有多少個"塊"
	while(N--)
	{
		Case=1;         //每個塊中測試的數量是獨立的,如在第一個塊中有case1,case2等等,但是到了第2個塊仍要從1開始
		while(scanf("%d %d",&n,&m)&&(n||m))   //n和m同時爲0時,一個塊結束
		{
			for(count=0,b=1;b<n;b++)
				for(a=1;a<b;a++)
					if((a*a+b*b+m)%(a*b)==0)
						count++;
			printf("Case %d: %d\n",Case++,count);
		}
		if(N)     //注意每個塊之間要有一個空行,最後一個塊後面當然就沒空行了,所以這裏是if(N)
			putchar('\n');
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章