UVA10700

来源:互联网 发布:淘宝ted baker 编辑:程序博客网 时间:2024/05/16 15:39

题意:求出一组数据可能出现的最小值和最大值,符号只有‘+’和‘*’

思路:通常我们遵循乘法优先,可以求出最小值,至于最大值,我们就让加法成为优先级,相当于,先算加法,再算乘法,主要用数组模拟

#include <stdio.h>#include <string.h>#define N 30char str[N];double s[N], max, min;int main() {    int n;    scanf("%d", &n);    getchar();    while (n--) {        gets(str);        int i, top;        char ch;        i = top = 0;        ch = '+';        while (str[i] != '\0') {            int num = 0;            while (str[i] >= '0' && str[i] <= '9') {                num = num * 10 + str[i] - '0';                i++;             }            if (ch == '+')                s[++top] = num;            else                s[top] *= num;            if (str[i] != '\0')                ch = str[i++];        }        min = 0;        for (int i = 1; i <= top; i++)            min += s[i];        i = top = 0;        ch = '*';         while (str[i] != '\0') {            int num = 0;            while (str[i] >= '0' && str[i] <= '9') {                num = num * 10 + str[i] - '0';                i++;             }            if (ch == '*')                s[++top] = num;            else                s[top] += num;            if (str[i] != '\0')                ch = str[i++];         }        max = 1;        for (int i = 1; i <= top; i++)            max *= s[i];        printf("The maximum and minimum are %.0lf and %.0lf.\n", max, min);    }    return 0;}


原创粉丝点击