1330: Five Cards

来源:互联网 发布:没有sqlserver服务 编辑:程序博客网 时间:2024/06/07 01:40

题目

Description

马克在玩一个游戏,他有5张卡片,每张卡片上会有一个数字。每个数字是一个正整数
马克可以丢弃一些卡片。他的目标是得到剩余卡片数字的最小和
允许马克丢弃两张或三张卡片(只能丢弃一次),并且丢弃的卡片的数字必须是相同的。当然,如果其中无相同数字的卡片,就不能丢弃卡片
给你5张卡片,你能找到在剩下卡片中的最小和吗?
Input

多组输入,每行输入包括5个数字t1,t2,t3,t4,和t5(1<=ti<=100),卡片上的的数字
Output

每组输出,输出剩下卡片上数字的最小和
Sample Input

7 3 7 3 20
7 9 3 1 8
10 10 10 10 10
Sample Output

26
28
20


代码块

import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner cn = new Scanner(System.in);        while (cn.hasNext()) {            int[] a = new int[5];            int sum = 0;            for (int i = 0; i < 5; i++) {                a[i] = cn.nextInt();                sum += a[i];            }            int max = 0;            for (int i = 0; i < 4; i++) {                if (a[i] == 0)                    continue;                int count = 0;                for (int j = i + 1; j < 5; j++) {                    if (a[j] == 0)                        continue;                    if (a[i] == a[j]) {                        count++;                        a[j] = 0;                    }                }                if (count > 0 && count < 3) {                    int z = (count + 1) * a[i];                    if (z > max)                        max = z;                } else if (count >= 3) {                    if (max < 3 * a[i])                        max = 3 * a[i];                }            }            System.out.println(sum - max);        }    }}