华东交通大学2015年ACM“双基”程序设计竞赛1007
Problem G
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 681 Accepted Submission(s) : 192
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
给定方程 x ^ 2 + bx + c = 0,求解方程由哪两个一次多项式(x + p) , (x + q)(p , q均为整数且p <= q)相乘得到
Input
输入包含多组测试样例(10组左右),处理到文件结束,每组数据输入两个整数(b,c) (-100 <= b,c <= 100)
Output
每组测试数据中
如果存在输出两个整数p , q , 若不存在整数p,q,输出impossible;
如果存在输出两个整数p , q , 若不存在整数p,q,输出impossible;
Sample Input
3 2 1 2
Sample Output
1 2 impossible
Author
首先当然要判断是否有解啦~(b*b-4*ac)是否小于0
然后用求根公式算两次,一次用int 一次用double 再算出误差,误差范围内就符合条件
#include<stdio.h> //#include<bits/stdc++.h> #include<string.h> #include<iostream> #include<math.h> #include<sstream> #include<set> #include<queue> #include<map> #include<vector> #include<algorithm> #include<limits.h> #define inf 0x3fffffff #define INF 0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define ULL unsigned long long using namespace std; int main() { int b,c; while(~scanf("%d%d",&b,&c)) { int sum_1; double sum_2; //-b+-sqrt(b*b-4*a*c)/2 if((b*b*1.0-4.0*c)<0) { printf("impossible\n"); continue; } sum_1=sqrt(b*b-4*c); sum_2=(double)sqrt(b*b-4*c); int ans_1,ans_2; double ans_3,ans_4; ans_1=(-1*b+sum_1)/2; ans_2=(-1*b-sum_1)/2; ans_3=(double)(-1.0*b+sum_2)/2; ans_4=(double)(-1.0*b-sum_2)/2; if(abs(ans_1-ans_3)<=1e-6&&abs(ans_2-ans_4)<=1e-6) { int x,y; x=-min(ans_1,ans_2); y=-max(ans_1,ans_2); printf("%d %d\n",min(x,y),max(x,y)); } else { printf("impossible\n"); } } return 0; }