耗时一天,终于把USACO搞明白了。从怎么写到怎么传。英语不行害死人呐。明天继续加油!上传喜果!
Greedy Gift Givers
A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to any or all of the other friends. Likewise, each friend might or might not receive money from any or all of the other friends. Your goal in this problem is to deduce how much more money each person gives than they receive.
The rules for gift-giving are potentially different than you might expect. Each person sets aside a certain amount of money to give and divides this money evenly among all those to whom he or she is giving a gift. No fractional money is available, so dividing 3 among 2 friends would be 1 each for the friends with 1 left over -- that 1 left over stays in the giver's "account".
In any group of friends, some people are more giving than others (or at least may have more acquaintances) and some people have more money than others.
Given a group of friends, no one of whom has a name longer than 14 characters, the money each person in the group spends on gifts, and a (sub)list of friends to whom each person gives gifts, determine how much more (or less) each person in the group gives than they receive.
IMPORTANT NOTE
The grader machine is a Linux machine that uses standard Unix conventions: end of line is a single character often known as '\n'. This differs from Windows, which ends lines with two charcters, '\n' and '\r'. Do not let your program get trapped by this!
PROGRAM NAME: gift1
INPUT FORMAT
| Line 1: | The single integer, NP | |||
| Lines 2..NP+1: | Each line contains the name of a group member | |||
| Lines NP+2..end: | NP groups of lines organized like this:
|
SAMPLE INPUT (file gift1.in)
5 dave laura owen vick amr dave 200 3 laura owen vick owen 500 1 dave amr 150 2 vick owen laura 0 2 amr vick vick 0 0
OUTPUT FORMAT
The output is NP lines, each with the name of a person followed by a single blank followed by the net gain or loss (final_money_value - initial_money_value) for that person. The names should be printed in the same order they appear on line 2 of the input.
All gifts are integers. Each person gives the same integer amount of money to each friend to whom any money is given, and gives as much as possible that meets this constraint. Any money not given is kept by the giver.
SAMPLE OUTPUT (file gift1.out)
dave 302 laura 66 owen -359 vick 141 amr -150
My Program:
/*
ID: silenwa1
LANG: JAVA
TASK: gift1
PROG:gift1
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
class gift1 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Scanner sr = new Scanner(new FileReader("gift1.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("gift1.out")));
int PeopleCt = sr.nextInt(); //people's count
String[] name = new String[PeopleCt];
for(int i=0; i<PeopleCt; i++){
name[i] = sr.next(); //read names
}
int[] balance = new int[PeopleCt];
int count = 0;
for(int i=0; i<PeopleCt; i++){ //loop from the first people, totally five people
count++;
String giver = sr.next(); //read the giver
int money = sr.nextInt(); //read the money
int num = sr.nextInt(); //read the number
// String[] receiver = new String[num];
if(num != 0){
int divmoney = money/num;
for(int j=0; j<PeopleCt; j++){
//reduce the amount of giver
if(name[j].equals(giver)){
balance[j] -= divmoney*num;
break;
}
}
String[] receiver = new String[num];
for(int j=0; j<num; j++){
receiver[j] = sr.next();
}
for(int k=0; k<num; k++){
for(int j=0; j<PeopleCt; j++){
if(receiver[k].equals(name[j])){
balance[j] += divmoney; //increase the amount of receiver
}
}
}
}
}
for(int i=0; i<PeopleCt; i++){
out.println(name[i]+" "+balance[i]);
}
out.close(); // close the output file
System.exit(0); // don't omit this!
// TODO Auto-generated method stub
}
}
总结:1 永远不要和主循环用一用的循环变量
2 String 是一个类
3 对文件的读写 有了新的体会
ex: Scanner sr = new Scanner(new FileReader("gift1.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("gift1.out")));
Scanner sr = new Scanner(new File("D:\\JavaTest\\20130718\\src\\test.txt"));
4 最后无非是要细心再细心。写好伪码以后再开始程序。网上也有一些相关的c++的程序可以参考顺便学习了c++。
5 今天也对debug有了新的体会和练习。找错归功于它!
8万+

被折叠的 条评论
为什么被折叠?



