HDOJ1017解题报告

Problem Description

Given two integers n and m, count the number of pairs of integers (a,b) such that 0 < a < b < n and (a^2+b^2 +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

 

题意:

            先输入一个数N然后会分N块输入,每块输入每次2个数,n,m,知道n=m=0时结束,当a和b满足0<a<b<n且使(a^2+b^2 +m)/(ab) 的值为整数时,那么这对a和b就是一组

            解 输出每次n和m对应的所有解的个数,但是要注意在每一块的输出中间有一个回车。

代码:

 

#include <iostream>
#include <string>
#include <cmath>
#include<iomanip>
using namespace std;

void main(){
	int N,n,m,num;
	cin>>N;
	//cin.get();加不加无所谓
	for(int r=0;r<N;r++){
		num = 0;
		while(cin>>n>>m && !(n==0&&m==0)){
			num++;
			int count = 0;
			for(int i=1;i<n;i++){
				for(int j=i+1;j<n;j++){
					double tempd = (i*i+j*j+m)/(double)(i*j);
					int tempi = (i*i+j*j+m)/(i*j);
					if(abs(tempd-tempi)<1e-6)
						count ++;
				}
			}
			cout<<"Case "<<num<<": "<<count<<endl;
		}
		if(r<N-1)
			cout.put('\n');

	}
}

 

这道题就是输出有些问题,不明题目中的N到底是什么:

image

还有那block,应该是块吧。。

刚开始也参考了别人的代码:

   1. #include<stdio.h>  
   2. int main()  
   3. {  
   4.     int a,b,m,n,t,i,k,y;  
   5.         scanf("%d",&y);  
   6.          for(i=0;i<y;i++)  
   7.          {  
   8.              k=1;  
   9.             while(scanf("%d%d",&n,&m),n||m)  
  10.             {  
  11.                 t=0;  
  12.                 for(a=1;a<n;a++)  
  13.                 {  
  14.                    for(b=a+1;b<n;b++)  
  15.                    {  
  16.                         if((a*a+b*b+m)%(a*b)==0)  
  17.                         t++;  
  18.                    }  
  19.                 }  
  20.                 printf("Case %d: %d\n",k,t);  
  21.                 k++;  
  22.             }  
  23.             if(i!=y-1)  
  24.             printf("\n");  
  25.         }  
  26.     return 0;  
  27. }  

 

总是不理解其中为什么要有for(i=0;i<y;i++)和 if(i!=y-1)   printf("\n");  直接粘贴过来,发现还是WA,仔细看看,有个k=1;在里面,原来是它是指每个block的序号要从头开始计算 。。。

posted @ 2012-01-12 21:44  Ю詺菛╀時代  阅读(150)  评论(0编辑  收藏  举报