zoukankan      html  css  js  c++  java
  • Simple Expression

    Description

    You probably know that Alex is a very serious mathematician and he likes to solve serious problems. This is another problem from Alex.
    You are given three nonnegative integers abc. You have to arrange them in some order and put +, − or × signs between them to minimize the outcome of the resulting expression. You are not allowed to use unary minus and parentheses in the expression. There must be exactly one sign between every pair of neighbouring numbers. You should use standard order for performing operations (multiplication first, addition and subtraction then).

    Input

    There are three lines with one integer in each line. The numbers are arranged in non-decreasing order (0 ≤ a ≤ b ≤ c ≤ 100).

    Output

    Print one number — the minimal outcome.

    Sample Input

    inputoutput
    1
    2
    3
    
    -5

    解题思路:三个数之间构成一个数学式,中间只能有两个运算符,既然要求数学式的最小值当然不能出现加法运算符了,实际上就有两种情况罢了,第二个运算符是减号还是乘号,取最小值。

    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int i,sum1,sum2;
        int a[3];
        for(i=0;i<3;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+3);
        sum1=a[0]-a[1]-a[2];
        sum2=a[0]-a[1]*a[2];
        printf("%d
    ",min(sum1,sum2));
        return 0;
    }
  • 相关阅读:
    贪心:SPOJ Backup Files
    杂题 SPOJ MOBILE2
    杂题 UVAoj 10000 Longest Paths
    杂题 UVAoj 107 The Cat in the Hat
    DP(斜率优化):HDU 3507 Print Article
    搜索(DLX): POJ 3074 3076 Sudoku
    DLX模板
    PHP代码优化技巧大盘点
    盘点PHP编程常见失误
    PHP Socket 编程详解
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9032133.html
Copyright © 2011-2022 走看看