2 条题解

  • 1
    @ 2024-10-24 11:53:44

    [CSP-J 2023] 一元二次方程 题解

    大模拟!

    解题思路

    题目比较长,但中心就是求一元二次方程的最大实数解,否则输出 NO,读题就可以知道:

    Δ = b2  4ac\Delta \ = \ b^{2} \ - \ 4ac

    如果 Δ < 0\Delta \ < \ 0,就没有实数解

    如果 Δ 0\Delta \ \ge 0,就依照题意来写:

    两个实数解 x1,2 = b±Δ2ax_{1,2} \ = \ \frac{-b\pm \sqrt{\Delta} }{2a}

    2a < 02a \ < \ 0 时,应当用 b±Δ2a\frac{-b\pm \sqrt{\Delta} }{2a}

    AC Code

    # include <bits/stdc++.h>
    using namespace std;
    bool kaifang(int x) {
    	int k = sqrt(x);
    	return k * k == x;
    }
    void huajie(int fenzi, int fenmu) {
    	int fu = 1;
    	if (fenzi  < 0) {
    		fu = -1;
    		fenzi = -fenzi;
    	}
    	int k = __gcd(fenzi, fenmu);
    	fenzi /= k;
    	fenmu /= k;
    	if (fenzi % fenmu == 0) {
    		cout << fu * fenzi / fenmu;
    	} else {
    		cout << fenzi * fu << "/" << fenmu;
    	}
    }
    int check(int x) {
    	int ret = 1;
    	for (int i = 2; i * i <= x; i++) {
    		if (x % (i * i) == 0) {
    			ret = i;
    		}
    	}
    	return ret;
    }
    void huajie2(int fenzi, int fenmu, int delta) {
    	if (fenzi % fenmu == 0) {
    		if (fenzi / fenmu == 1) {
    			cout << "sqrt(" << delta << ")" << "\n";
    		} else {
    			cout << fenzi / fenmu << "*sqrt(" << delta << ")" << "\n";
    		}
    		return;
    	}
    	int k = __gcd(fenzi, fenmu);
    	fenzi /= k;
    	fenmu /= k;
    	if (fenzi == 1) {
    		cout << "sqrt(" << delta << ")/" << fenmu << "\n";
    	} else {
    		cout << fenzi << "*sqrt(" << delta << ")/" << fenmu << "\n";
    	}
    }
    int main() {
        freopen("uqe.in", "r", stdin);
        freopen("uqe.out", "w", stdout);
    	int T, M;
    	scanf("%d%d", &T, &M);
    	while (T--) {
    		int a, b, c;
    		scanf("%d%d%d", &a, &b, &c);
    		if (a < 0) {
    			a *= -1;
    			b *= -1;
    			c *= -1;
    		}
    		int delta = b * b - 4 * a * c;
    		if (delta < 0) {
    			printf("NO\n");
    			continue;
    		}
    		if (kaifang(delta)) {
    			int ret;
    			int flag = 0;
    			int k = sqrt(delta);
    			ret = (-b + k) / (2 * a);
    			if ((-b + k) % (2 * a) == 0) {
    				printf("%d\n", ret);
    				continue;
    			}
    			huajie(-b + sqrt(delta), 2 * a);
    			printf("\n");
    			continue;
    		}
    		if (b != 0) {
    			huajie(-b, 2 * a);
    			printf("+");
    		}
    		int yinzi = check(delta);
    		delta /= yinzi * yinzi;
    		huajie2(yinzi, 2 * a, delta);
    	}
        return 0;
    }
    
    • @ 2025-4-20 14:39:22

      少: fclose(stdin/stdout);fclose(stdin/stdout);

    • @ 2025-5-4 20:41:44

      @ 有freopen了

  • 0
    @ 2025-10-12 10:06:44

    请输入文本

    大模拟

    #include<bits/stdc++.h>
    using namespace std;
    int t,m;
    int gcd(int x,int y){return y==0?x:gcd(y,x%y);}
    void print(int p,int q){
    	if(p==0){
    		cout<<0;
    		return;
    	}
    	int g=gcd(p,q);
    	p/=g;q/=g;
    	if(q<0){
    		p=-p;
    		q=-q;
    	}
    	if(q==1)cout<<p;
    	else cout<<p<<'/'<<q;
    }
    pair<int,int>del(int n){
    	int k=1,r=n;
    	for(int i=2;i*i<=r;++i){
    		if(r%(i*i)==0){
    			while(r%(i*i)==0){
    				r/=(i*i);
    				k*=i;
    			}
    		}
    	}
    	return {k,r};
    }
    int main(){
    	cin>>t>>m;
    	while(t--){
    		int a,b,c;
    		cin>>a>>b>>c;
    		int delta=b*b-4*a*c;
    		if(delta<0){
    			cout<<"NO"<<endl;
    			continue;
    		}
    		int sqde=round(sqrt((long double)delta));
    		if(sqde*sqde==delta){
    			int d=2*a;
    			int num;
    			if(a>0)num=-b+sqde;
    			else num=-b-sqde;
    			print(num,d);
    			cout<<endl;
    		}else{
    			if(b!=0){
    				print(-b,2*a);
    				cout<<'+';
    			}
    			auto ss=del(delta);
    			int k=ss.first;
    			int r=ss.second;
    			int d=abs(2*a);
    			int g=gcd(k,d);
    			int p=k/g,q=d/g;
    			if(p==1&&q==1)cout<<"sqrt("<<r<<')';
    			else if(q==1)cout<<p<<"*sqrt("<<r<<')';
    			else if(p==1)cout<<"sqrt("<<r<<")/"<<q;
    			else cout<<p<<"*sqrt("<<r<<")/"<<q;
    			cout<<endl;
    		}
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    308
    时间
    1000ms
    内存
    512MiB
    难度
    7
    标签
    递交数
    34
    已通过
    10
    上传者