整数解
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 40071 Accepted Submission(s): 13985
题目
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2092
Problem Description
有二个整数,它们加起来等于某个整数,乘起来又等于另一个整数,它们到底是真还是假,也就是这种整数到底存不存在,实在有点吃不准,你能快速回答吗?看来只能通过编程。
例如:
x + y = 9,x * y = 15 ? 找不到这样的整数x和y
1+4=5,1*4=4,所以,加起来等于5,乘起来等于4的二个整数为1和4
7+(-8)=-1,7*(-8)=-56,所以,加起来等于-1,乘起来等于-56的二个整数为7和-8
Input
输入数据为成对出现的整数n,m(-10000
Output
只需要对于每个n和m,输出“Yes”或者“No”,明确有还是没有这种整数就行了。
Sample Input
9 15
5 4
1 -56
0 0
Sample Output
No
Yes
Yes
题解
数学公式
x=−b±b2−4ac−−−−−−−√2a
x
=
−
b
±
b
2
−
4
a
c
2
a
ac代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//x*x-n*x+m=0
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)&&(n||m))
{
double d=n*n-4*m;
if(d<0)printf("No\n");
else
{
d=sqrt(d);
double a= (double) (n+d)/2,b= (double) (n-d)/2;
if(( (int)a ==a) &&((int)b ==b))
printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
总结:
wa了几次:输出的YES,NO 没有注意大小写,此题非常简单,要注意细节
目录
用 [TOC]
来生成目录: