U E D R , A S I H C RSS

Primary Arithmetic/sun

풀이 λ°©ν–₯

λ‹¨μˆœνžˆ μ‚¬λžŒμ΄ λ§μ…ˆμ„ ν•˜λŠ” μ‹μœΌλ‘œ ν’€μ—ˆλ‹€. 끝자리 λΆ€ν„° ν•˜λ‚˜μ”© 더해가며, μ˜¬λ¦Όμˆ˜κ°€ λ°œμƒν–ˆμ„ 경우 같이 λ”ν•˜λŠ” 방식을 μ΄μš©ν–ˆλ‹€.

~cpp 
    1234
   + 567
   ----
    1081

μœ„μ—μ„œμ²˜λŸΌ, κ·Έλ™μ•ˆ 더 ν°μˆ˜λΌ μœ„μ— 놓고 λ”ν•˜λŠ”λ°μ— μ΅μˆ™ν•΄μ Έ μžˆκΈ°μ— 같은 방식을 λ”°λžλ‹€. (κ²½ν—˜μƒ, μ–΄λ–€ νŠΉλ³„ν•œ μ•Œκ³ λ¦¬μ¦˜μ„ λ”°λ₯΄λŠ”κ²Œ μ•„λ‹ˆλΌλ©΄, ν˜„μ‹€ μ„Έκ³„λΌ λͺ¨λΈλ§ν•˜λŠ”κ²Œ νŽΈν•œκ²ƒ κ°™λ‹€.)

숫자 생성기

풀이 λ°©ν–₯을 μ •ν•˜κ³  λ³΄λ‹ˆ, μˆ«μžλΌ ν•˜λ‚˜μ”© λμ—μ„œλΆ€ν„° λ–Όμ–΄μ˜¬ ν•„μš”κ°€ 생겼닀.

ν…ŒμŠ€νŠΈ μž‘μ„± (NumberGeneratorTest.java)

~cpp 
package primaryarithmetic;

import junit.framework.TestCase;

public class NumberGeneratorTest extends TestCase {

	public void testNoNumber() {
		NumberGenerator ng = new NumberGenerator();
		assertFalse( ng.hasNext() );
	}
	
	public void test123() {
		NumberGenerator ng = new NumberGenerator(123);
		assertEquals( 3, ng.next() );
		assertEquals( 2, ng.next() );
		assertTrue( ng.hasNext() );
		assertEquals( 1, ng.next() );
		assertFalse( ng.hasNext() );
	}
}

μ§€κΈˆ 생각해보면 ~cpp testNoNumberλŠ” ν•„μš”μ—†λŠ”κ²ƒ κ°™λ‹€. λ‚˜μ€‘μ— 글을 μ“°λ‹€λ³΄λ‹ˆ, 같이 μ“°κ²Œ λ¬λŠ”λ° μ›λž˜λŠ” μœ„μ˜ ν…ŒμŠ€νŠΈλΌ λ¨Όμ € μž‘μ„±ν•˜κ³  ν…ŒμŠ€νŠΈ 톡과후 μ•„λž˜μͺ½ ν…ŒμŠ€νŠΈλΌ μΆ”κ°€ν–ˆλ‹€. 이번 μž‘μ—…κ³Ό λ³„λ„λ‘œ 코딩후에 λ­”κ°€ν•˜μžλŠ” κ²°κ΅­ λ†“μΉ˜λŠ”κ²Œ λ§Žλ‹€λŠ”κ±Έ λ‹€μ‹œν•œλ²ˆ 증λͺ…ν•˜κ²Œ λœλ‹€. :) see NowOrNever

μœ„ ν…ŒμŠ€νŠΈλΌ λ§Œμ±ν•˜λŠ” μ½”λ“œ μž‘μ„± (NumberGenerator.java)

~cpp 
package primaryarithmetic;

public class NumberGenerator {

	private int number;
	private byte[] numbers;
	private int numPointer;

