uva 10700 - Camel trading

点击打开链接

题目意思:     给定一个表达式,要求找到这个表达式的最大值和最小值

解题思路:     1:思路:模拟题
                      2:对于给定的一个表达式,最小值就是直接去计算这个表达式。如果要求算出的值最大,那么我们知道乘号的个数是不会改变的,所以如果能够让乘号旁边的数字越大越好,所 以我们把所有+旁边的数全部加为一个数,然后在计算就是最大值,例如3+11+4*1*13*12*8+3*3+8应该就是要(3+11+4)*(1)*(13)*(12)*(8+3)*(3+8)
                      3:由于这里的数字是1-20,并且最多有12个,那么最坏情况20*20.........*20,20个连乘那么结果超过int,所以用long long才保证正确,这个地方WA了N次,后来含着泪A了
                      4:这一题用到了两个常用于处理字符串,1 split函数用来切割字符串 2 sscanf用来读取字符串中的数字.见我博客《ACM---资料收集》有详细的介绍

代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
#define MAXN 100

int t;
long long  s[MAXN];
char ch[50];

void solve(){
    long long  min , max;//longlong
    long long i , j , tmp , a;
    char str[MAXN][100] , tmp_ch[MAXN];
    memset(s , 0 , sizeof(s));

    //求最小值
    memcpy(tmp_ch , ch , sizeof(tmp_ch));
    const char *split = "+" ; char *p;
    p = strtok(tmp_ch , split) ; i = 0;
    while(p != NULL){
        strcpy(str[i++] , p);
        p = strtok(NULL,split);
    }
    for(j = 0 , min = 0; j < i ; j++){
        split = "*" ; tmp = 1;
        p = strtok(str[j] , split);
        while(p != NULL){
            sscanf(p , "%lld" , &a) ; tmp *= a;
            p = strtok(NULL, split);
        }
        min += tmp;
    }

    //求最大值
    memcpy(tmp_ch , ch , sizeof(tmp_ch));
    memset(s , 0 , sizeof(s));
    split = "*" ;
    p = strtok(tmp_ch,split) ; i = 0;
    while(p != NULL){
        strcpy(str[i++] , p) ; p = strtok(NULL,split);
    }
    for(j = 0 ; j < i ; j++){
        split = "+" ; tmp = 0;
        p = strtok(str[j] , split);
        while(p != NULL){
            sscanf(p , "%lld" , &a) ; tmp += a;
            p = strtok(NULL, split);
        }
        s[j] = tmp;
    }
    for(i = 0 , max = 1 ; s[i] != 0 ; i++) max *= s[i];
    printf("The maximum and minimum are %lld and %lld.\n" , max , min);
}

int main(){
    //freopen("input.txt" , "r" , stdin);
    scanf("%d%*c" , &t);
    while(t--){
         gets(ch) ; solve();
    }
    return 0;
}
时间: 2024-09-20 06:25:05

uva 10700 - Camel trading的相关文章

UVa 10700 Camel trading:计算表达式

10700 - Camel trading Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1641 Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formul

UVa 10700:Camel trading

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1641 原题: Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin in the

uva 11054 - Wine trading in Gergovia

点击打开链接uva 11054 题目意思:    有一个城市,城市里的每一个人都在做酒生意,有的人是要买进用正数表示,有的人是要卖出用负数表示.现在规定这个城市的每家每户都连在一起,还有L升酒的交易代价为"两家的距离xL",要求我们找到最小的交易代价 解题思路:     1:贪心                        2:由于每一个人要买的或要卖的物品是一定的,那么我们知道如果要让每一个人的产生最小的交易代价就是让每一个人都和他相邻的人交易,那么这样就有最优解,所以只要从左向右

UVa 11054:Wine trading in Gergovia

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1995 [原题] As you may know from the comic "Asterix and the Chieftain's Shield", Gergovia consists of one street, and

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.