九度OJ 1019:简单计算器 (基础题、DP)
生活随笔
收集整理的這篇文章主要介紹了
九度OJ 1019:简单计算器 (基础题、DP)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
時間限制:1 秒
內存限制:32 兆
特殊判題:否
提交:6725
解決:2454
題目描述:思路:
可以用動態規劃的思路來做。保存兩個數和中間的計算符,逐步向后推進即可。
據說用棧做這個題效果也不錯。
代碼:
#include <stdio.h> #include <string.h> #include <stdlib.h>#define M 200int isnumber(char c) {return ('0' <= c && c <= '9'); }double compute(double a, char op, double b) {switch(op){case '+':return a+b;case '-':return a-b;case '*':return a*b;case '/':return a/b;} }void combine(double *a, char *op, double *b, char opnew, double c) {if (opnew == '+' || opnew == '-'){*a = compute(*a, *op, *b);*op = opnew;*b = c;}else*b = compute(*b, opnew, c); }int main(void) {char s[M+1], tmp[M+1];int i, j;double a, b, c;char op, opnew;double res;while (gets(s)){if (strcmp(s, "0") == 0)break;a = 0.0;b = 0.0;op = '+';i = 0;while (s[i]){if (i != 0){i++;opnew = s[i++];i++;}elseopnew = '+';j = 0;while (isnumber(s[i]))tmp[j++] = s[i++];tmp[j] = '\0';c = atoi(tmp);combine(&a, &op, &b, opnew, c);}res = compute(a, op, b);printf("%.2lf\n", res);}return 0; } /**************************************************************Problem: 1019User: liangrx06Language: CResult: AcceptedTime:0 msMemory:912 kb ****************************************************************/轉載于:https://www.cnblogs.com/liangrx06/p/5084012.html
總結
以上是生活随笔為你收集整理的九度OJ 1019:简单计算器 (基础题、DP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 讨论IM软件企业知识—会谈session
- 下一篇: centos7加固手册