	public NumberGenerator() {
		number = -1;
	}

	public NumberGenerator( int number ) {
		if( number < 0 ) throw new ArithmeticException( "0 λ˜λŠ” κ·Έ μ΄μƒμ˜ μ •μˆ˜λ§Œ κ°€λŠ₯ν•©λ‹ˆλ‹€: " + number );
		this.number = number;
		init();
	}

	private void init() {
		numbers = String.valueOf(number).getBytes();
		numPointer = numbers.length - 1;
	}

	public boolean hasNext() {
		if( number == -1 ) return false;
		return numPointer >= 0;
	}

	public int next() {
		return numbers[numPointer--] - '0';
	}
}

PrimaryArithmetic

μ—¬κΈ°μ„œλ„ λ§ˆμ°¬κ°€μ§€λ‘œ ν…ŒμŠ€νŠΈλΌ μž‘μ„±ν•˜κ²Œ λ¬λŠ”λ°, μ½”λ“œλΌ μΆ”κ°€ν•˜λ‹€λ³΄λ‹ˆ λΉ„μŠ·ν•œ νŒ¨ν„΄μœΌλ‘œ ν…ŒμŠ€νŠΈλΌ ν•˜κ²Œ λ˜μ„œ, ν…ŒμŠ€νŠΈ μ…‹ μΆ”κ°€λ‘œ 이λ„지도둝 ν…ŒμŠ€νŠΈλΌ κ΅¬μ„±

ν…ŒμŠ€νŠΈ μž‘μ„± (PrimaryArithmeticTest.java)

~cpp 
package primaryarithmetic;

import junit.framework.TestCase;

public class PrimaryArithmeticTest extends TestCase {

	public void testCases() {
		int [][] sets = {
				{ 0, 1, 1 },
				{ 1, 5, 5 },
				{ 3, 555, 555 },
				{ 0, 123, 456 },
				{ 1, 123, 594 }
		};
		
		for( int i=0; i<sets.length; i++ ) {
			int expected = sets[i][0];
			int num1 = sets[i][1];
			int num2 = sets[i][2];
			
			verify( expected, num1, num2 );
		}
	}

	private void verify(int expected, int num1, int num2) {
		int result = PrimaryArithmetic.add(num1, num2);
		try {
			assertEquals( expected, result );
		}
		catch( Throwable e ) {
			throw new ArithmeticException( "We expected " + expected + ", but was " + result + " (num1=" + num1 + ", num2=" + num2 + ")" );
		}
	}
}

(μ—­μ‹œλ‚˜, ν•˜λŠ” 쀑간에 λ¬Έμ„œλΌ μž‘μ„±ν•˜μ§€ μ•ŠμœΌλ‹ˆ μ»€λ‹€λž€ 동기가 μ—†λŠ”ν•œ μžμ„Ένžˆ 쓰지 μ•Šκ²Œ λœλ‹€. 흑흑)

ν…ŒμŠ€νŠΈλΌ λ§Œμ±ν•˜λŠ” μ½”λ“œ μž‘μ„± (PrimaryArithmetic.java)

~cpp 
package primaryarithmetic;

public class PrimaryArithmetic {

	public static int add( int num1, int num2 ) {
		NumberGenerator ng1 = new NumberGenerator( Math.max(num1, num2) );
		NumberGenerator ng2 = new NumberGenerator( Math.min(num1, num2) );
		
		int counts = 0;
		int sumUp = 0;
		
		while( ng1.hasNext() && ng2.hasNext() ) {
			int n1 = ng1.next();
			int n2 = ng2.next();
			int sum = n1 + n2 + sumUp;
			
			if( sum >= 10 ) {
				sumUp = 1;
				counts ++;
			}
			else sumUp = 0;
		}
		
		while( sumUp == 1 && ng1.hasNext() ) {
			int sum = ng1.next() + sumUp;
			if( sum >= 10 ) counts ++;
			else break;
		}
		
		return counts;
	}
}

