hdu 2092 ?整數(shù)解
整數(shù)解
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 58916 Accepted Submission(s): 20533
Problem Description
有二個整數(shù),它們加起來等于某個整數(shù),乘起來又等于另一個整數(shù),它們到底是真還是假,也就是這種整數(shù)到底存不存在,實在有點吃不準(zhǔn),你能快速回答嗎?看來只能通過編程。
例如:
x + y = 9,x * y = 15 ? 找不到這樣的整數(shù)x和y
1+4=5,1*4=4,所以,加起來等于5,乘起來等于4的二個整數(shù)為1和4
7+(-8)=-1,7*(-8)=-56,所以,加起來等于-1,乘起來等于-56的二個整數(shù)為7和-8
Input
輸入數(shù)據(jù)為成對出現(xiàn)的整數(shù)n,m(-10000<n,m<10000),它們分別表示整數(shù)的和與積,如果兩者都為0,則輸入結(jié)束。
Output
只需要對于每個n和m,輸出“Yes”或者“No”,明確有還是沒有這種整數(shù)就行了。
Sample Input
9 15
5 4
1 -56
0 0
Sample Output
No
Yes
Yes

解題思路:將兩個方程聯(lián)立成一個一元二次方程,使用根的判別式進行求解;
代碼:
#include<stdio.h>
#include<math.h>
int main()
{
int n, m;
while (scanf("%d %d",&n,&m)!=EOF)
{
if (n == 0 && m == 0)
break;
int delta = n*n - 4 * m;
if (delta < 0)
{
printf("No\n");
continue;
}
int ac = sqrt(delta);
if (ac*ac != delta)
{
printf("No\n");
continue;
}
int frac_1 = n + ac;
int frac_2 = n - ac;
if (frac_1 % 2 == 0 && frac_2 % 2 == 0)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
評論
圖片
表情
