More actions
imported>Unknown No edit summary |
(Repair batch-0002 pages from live compare) |
||
| Line 19: | Line 19: | ||
} | } | ||
private String | private String[] extractNames(String line) { | ||
String | String[] divide = line.split(":"); | ||
String | String[] peopleName = divide[0].split(","); | ||
for (int i = 0; i < peopleName.length; i++) | for (int i = 0; i < peopleName.length; i++) | ||
peopleName | peopleName[i] = peopleName[i].trim(); | ||
String | String[] people = new String[peopleName.length/2]; | ||
for (int i = 0; i < peopleName.length; i+=2) { | for (int i = 0; i < peopleName.length; i+=2) { | ||
people | people[i/2] = peopleName[i] + ", " + peopleName[i+1]; | ||
} | } | ||
return people; | return people; | ||
} | } | ||
private void setErdosNumber(String | private void setErdosNumber(String[] people) { | ||
for(String name : people) { | for(String name : people) { | ||
if (name.compareTo("Erdos, P.") == 0) { | if (name.compareTo("Erdos, P.") == 0) { | ||
| Line 55: | Line 55: | ||
} | } | ||
private void withErdos(String | private void withErdos(String[] people) { | ||
for(String name : people) { | for(String name : people) { | ||
if (name.compareTo("Erdos, P.") != 0) | if (name.compareTo("Erdos, P.") != 0) | ||
| Line 62: | Line 62: | ||
} | } | ||
private void withCoWorker(String | private void withCoWorker(String[] people) { | ||
for(String name : people) { | for(String name : people) { | ||
if (!isInclude(name)) { | if (!isInclude(name)) { | ||
| Line 70: | Line 70: | ||
} | } | ||
private void noAssociate(String | private void noAssociate(String[] people) { | ||
for(String name : people) | for(String name : people) | ||
tm.put(name, "infinity"); | tm.put(name, "infinity"); | ||
| Line 84: | Line 84: | ||
private void printErdosNumber(int scenario, int name) { | private void printErdosNumber(int scenario, int name) { | ||
String | String [] names = new String[name]; | ||
for(int i = 0; i < name; i++) { | for(int i = 0; i < name; i++) { | ||
names | names[i] = this.readLine(); | ||
} | } | ||
System.out.println("Scenario " + ++scenario); | System.out.println("Scenario " + ++scenario); | ||
| Line 93: | Line 93: | ||
} | } | ||
} | } | ||
public static void main(String | public static void main(String[] args) { | ||
ErdosNumbers erdos = new ErdosNumbers(); | ErdosNumbers erdos = new ErdosNumbers(); | ||
int scenario = Integer.parseInt(erdos.readLine()); | int scenario = Integer.parseInt(erdos.readLine()); | ||
for(int testCase = 0; testCase < scenario; testCase++) { | for(int testCase = 0; testCase < scenario; testCase++) { | ||
String | String [] nums = erdos.readLine().split(" "); | ||
int paper = Integer.parseInt(nums | int paper = Integer.parseInt(nums[0]); | ||
int name = Integer.parseInt(nums | int name = Integer.parseInt(nums[1]); | ||
for(int p = 0; p < paper; p++) { | for(int p = 0; p < paper; p++) { | ||
String | String[] people = erdos.extractNames(erdos.readLine()); | ||
erdos.setErdosNumber(people); | erdos.setErdosNumber(people); | ||
} | } | ||
| Line 126: | Line 126: | ||
String line = "Smith, M.N., Martin, G., Erdos, P.: " + | String line = "Smith, M.N., Martin, G., Erdos, P.: " + | ||
"Newtonian forms of prime factor matrices"; | "Newtonian forms of prime factor matrices"; | ||
String | String[] divide = line.split(":"); | ||
assertEquals("Smith, M.N., Martin, G., Erdos, P.", divide | assertEquals("Smith, M.N., Martin, G., Erdos, P.", divide[0]); | ||
String | String[] person = divide[0].split(","); | ||
assertEquals(6, person.length); | assertEquals(6, person.length); | ||
for (int i = 0; i < person.length; i++) | for (int i = 0; i < person.length; i++) | ||
person | person[i] = person[i].trim(); | ||
for (int i = 0; i < person.length; i+=2) { | for (int i = 0; i < person.length; i+=2) { | ||
person | person[i/2] = person[i] + ", " + person[i+1]; | ||
} | } | ||
assertEquals("Smith, M.N.", person | assertEquals("Smith, M.N.", person[0]); | ||
assertEquals("Martin, G.", person | assertEquals("Martin, G.", person[1]); | ||
assertEquals("Erdos, P.", person | assertEquals("Erdos, P.", person[2]); | ||
} | } | ||
public void testMap() { | public void testMap() { | ||
TreeMap<String, String> tm = new TreeMap<String, String>(); | TreeMap<String, String> tm = new TreeMap<String, String>(); | ||
String | String[] person = {"Smith, M.N.", "Martin, G.", "Erdos, P."}; | ||
for(int i = 0; i < person.length; i++) | for(int i = 0; i < person.length; i++) | ||
tm.put(person | tm.put(person[i], "0"); | ||
assertEquals("0", tm.get(person | assertEquals("0", tm.get(person[0])); | ||
tm.put(person | tm.put(person[0], "1"); | ||
assertEquals("1", tm.get(person | assertEquals("1", tm.get(person[0])); | ||
assertEquals(true, tm.containsKey("Smith, M.N.")); | assertEquals(true, tm.containsKey("Smith, M.N.")); | ||
} | } | ||
| Line 161: | Line 161: | ||
---- | ---- | ||
ErdosNumbers | ErdosNumbers | ||
Revision as of 00:16, 27 March 2026
2005.4.30
소스
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeMap;
public class ErdosNumbers {
private TreeMap<String, String> tm;
private ArrayList<String> nameList;
ErdosNumbers() {
tm = new TreeMap<String, String>();
}
public String readLine() {
Scanner sc = new Scanner(System.in).useDelimiter("\n");
return sc.next().trim();
}
private String[] extractNames(String line) {
String[] divide = line.split(":");
String[] peopleName = divide[0].split(",");
for (int i = 0; i < peopleName.length; i++)
peopleName[i] = peopleName[i].trim();
String[] people = new String[peopleName.length/2];
for (int i = 0; i < peopleName.length; i+=2) {
people[i/2] = peopleName[i] + ", " + peopleName[i+1];
}
return people;
}
private void setErdosNumber(String[] people) {
for(String name : people) {
if (name.compareTo("Erdos, P.") == 0) {
withErdos(people);
return;
}
}
boolean joint = false;
nameList = new ArrayList<String>();
for(String name : people) {
if (tm.containsKey(name)) {
joint = true;
nameList.add(name);
}
}
if (joint) withCoWorker(people);
else noAssociate(people);
nameList.clear();
}
private void withErdos(String[] people) {
for(String name : people) {
if (name.compareTo("Erdos, P.") != 0)
tm.put(name, "1");
}
}
private void withCoWorker(String[] people) {
for(String name : people) {
if (!isInclude(name)) {
tm.put(name, "2");
}
}
}
private void noAssociate(String[] people) {
for(String name : people)
tm.put(name, "infinity");
}
private boolean isInclude(String person) {
for(int i = 0; i < nameList.size(); i++) {
if (person.compareTo(nameList.get(i)) == 0)
return true;
}
return false;
}
private void printErdosNumber(int scenario, int name) {
String [] names = new String[name];
for(int i = 0; i < name; i++) {
names[i] = this.readLine();
}
System.out.println("Scenario " + ++scenario);
for(String eachName : names) {
System.out.println(eachName + " " + tm.get(eachName));
}
}
public static void main(String[] args) {
ErdosNumbers erdos = new ErdosNumbers();
int scenario = Integer.parseInt(erdos.readLine());
for(int testCase = 0; testCase < scenario; testCase++) {
String [] nums = erdos.readLine().split(" ");
int paper = Integer.parseInt(nums[0]);
int name = Integer.parseInt(nums[1]);
for(int p = 0; p < paper; p++) {
String[] people = erdos.extractNames(erdos.readLine());
erdos.setErdosNumber(people);
}
erdos.printErdosNumber(testCase, name);
erdos.tm.clear();
}
}
}
Test Code
import java.util.TreeMap;
import junit.framework.TestCase;
public class TestErdosNumbers extends TestCase {
public void testInput() {
ErdosNumbers en = new ErdosNumbers();
//assertEquals("", en.readLine());
}
public void testPersonName() {
String line = "Smith, M.N., Martin, G., Erdos, P.: " +
"Newtonian forms of prime factor matrices";
String[] divide = line.split(":");
assertEquals("Smith, M.N., Martin, G., Erdos, P.", divide[0]);
String[] person = divide[0].split(",");
assertEquals(6, person.length);
for (int i = 0; i < person.length; i++)
person[i] = person[i].trim();
for (int i = 0; i < person.length; i+=2) {
person[i/2] = person[i] + ", " + person[i+1];
}
assertEquals("Smith, M.N.", person[0]);
assertEquals("Martin, G.", person[1]);
assertEquals("Erdos, P.", person[2]);
}
public void testMap() {
TreeMap<String, String> tm = new TreeMap<String, String>();
String[] person = {"Smith, M.N.", "Martin, G.", "Erdos, P."};
for(int i = 0; i < person.length; i++)
tm.put(person[i], "0");
assertEquals("0", tm.get(person[0]));
tm.put(person[0], "1");
assertEquals("1", tm.get(person[0]));
assertEquals(true, tm.containsKey("Smith, M.N."));
}
}
쓰레드
- 자바 1.5의 새로운 기능을 조금 사용해보았다. 클래스 Scanner는 이전 방식으로 하는 것보다 훨씬 편한 기능을 제공해 주었다. for loop에서 신기하게 배열을 참조하는 방식이 Eclipse에서 에러로 인식된다.
이클립스 최신버전을 사용하면 되던데?! -문보창 latest stable 버전 받으니 되네. 땡쓰~ :) -- 재선
ErdosNumbers