zoukankan      html  css  js  c++  java
  • 九度-题目1103 二次方程计算器

    http://ac.jobdu.com/problem.php?pid=1103

    题目1103:二次方程计算器

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:3767

    解决:872

    题目描述:

    设计一个二次方程计算器

    输入:

    每个案例是关于x的一个二次方程表达式,为了简单,每个系数都是整数形式。

    输出:

    每个案例输出两个实数(由小到大输出,中间由空格隔开),保留两位小数;如果无解,则输出“No Solution”。

    样例输入:
    x^2+x=3x+4
    样例输出:
    -1.24 3.24
    来源:
    2011年上海交通大学计算机研究生机试真题
    对字符串的遍历判断,判断的条件比较多,要考虑到每一种可能的状态。
     1 #include <iostream>
     2 #include <iomanip>
     3 #include <math.h>
     4 #include <ctype.h>
     5 using namespace std;
     6 
     7 void operate(string str,int &A,int &B,int &C){
     8     for(int i=0;i<str.length();i++){///1
     9         if(str[i] == '+')
    10             continue;
    11         else if(str[i] == '-'){///2
    12             //不处理
    13         }
    14         else if(str[i] == 'x'){///3    //系数为正负1的情况
    15             if(i+1 < str.length() && str[i+1] == '^'){
    16                 if(i-1>=0 && str[i-1] == '-')
    17                     A = A - 1;
    18                 else
    19                     A += 1;
    20                 i = i + 2;
    21             }
    22             else{
    23                 if(i-1>=0 && str[i-1] == '-')
    24                     B = B -1;
    25                 else
    26                     B += 1;
    27                 i = i + 1;
    28             }
    29         }
    30         else{///4 是数字
    31             int temp = 0;
    32             int j = i;
    33             while(isdigit(str[i])){
    34                 temp = temp*10 + str[i] - '0';
    35                 i++;
    36             }
    37             if(j-1>=0 && str[j-1] == '-')
    38                 temp = 0 - temp;
    39             if(i< str.length() && str[i] == 'x'){
    40                 if(i+1 < str.length() && str[i+1] == '^'){
    41                     A += temp;
    42                     i = i + 2;
    43                 }
    44                 else{
    45                     B += temp;
    46                     i = i + 1;
    47                 }
    48             }
    49             else
    50                 C += temp;
    51         }
    52 
    53     }
    54 }
    55 
    56 int main(){
    57     string str;
    58     while(cin>>str){
    59 
    60         int j = str.find('=', 0);
    61         string str_left,str_right;
    62         str_left = str.substr(0,j);
    63         str_right = str.substr(j+1);
    64         int A1 = 0,B1 = 0,C1 = 0;
    65         int A2 = 0,B2 = 0,C2 = 0;
    66         operate(str_left,A1,B1,C1);
    67         operate(str_right,A2,B2,C2);
    68         int a = A1 - A2;
    69         int b = B1 - B2;
    70         int c = C1 - C2;   //化方程为标准式,a,b,c为系数
    71         int temp = b*b - 4*a*c;
    72         if(temp < 0)
    73              cout<<"No Solution"<<endl;
    74         else{
    75             double x1,x2;
    76             x1 = (double)(0-b+sqrt(temp))/(2*a);
    77             x2 = (double)(0-b-sqrt(temp))/(2*a);
    78             if(x1<x2)
    79                 cout<<fixed<<setprecision(2)<<x1<<" "<<x2<<endl;
    80             else
    81                 cout<<fixed<<setprecision(2)<<x2<<" "<<x1<<endl;
    82         }
    83     }
    84     return 0;
    85 }
  • 相关阅读:
    复习静态页面polo-360
    fsm三种建模思路比较
    web前端学习(一) j2ee环境搭配+jsp中的编码问题
    git与github建立链接(学习笔记)
    g++编译多个源原文件和头文件(转载)
    百度搜索引擎设置
    Google自带截图工具的使用
    git与github建立链接(将本次项目与网络GitHub同步) --转存笔记
    初识JQuery(1)-选择器
    同步与异步的区别
  • 原文地址:https://www.cnblogs.com/shenckicc/p/6862359.html
Copyright © 2011-2022 走看看