USACO贪婪的礼物送给Java扫描仪错误



现在,我正在研究USACO问题贪婪的礼物。这是问题语句:

一组NP(2≤np≤10)独特地命名的朋友已决定交换金钱的礼物。这些朋友中的每一个都可能不会给其他任何或所有其他朋友。同样,每个朋友可能也可能不会从任何其他朋友那里收到钱。您在这个问题中的目标是推断每个人付出的钱比他们收到的钱更多。

送礼规则可能与您预期的不同。每个人都搁置一定数量的钱,将这笔钱均匀地分配给他或她送给礼物的人。没有分数的钱,所以将3个在2个朋友中划分为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)

我在做什么错?

这是因为您需要输入和输出gift1.ingift1.out而不是gift1.in.txtgift1.out.txt

最新更新