这个代码怎么爆10分
查看原帖
这个代码怎么爆10分
1327854
zhengyuchen201314楼主2024/9/22 10:53
#include <bits/stdc++.h> 
using namespace std;
int gcd(int x,int y){
	if(y==0) return x;
	if(x==0) return y;
	return gc(y%x,x);
}
int main()
{
	int t,m;
	cin >> t >> m;
	for(int i=1;i<=t;i++){
		int a,b,c;
		cin >> a >> b >> c;
		if((b*b-4*a*c)<0){
			cout<< "NO";
		}	
		else{
			if((b*b-4*a*c)==0){
				//只有一个根,为-b/2a
				if((0-b)%(2*a)==0){
					cout << ((0-b)/(2*a));
				}
				else{
					cout << (0-b)/gcd((0-b),(2*a)) << '/' <<(2*a)/gcd((0-b),(2*a));
				}
			}
			else{//有两个根
				if((((0-b)+(int)(sqrt(b*b-4*a*c)))%(a*2)==0)&&(sqrt(b*b-4*a*c)*sqrt(b*b-4*a*c))==(b*b-4*a*c)){
					cout <<((0-b)+(sqrt(b*b-4*a*c))/(2*a)) << endl;
					continue;
				}
				if((0-b)%(2*a)==0){
					if(((0-b)/(2*a))!=0) cout << ((0-b)/(2*a)) << '+';
				}
				else{
					cout << (0-b)/gcd((0-b),(2*a)) << '/' <<(2*a)/gcd((0-b),(2*a)) << '+';
				}
				if((sqrt(b*b-4*a*c)*sqrt(b*b-4*a*c))==(b*b-4*a*c)){
					int w=sqrt(b*b-4*a*c);
					if(w%2==0){
						cout << w/2;
					} 
					else{
						cout << w << '/' << 2;
					}
				}
				else{
					if((b*b-4*a*c)%4==0){
						cout << "sqrt(" << (b*b-4*a*c)/4 << ")";	
					}
					else cout << "sqrt(" << (b*b-4*a*c) << ")/2";
				}
			}
		}
		cout << '\n';
	}
	return 0;
}
2024/9/22 10:53
加载中...