暴力枚举H

来源:互联网 发布:php获取ip地区 编辑:程序博客网 时间:2024/06/02 04:47
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. 

题意:输入n,m, 计算同时满足0 < a < b < n和(a^2+b^2 +m)/(ab)是整数的a b对数的数目并按要求输出,第一行的N意思为有几个输入模块,模块间用空格隔开,以 0 0为结束标志,但在N与第一个模块间没有空格,最后一个模块稍有区别,这道题格式很重要,希望能帮到你! 

#include<stdio.h>
int main()
{
    int n,m,a,b, sum=0, j=1, p=1, N;


    scanf( "%d", &N );


   loop : while (j <=N )
    {while (scanf ( "%d %d", &n, &m) ! =  EOF )
    if( n == 0 && m == 0 )
    {
        if(j <  N)
        {printf ( "\n" );}
        j ++;
        p = 1;


        goto loop;
    }
    else
    {{for (b = 1; b < n; b++)
    {
      for ( a = 1; a < b; a++)
      {
        int  t = a*a + b*b + m;
        if ( t % (a*b) == 0)
            sum++;
      }
    }
    printf ( "Case %d: %d\n", p, sum );
    p = p + 1;
    sum = 0;
    }}
    }


    return 0;
}