인터넷에서 우연히 보게된 코드이다.
보통 구구단을 출력하는 프로그램을 작성하라고 하면 이중 for문을 사용해서 합니다.
for i=1 ~ 9 까지
for j=1~9까지
출력 i x j = i*j
대충 이런 식이죠.
이는 for문 하나만을 사용해서 출력하는 방법입니다
인터넷에서 본 건 C 소스지만 공부 겸해서 Objective-C 로 작성해봤습니다.
//
// main.m
// prg1
//
// Created by 최규성 on 12. 11. 14..
// Copyright (c) 2012년 규성 최. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Programming is fun!");
int iCounter;
int iDan,iMul;
for (iCounter=0; iCounter<9*9; ++iCounter) {
iDan = iCounter/9+1;
iMul = iCounter%9+1;
NSLog(@"%d x %d = %d",iDan,iMul,iDan*iMul);
}
}
return 0;
}
>> 결과:
2012-11-15 00:08:35.686 prg1[4163:403] Programming is fun!
2012-11-15 00:08:35.688 prg1[4163:403] 1 x 1 = 1
2012-11-15 00:08:35.690 prg1[4163:403] 1 x 2 = 2
2012-11-15 00:08:35.691 prg1[4163:403] 1 x 3 = 3
2012-11-15 00:08:35.692 prg1[4163:403] 1 x 4 = 4
2012-11-15 00:08:35.693 prg1[4163:403] 1 x 5 = 5
2012-11-15 00:08:35.694 prg1[4163:403] 1 x 6 = 6
2012-11-15 00:08:35.694 prg1[4163:403] 1 x 7 = 7
2012-11-15 00:08:35.695 prg1[4163:403] 1 x 8 = 8
2012-11-15 00:08:35.695 prg1[4163:403] 1 x 9 = 9
2012-11-15 00:08:35.696 prg1[4163:403] 2 x 1 = 2
2012-11-15 00:08:35.697 prg1[4163:403] 2 x 2 = 4
2012-11-15 00:08:35.697 prg1[4163:403] 2 x 3 = 6
2012-11-15 00:08:35.698 prg1[4163:403] 2 x 4 = 8
2012-11-15 00:08:35.698 prg1[4163:403] 2 x 5 = 10
2012-11-15 00:08:35.699 prg1[4163:403] 2 x 6 = 12
2012-11-15 00:08:35.699 prg1[4163:403] 2 x 7 = 14
2012-11-15 00:08:35.700 prg1[4163:403] 2 x 8 = 16
2012-11-15 00:08:35.700 prg1[4163:403] 2 x 9 = 18
2012-11-15 00:08:35.701 prg1[4163:403] 3 x 1 = 3
2012-11-15 00:08:35.701 prg1[4163:403] 3 x 2 = 6
2012-11-15 00:08:35.702 prg1[4163:403] 3 x 3 = 9
2012-11-15 00:08:35.702 prg1[4163:403] 3 x 4 = 12
2012-11-15 00:08:35.703 prg1[4163:403] 3 x 5 = 15
2012-11-15 00:08:35.704 prg1[4163:403] 3 x 6 = 18
2012-11-15 00:08:35.704 prg1[4163:403] 3 x 7 = 21
2012-11-15 00:08:35.705 prg1[4163:403] 3 x 8 = 24
2012-11-15 00:08:35.716 prg1[4163:403] 3 x 9 = 27
2012-11-15 00:08:35.716 prg1[4163:403] 4 x 1 = 4
2012-11-15 00:08:35.717 prg1[4163:403] 4 x 2 = 8
2012-11-15 00:08:35.717 prg1[4163:403] 4 x 3 = 12
2012-11-15 00:08:35.718 prg1[4163:403] 4 x 4 = 16
2012-11-15 00:08:35.718 prg1[4163:403] 4 x 5 = 20
2012-11-15 00:08:35.719 prg1[4163:403] 4 x 6 = 24
2012-11-15 00:08:35.719 prg1[4163:403] 4 x 7 = 28
2012-11-15 00:08:35.720 prg1[4163:403] 4 x 8 = 32
2012-11-15 00:08:35.720 prg1[4163:403] 4 x 9 = 36
2012-11-15 00:08:35.721 prg1[4163:403] 5 x 1 = 5
2012-11-15 00:08:35.722 prg1[4163:403] 5 x 2 = 10
2012-11-15 00:08:35.722 prg1[4163:403] 5 x 3 = 15
2012-11-15 00:08:35.724 prg1[4163:403] 5 x 4 = 20
2012-11-15 00:08:35.725 prg1[4163:403] 5 x 5 = 25
2012-11-15 00:08:35.726 prg1[4163:403] 5 x 6 = 30
2012-11-15 00:08:35.726 prg1[4163:403] 5 x 7 = 35
2012-11-15 00:08:35.727 prg1[4163:403] 5 x 8 = 40
2012-11-15 00:08:35.727 prg1[4163:403] 5 x 9 = 45
2012-11-15 00:08:35.728 prg1[4163:403] 6 x 1 = 6
2012-11-15 00:08:35.728 prg1[4163:403] 6 x 2 = 12
2012-11-15 00:08:35.730 prg1[4163:403] 6 x 3 = 18
2012-11-15 00:08:35.731 prg1[4163:403] 6 x 4 = 24
2012-11-15 00:08:35.732 prg1[4163:403] 6 x 5 = 30
2012-11-15 00:08:35.732 prg1[4163:403] 6 x 6 = 36
2012-11-15 00:08:35.733 prg1[4163:403] 6 x 7 = 42
2012-11-15 00:08:35.733 prg1[4163:403] 6 x 8 = 48
2012-11-15 00:08:35.736 prg1[4163:403] 6 x 9 = 54
2012-11-15 00:08:35.736 prg1[4163:403] 7 x 1 = 7
2012-11-15 00:08:35.737 prg1[4163:403] 7 x 2 = 14
2012-11-15 00:08:35.737 prg1[4163:403] 7 x 3 = 21
2012-11-15 00:08:35.738 prg1[4163:403] 7 x 4 = 28
2012-11-15 00:08:35.739 prg1[4163:403] 7 x 5 = 35
2012-11-15 00:08:35.740 prg1[4163:403] 7 x 6 = 42
2012-11-15 00:08:35.740 prg1[4163:403] 7 x 7 = 49
2012-11-15 00:08:35.741 prg1[4163:403] 7 x 8 = 56
2012-11-15 00:08:35.742 prg1[4163:403] 7 x 9 = 63
2012-11-15 00:08:35.742 prg1[4163:403] 8 x 1 = 8
2012-11-15 00:08:35.743 prg1[4163:403] 8 x 2 = 16
2012-11-15 00:08:35.744 prg1[4163:403] 8 x 3 = 24
2012-11-15 00:08:35.744 prg1[4163:403] 8 x 4 = 32
2012-11-15 00:08:35.745 prg1[4163:403] 8 x 5 = 40
2012-11-15 00:08:35.746 prg1[4163:403] 8 x 6 = 48
2012-11-15 00:08:35.746 prg1[4163:403] 8 x 7 = 56
2012-11-15 00:08:35.747 prg1[4163:403] 8 x 8 = 64
2012-11-15 00:08:35.747 prg1[4163:403] 8 x 9 = 72
2012-11-15 00:08:35.748 prg1[4163:403] 9 x 1 = 9
2012-11-15 00:08:35.748 prg1[4163:403] 9 x 2 = 18
2012-11-15 00:08:35.749 prg1[4163:403] 9 x 3 = 27
2012-11-15 00:08:35.749 prg1[4163:403] 9 x 4 = 36
2012-11-15 00:08:35.750 prg1[4163:403] 9 x 5 = 45
2012-11-15 00:08:35.750 prg1[4163:403] 9 x 6 = 54
2012-11-15 00:08:35.751 prg1[4163:403] 9 x 7 = 63
2012-11-15 00:08:35.751 prg1[4163:403] 9 x 8 = 72
2012-11-15 00:08:35.780 prg1[4163:403] 9 x 9 = 81
'Programming > Swift' 카테고리의 다른 글
[Objective-C] 03. 클래스, 객체, 메서드 (0) | 2012.11.14 |
---|---|
관련 정보 사이트 정리 (0) | 2012.08.16 |
윈도우에서 익히는 Object-C : 맥부기의 맨날할래님 (2) | 2011.01.04 |
댓글