μ‘μš©ν”„λ‘œκ·Έλž¨μœΌλ‘œ

μ–Έμ–΄ νŠΉμ„±μƒ λΌμΈμˆ˜κ°€ κΈΈμ–΄μ§€λŠ”κ±Έ 느끼며 (PrimaryArithmeticApp.java)

~cpp 
package primaryarithmetic;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PrimaryArithmeticApp {
	
	public static void main( String [] args ) throws IOException {
		BufferedReader in = new BufferedReader( new InputStreamReader( System.in ));
		String line;
		
		while( (line=in.readLine()) != null ) {
			String [] numbers = line.split( " " );
			int num1 = Integer.parseInt( numbers[0] );
			int num2 = Integer.parseInt( numbers[1] );
			if( num1 == 0 && num2 == 0 ) break;
			
			int counts = PrimaryArithmetic.add( num1, num2 );
			print( counts );
		}
	}

	private static void print( int counts ) {
		String occurs = (counts == 0) ? "No" : String.valueOf(counts);
		String postfix = (counts > 1) ? "s" : "";
		System.out.println( occurs + " carry operation" + postfix + "." );
	}
}

뢀둝 (μ¨ν”„λΌμ΄μ¦ˆ 버전)

문제 ν’€μ΄λΌ μœ„ν•΄μ„œ 별닀λ₯Έ μ•Œκ³ λ¦¬μ¦˜μ΄ μ•„λ‹Œ ν˜„μ‹€μ—μ„œμ˜ λ§μ…ˆ 방법을 μ‚¬μš©ν–ˆκ³ , μ½”λ“œλŸ‰μ΄ λ³„λ‘œ λ˜μ§€ μ•ŠμŒμ—λ„ 1μ‹œκ°„μ΄ 걸린것은 도쀑에 msn으둜 친ꡬ과 μ±„νŒ…μ„ ν•˜λ©° ν–ˆκΈ° λ•Œλ¬Έμ΄λ‹€. 본인이 λŠλΌκΈ°μ—λŠ” msn을 μ–Όλ§ˆ ν•˜μ§€ μ•Šμ•˜λ˜κ²ƒ 같은데 μ‹€μ œλ‘œλŠ” μ‹œκ°„μ΄ κ½€ μ§€λ‚˜μžˆλŠ”κ±Έ 보면 μ•„μΈμŠˆνƒ€μΈμ˜ μƒλŒ€μ„±μ›λ¦¬μ— 따라 μ‹œκ°„μ΄ 흐름을 μ•Œ 수 μžˆλ‹€. 직μž₯인의 경우 이것을 μ€ λ” μΌλ°˜ν™” 해보면, '왠지 였늘 ν•˜λ£¨ μΌν•˜κΈ° μ‹«λ‹€'라고 λŠκ»΄μ§€κ³ , νœ΄κ°€λΌ μ“Έ 수 없을땐 근무쀑에 λ©”μ‹ μ €λΌ ν•˜λ©΄ μ‹œκ°„μ΄ 금방 감을 μ•Œ 수 μžˆκ² λ‹€.

단, κΈ°μˆ μ—λŠ” μ–Έμ œλ‚˜ μ–΄λ‘μš΄ 면이 있λ“이, μ‹œκ°„μ„ μ—„μ²­λ‚˜κ²Œ 빨리 λ³΄λ‚΄λŠ” 도쀑 μƒμ‚¬μ™€μ˜ μ›μΉ˜μ•ŠλŠ” λ©”μ‹œμ§•μ€ λ°˜λŒ€μ˜ νš¨κ³ΌλΌ λ‚˜νƒ€λ‚Όμˆ˜λ„ μžˆλ‹€. (즉, μ΄λŸ΄λ• 'μ˜€ν”„λΌμΈμœΌλ‘œ λ³΄μž„'을 μ΄μš©ν•˜λ„λ‘ ν•˜μž)

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:24:02
Processing time 0.0257 sec