• UVA 10700 Camel trading


     1 #include <iostream>
     2 #include <cstdio>
     3 #include <stack>
     4 #define sc(x)    scanf("%d",&x)
     5 #define sc1(x)    scanf("%lld",&x)
     6 #define pf(x)    printf("%d
    ",x)
     7 #define FOR(i,b,e)    for(int i=b;i<N;i++)
     8 using namespace std;
     9 stack <long long>Max;
    10 stack <long long>Min;
    11 int main()
    12 {
    13     int N;
    14     sc(N);
    15     while(N--)
    16     {
    17         while(!Max.empty())
    18         Max.pop();
    19         while(!Min.empty())
    20         Min.pop();
    21         long long a, t, ans_max = 1, ans_min = 0;
    22         sc1(a);
    23         char ch;
    24         Min.push(a);
    25         Max.push(a);
    26         while((ch = getchar()) != '
    ')
    27        {
    28                sc1(a);
    29                if(ch == '+'){
    30                Min.push(a);
    31                t = Max.top();
    32                Max.pop();
    33                t += a;
    34                Max.push(t);
    35             }
    36             else if(ch=='*'){
    37                Max.push(a);
    38                t = Min.top();
    39                Min.pop();
    40                t *= a;
    41                Min.push(t);               
    42             }
    43        }
    44        while(!Min.empty())
    45        {
    46                ans_min += Min.top();
    47                Min.pop();
    48        }
    49        while(!Max.empty())
    50        {
    51                ans_max *= Max.top();
    52                Max.pop();
    53        }
    54         printf("The maximum and minimum are %lld and %lld.
    ",ans_max,ans_min);
    55     }
    56     return 0;
    57 }
    View Code

    这个是数学思路,需要用栈解决,同时输入字符是getchar();

    通过一个一个字符输入,避免了字符串的输入,那时还要考虑转换成数组问题。

  • 相关阅读:
    281. Zigzag Iterator
    298. Binary Tree Longest Consecutive Sequence
    482. License Key Formatting
    361. Bomb Enemy
    373. Find K Pairs with Smallest Sums
    304. Range Sum Query 2D
    308. Range Sum Query 2D
    307. Range Sum Query
    303. Range Sum Query
    247. Segment Tree Query II
  • 原文地址:https://www.cnblogs.com/ghostTao/p/4392667.html
Copyright © 2020-2023  润新知