U E D R , A S I H C RSS

Erdos Numbers/황재선

2005.4.30

소스

~cpp 
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
~cpp 
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 버전 받으니 되네. 땡쓰~ :) -- 재선

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:13
Processing time 0.0101 sec