zoukankan      html  css  js  c++  java
  • Leetcode-640 Solve the Equation(求解方程)

      1 class Solution
      2 {
      3     private:
      4         enum op {PLUS,MINUS};
      5         int left_constant_sum = 0;
      6         int left_x_sum = 0;
      7         int right_constant_sum = 0;
      8         int right_x_sum = 0;
      9     public:
     10         void pre_measure(string& s)
     11         {
     12             for(int i = 0;i < s.size();i ++)
     13             {
     14                 if(s[i]=='0'&&s[i+1]=='x'&&(i==0||!isdigit(s[i-1])))
     15                     s[i+1]='0';
     16             }
     17         }
     18         string solveEquation(string equation)
     19         {
     20             enum op OP = PLUS;
     21             int index;
     22             int tmp_store = 0;
     23             int zero_x_warning;
     24             pre_measure(equation);
     25             for(index = 0; equation[index]!='='; index ++)
     26             {
     27                 if(isdigit(equation[index]))
     28                 {
     29                     tmp_store = 10 * tmp_store + equation[index]-'0';
     30                 }
     31                 else if(equation[index]=='x')
     32                 {
     33                     if(OP==PLUS)
     34                     {
     35                         if(tmp_store!=0)
     36                             left_x_sum += tmp_store;
     37                         else
     38                             left_x_sum ++;
     39                     }
     40                     else
     41                     {
     42                         if(tmp_store!=0)
     43                             left_x_sum -= tmp_store;
     44                         else
     45                             left_x_sum --;
     46                     }
     47                     tmp_store = 0;
     48                 }
     49                 else // is '+' or '-'
     50                 {
     51                     if(OP==PLUS)
     52                         left_constant_sum += tmp_store;
     53                     else
     54                         left_constant_sum -= tmp_store;
     55                     if(equation[index]=='+')
     56                         OP = PLUS;
     57                     else
     58                         OP = MINUS;
     59                     tmp_store = 0;
     60                 }
     61             }
     62             if(OP==PLUS)
     63                 left_constant_sum += tmp_store;
     64             else
     65                 left_constant_sum -= tmp_store;
     66             tmp_store = 0;
     67 
     68             for(OP = PLUS,index ++; index < equation.size(); index ++)
     69             {
     70                 if(isdigit(equation[index]))
     71                 {
     72                     tmp_store = 10 * tmp_store + equation[index]-'0';
     73                 }
     74                 else if(equation[index]=='x')
     75                 {
     76                     if(OP==PLUS)
     77                     {
     78                         if(tmp_store!=0)
     79                             right_x_sum += tmp_store;
     80                         else
     81                             right_x_sum ++;
     82                     }
     83                     else
     84                     {
     85                         if(tmp_store!=0)
     86                             right_x_sum -= tmp_store;
     87                         else
     88                             right_x_sum --;
     89                     }
     90                     tmp_store = 0;
     91                 }
     92                 else // is '+' or '-'
     93                 {
     94                     if(OP==PLUS)
     95                         right_constant_sum += tmp_store;
     96                     else
     97                         right_constant_sum -= tmp_store;
     98                     if(equation[index]=='+')
     99                         OP = PLUS;
    100                     else
    101                         OP = MINUS;
    102                     tmp_store = 0;
    103                 }
    104             }
    105             if(OP==PLUS)
    106                 right_constant_sum += tmp_store;
    107             else
    108                 right_constant_sum -= tmp_store;
    109             //cout << left_constant_sum << " " << left_x_sum << " " << right_constant_sum << " " << right_x_sum << endl;
    110             
    111             string result;
    112             if(left_constant_sum == right_constant_sum
    113             && left_x_sum == right_x_sum)
    114                 result = "Infinite solutions";
    115             else if(left_x_sum == right_x_sum)
    116                 result = "No solution";
    117             else
    118             {
    119                 result = "x=";
    120                 char tmp_s[1000];
    121                 sprintf(tmp_s,"%d",(right_constant_sum-left_constant_sum)/(left_x_sum-right_x_sum));
    122                 result += tmp_s;
    123             }
    124             return result;
    125         }
    126 };
  • 相关阅读:
    Python-24-Django(Model Form、Ajax、上传文件、KindEditor)
    P23-Django-model、Form补充 & 序列化
    P22-Django-Session、CSRF、Form、信号
    21-Python-Django进阶补充篇
    Python-Django进阶
    Python-18-Django 基础篇
    17-前端开发之jQuery
    15-前端开发之JavaScript
    14-前端开发之CSS
    14-前端开发之HTML
  • 原文地址:https://www.cnblogs.com/Asurudo/p/9493399.html
Copyright © 2011-2022 走看看