求佩尔方程的解

求佩尔方程的解

  • Description
  • 求关于x y的二次不定方程的解 x2-ny2=1
  • Input
  • 多组输入数据,先输入组数T 然后输入正整数n(n<=100)
  • Output
  • 对于每组数据输出一行,求y<=10000的最小正整数解 ,输出y的值,如果在此范围内没有解则输出No
  • Sample Input
  • 1
  • 73
  • Sample Output
    -No
#include<stdio.h>
#include<math.h>
int main()
{
 int t,n;
 long i,j;	//对 int范围懵
 int count;
 scanf("%d",&t);
 while(t--)
 {
  count=0;
  scanf("%d",&n);
  for(i=1;i<=10000;i++)
  {
   j=floor(sqrt(n*i*i+1)+0.5);	//+0.5确保前式有用
   if(j*j-n*i*i==1)
   {
    printf("%ld\n",i);
    count++;
    break;
   }
  }
  if(count==0)
   printf("No\n");
 } 
}

猜你喜欢

转载自blog.csdn.net/weixin_43613299/article/details/85162708