U E D R , A S I H C RSS

프로그래밍/Digit Generator


걸린 시간 : 20분
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class DigitGenerator {
	private static int processOneCase(String line) {
		int number = Integer.parseInt(line);
		
		for(int i = 1; i < number; i++) {
			int bitSum = 0;
			String str = Integer.toString(i);
			String [] bits = str.split("");
			for(String each : bits) {
				if (each.matches("")) {
					continue;
				}
				bitSum += Integer.parseInt(each);
			}
			if (i + bitSum == number) {
				return i;
			}
		}
		return 0;
	}

	public static void main(String[] args) {
		try {
			BufferedReader br = new BufferedReader(new FileReader("test.txt"));
			String line = br.readLine();
			int testCase = Integer.parseInt(line);
			for(int i = 0; i < testCase; i++) {
				line = br.readLine();
				int result = processOneCase(line);
				System.out.println(result);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


(zeropage)코드레이스출동 연습

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:25
Processing time 0.0134 sec