9 155 41 -560 0 Sample OutputNoYesYes/********************************************************************************************這個題可以轉(zhuǎn)化成一個一元二次方程,只要存在根,且為整數(shù),即可滿足題意。/******************x + y = nx * Y = m/******************兩式子聯(lián)立可得:x*x - n*x + m = 0(即一個一元二次方程,當根x是整數(shù)是,即可滿足題意,x是整數(shù),y當然也成了整數(shù))1.先判斷方程的根個數(shù),令d = n*n -4*m 若d < 0 ,無解,直接輸出No 若d >=0 ,有解,進而判斷他的根是不是整數(shù)2.用求根公式得出根。根 x1 = ( n + sqrt(d) )/ 2; x2 = ( n - sqrt(d) )/ 2; 判斷x1,x2是不是整數(shù)。/*********************************************************
代碼如下:
/*********************************************************
#include<stdio.h>#include<math.h>int main(){ int n,m; double d; while(scanf("%d%d",&n,&m),n||m) { d=((double)(n*n-4*m)); if(d<0) { puts("No"); } else if(d==0) { if(n%2==0) //根x = n / 2 ;只需要判斷n的奇偶性,即可知道根是不是整數(shù) { puts("Yes"); } else { puts("No"); } } else { double u=(n+sqrt(d))/2.0; double j=(n-sqrt(d))/2.0; if(u-(int)u==0&&j-(int)j==0) { puts("Yes"); } else { puts("No"); } } } return 0;}
新聞熱點
疑難解答