USACO贪婪礼物给Java扫描程序错误

时间:2016-12-23 16:37:56

标签: java

现在我正在研究USACO问题贪婪礼品赠送者。这是问题陈述:

一群NP(2≤NP≤10)独特的朋友决定交换金钱的礼物。这些朋友中的每一个可能会或可能不会向任何或所有其他朋友提供一些钱。同样,每个朋友可能会或可能不会从任何或所有其他朋友那里收钱。你在这个问题上的目标是推断每个人给予的收入比收到的多多少。

赠送礼物的规则可能与您预期的不同。每个人都留出一定数量的钱给予并将这笔钱平均分配给他或她送给他们的所有人。没有可用的小数部分资金,因此将2个朋友中的3分别为剩下1个的朋友分别为1个 - 剩下的1个留在给予者"帐户"。

在任何一群朋友中,有些人比其他人更有奉献(或者至少可能有更多的熟人),有些人比其他人有更多的钱。

鉴于一群朋友,其中没有一个人的姓名超过14个字符,该组中每个人花在礼物上的钱,以及每个人送给他们的礼物的(子)列表,决定了多少组中每个人给予的比他们收到的更多(或更少)。

这是我用java编写的代码:

import java.io.*;
import java.util.*;

class gift1{
    public static void main(String[] args) throws IOException{
        //scan np
        Scanner sc = new Scanner("gift1.int.txt");
        int np = sc.nextInt();

        //scan friend names => String Array/Maps
        Map<String, Integer> initial = new HashMap<String, Integer>();
        Map<String, Integer> total = new HashMap<String, Integer>();
        String[] people = new String[np - 1];
        int counter = 0;
        for (int i = 0; i < np; i++){
            String d = sc.next();
            initial.put(d, 0);
            total.put(d, 0);
            people[counter] = d;
            counter ++;
        }

        //scan person into HashMap with initial money (initial amount of money)
        for (int k = 0; k < np; k++){
            String a = sc.next();
            int b = sc.nextInt();
            int c = sc.nextInt();
            initial.put(a, b);
            if (c != 0){
                total.put(a, b + total.get(a) - (b/c));
                for (int j = 0; j < c; j++){
                    String person = sc.next();
                    total.put(person, total.get(person) + (b/c));
                }
            }
        }

        sc.close();

        //print output
        PrintWriter pw = new PrintWriter("gift.out.txt");
        for (int p = 0; p < np; p++){
            pw.println(people[p] + " " + (total.get(people[p]) - initial.get(people[p])));
        }

        pw.close();

    }
}

请注意,我已经创建了名为gift1.in.txt和gift1.out.txt的文件,因此不存在问题。当我尝试运行此代码时,它给出了以下错误:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at gift1.main(gift1.java:8)

我做错了什么?

0 个答案:

没有答案