zoukankan      html  css  js  c++  java
  • 题目1103:二次方程计算器(字符串操作以及基础数学知识)

    题目链接:http://ac.jobdu.com/problem.php?pid=1103

    详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

    参考代码:

    //
    //  1103 二次方程计算器.cpp
    //  Jobdu
    //
    //  Created by PengFei_Zheng on 07/05/2017.
    //  Copyright © 2017 PengFei_Zheng. All rights reserved.
    //
     
    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    #include <cstring>
    #include <cmath>
    #include <climits>
    #include <stack>
    #define MAX_SIZE 101
    //#define debug
     
    using namespace std;
     
    int leftA, rightA;
    int leftB, rightB;
    int leftC, rightC;
     
    char equation[1001];
     
    void init(){
        leftA = rightA = 0;
        leftB = rightB = 0;
        leftC = rightC = 0;
    }
     
    void cal(string str, int &a, int &b, int &c){
        int len = (int)str.size();
        int i = 0;
        for( ; i < len ; i++){
            if(str[i]=='+') continue;
            else if(str[i]=='-'){
            }
            else if(str[i]=='x'){// 系数为1
                if(i+1 < len && str[i+1]=='^'){
                    if(i-1>=0 && str[i-1]=='-'){
                        a-=1;
                    }
                    else{
                        a+=1;
                    }
                    i+=3;
                }
                else{
                    if(i-1>=0 && str[i-1]=='-'){
                        b-=1;
                    }
                    else{
                        b+=1;
                    }
                    i+=1;
                }
            }
            else{
                int tmp = 0;
                int j = i;
                while(isdigit(str[i])){
                    tmp = tmp*10 + str[i]-'0';
                    i++;
                }
                if(j-1>=0 && str[j-1] == '-'){
                    tmp = 0 - tmp;
                }
                if(i < len && str[i]=='x'){
                    if(i+1<len && str[i+1]=='^'){
                        a+=tmp;
                        i+=3;
                    }
                    else{
                        b+=tmp;
                        i+=1;
                    }
                }
                else{
                    c+=tmp;
                }
            }
        }
    }
    int main(){
    #ifdef debug
        freopen("/Users/pengfei_zheng/Desktop/input.txt", "r", stdin);
    #endif
        while(scanf("%s",equation)!=EOF){
            string s(equation);
            int len = (int)s.size();
            int idx = 0;
            for(int i = 0 ; i < len ; i++){
                if(s[i]=='='){
                    idx = i;
                    break;
                }
            }
            init();
            string strLeft = s.substr(0,idx);
            string strRight = s.substr(idx+1);
            cal(strLeft,leftA,leftB,leftC);
            cal(strRight,rightA,rightB,rightC);
            int a = leftA - rightA;
            int b = leftB - rightB;
            int c = leftC - rightC;
            int tmp = b*b - 4*a*c;
            if(tmp<0){
                printf("No Solution
    ");
            }
            else{
                double x1,x2;
                x1 = (double)(0-b+sqrt(tmp))/(2*a);
                x2 = (double)(0-b-sqrt(tmp))/(2*a);
                if(x1<x2){
                    printf("%.2lf %.2lf
    ",x1,x2);
                }
                else {
                    printf("%.2lf %.2lf
    ",x2,x1);
                }
            }
        }
        return 0;
    }
     
    /**************************************************************
        Problem: 1103
        User: zpfbuaa
        Language: C++
        Result: Pending
    ****************************************************************/
  • 相关阅读:
    【微服务架构】SpringCloud组件和概念介绍(一)
    getaddrinfo()详解
    通货膨胀背景下,只能好好搞技术了
    到此为止,重新开始
    二级指针
    一篇关于spice vdagent和利用virtmanager配置spice的文章
    局域网中两台无线路由器联接配置方法图文教程
    ovirt简介
    IIS fastcgi 超时处理
    关于eclipse无法自动编译的问题
  • 原文地址:https://www.cnblogs.com/zpfbuaa/p/6822359.html
Copyright © 2011-2022 走看看