1. 도형 그리기 ¶
~cpp * ***** * ***** ** **** ** **** *** *** *** *** **** ** **** ** ***** * ***** *
- 왜 다 어려운거야.ㅡㅜ - 일정
- - 승균
2.1. 따로따로 버젼 ¶
~cpp #include <iostream.h> void main() { int num; cout << "숫자를 입력하시오 - "; cin >> num; cout << "\n"; cout << "삼각형 # 1" << "\n\n"; for (int i = 1; i < num + 1; i++) { for (int j = 0; j < i; j++) cout << "*"; for (int a = num + 1; a > i; a--) cout << " "; cout << "\n"; } cout << "\n"; cout << "삼각형 # 2" << "\n\n"; for (int k = 1; k < num + 1; k++) { for (int l = num + 1; l > k; l--) cout << "*"; for (int b = 0; b < k; b++) cout << " "; cout << "\n"; } cout << "\n"; cout << "삼각형 # 3" << "\n\n"; for (int m = 1; m < num + 1; m++) { for (int n = num; n > m; n--) cout << " "; for (int o = 0; o < m; o++) cout << "*"; cout << "\n"; } cout << "\n"; cout << "삼각형 # 4" << "\n\n"; for (int p = 1; p < num + 1; p++) { for (int q = 1; q < p; q++) cout << " "; for (int r = num + 1; r > p; r--) cout << "*"; cout << "\n"; } }
2.2. 일렬 버젼 ¶
~cpp #include <iostream.h> void main() { int num; cout << "숫자를 입력하시오 - (단, 20을 넘어가면 삼각형의 형태가 대략 좇치 않소)"; cin >> num; cout << "\n"; cout << "삼각형 싸그라 다" << "\n\n"; for (int i = 1; i < num + 1; i++) { for (int j = 0; j < i; j++) cout << "*"; for (int a = num + 1; a > i; a--) cout << " "; for (int l = num + 1; l > i; l--) cout << "*"; for (int b = 0; b < i; b++) cout << " "; for (int n = num; n > i; n--) cout << " "; for (int o = 0; o < i; o++) cout << "*"; for (int q = 0; q < i; q++) cout << " "; for (int r = num + 1; r > i; r--) cout << "*"; cout << "\n"; } }
3. 황일정 ¶
~cpp #include<iostream> using namespace std; int main() { cout<<"도형그리기"<<endl<<endl; for(int i=0;i<5;i++) { for(int j=0;j<i+1;j++) cout<<"*"; for(j=0;j<5-i;j++) cout<<" "; for(j=0;j<5-i;j++) cout<<"*"; for(j=1;j<=i;j++) cout<<" "; for(j=0;j<5-i;j++) cout<<" "; for(j=0;j<i+1;j++) cout<<"*"; for(j=0;j<i+1;j++) cout<<" "; for(j=0;j<5-i;j++) cout<<"*"; cout<<endl; } return 0; }
3.1. 숫자입력버젼(승균이가 했길래;나도~^^) ¶
~cpp #include<iostream> using namespace std; int main() { cout<<"도형그리기"<<endl; cout<<"숫자 입력하세요:"; int a; cin>>a; for(int i=0;i<a;i++) { for(int j=0;j<i+1;j++) cout<<"*"; for(j=0;j<a-i;j++) cout<<" "; for(j=0;j<a-i;j++) cout<<"*"; for(j=1;j<=i;j++) cout<<" "; for(j=0;j<a-i;j++) cout<<" "; for(j=0;j<i+1;j++) cout<<"*"; for(j=0;j<i+1;j++) cout<<" "; for(j=0;j<a-i;j++) cout<<"*"; cout<<endl; } return 0; }
- 이야 깔끔하다~ 변수를 하나로 통일하는 방법도 있었군 - 승균
4. 1002 ¶
~cpp #include <iostream> using namespace std; #define BOARDSIZE_X 40 #define BOARDSIZE_Y 5 bool bitmap[BOARDSIZE_X][BOARDSIZE_Y]; void drawTriangle(int startX, int startY, int size, int deltaX, int deltaY) { for (int i=0;i<size;i++) { for (int j=0;j<=i;j++) bitmap[startX + j*deltaX][startY + i*deltaY] = true; } } void init() { memset(bitmap, 0, sizeof(bitmap)); } void printBoard() { for (int i=0;i<BOARDSIZE_Y;i++) { for (int j=0;j<BOARDSIZE_X;j++) cout << ((bitmap[j][i]) ? "*" : " "); cout << endl; } } void main() { init(); drawTriangle(0,0,5, 1, 1); drawTriangle(5,4,5, 1,-1); drawTriangle(15,0,5,-1, 1); drawTriangle(20,4,5,-1,-1); printBoard(); }
5. 임인택 ¶
~cpp for i in range(1,6): as = i ds = 6-i print '*'*as,' '*ds,'*'*ds,' '*5,'*'*as,' '*as,'*'*ds
- 하핫 파이썬의 힘이네요 - 민수
6. 류주영 ¶
~cpp #include <iostream> using namespace std; void showmenu(); int main() { showmenu(); int choice; cin >> choice; while(choice != 6) { switch(choice) { case 1 : { for(int i=1;i<=5;i++) { for(int j=1;j<=i;j++) cout << "*"; cout << "\n"; } } break; case 2 : { for(int i=5;i>=1;i--) { for(int j=1;j<=i;j++) cout << "*"; cout << "\n"; } } break; case 3 : { for(int i=5;i>=1;i--) { for(int j=1;j<i;j++) cout << " "; for(int k=1;k<=6-i;k++) cout << "*"; cout << "\n"; } } break; case 4 : { for(int i=1;i<=5;i++) { for(int j=1;j<i;j++) cout << " "; for(int k=1;k<=6-i;k++) cout << "*"; cout << "\n"; } } break; case 5 : { for(int i=1;i<=5;i++) { for(int j=1;j<=i;j++) cout << "*"; for(int k=1;k<=6-i;k++) cout << " "; for(int l=1;l<=6-i;l++) cout << "*"; cout << " "; for(int m=1;m<=i;m++) cout << "*"; for(int n=1;n<=i;n++) cout << " "; for(int o=1;o<=6-i;o++) cout << "*"; cout << "\n"; } } break; default : cout << "그것은 선택할 수 없어요.\n"; } showmenu(); cin >> choice; } cout << "종료해요.\n"; return 0; } void showmenu() { cout << "1~6번중에서 하나를 선택하세요.(6번은종료,숫자만입력할것) : "; }
위에 선배님들이 해주신게 너무 짧아서 부끄러운-_-;;
- 그래도 웬지 소스가 길면 뽀대 나잖아~ -_-;;;;;; - 승균
7. Python Vs C/C++ 모두 같은 생각의 소스 ¶
- 같은 생각으로 작성한다면 소스가 어떻게 표현될까 궁금했다. 소스양만 따지면, Python 을 위한 문제인가.
- 포멧팅 이용, index 두개
7.1. Python ¶
Guido van Rossum 이라면 이거에 찬성할것 같다.
~cpp num = input("plz input number of asterisk: ") format = ( '%%%ds '% (num) ) * 4 for odd,even in zip(range(1,num+1), range(num,0,-1)): print format%( ('*'* odd,'*'*even) * 2 )
7.2. C ¶
Kernighan과 Ritchie가 이걸 찬성할까? 모르겠다.
~cpp #include <stdio.h> #define MAX 21 int main(){ char odd[MAX],even[MAX],format[20]; int i,j, num; printf("plz input asterisk number(below %d):",MAX); scanf("%d",&num); if( num > MAX ) exit(0); sprintf(format,"%%%ds %%%ds %%%ds %%%ds\n",num,num,num,num); memset(odd,0,MAX); for(i=1,j=num;i<=num;i++,j--){ memset(odd, '*', i); memset(even,'*', j); even[j]=0; printf(format,odd,even,odd,even); } return 0; }
7.3. C++ ¶
Bjarne Stroustrup ,Alexander Stepanov는 어떻게 작성하길 원할까?
~cpp #include <iostream> #include <string> using namespace std; inline void print(int& num,string& str){ cout.width(num+1); cout<<str; } int main(){ int num; cout << "plz number of asterisk:"; cin >> num; string asterisks; asterisks.assign(num, '*'); for (int i=1, j=num;i<=num;i++,j--){ string odd = asterisks.substr(0,i); string even = asterisks.substr(0,j); print(num,odd); print(num,even); print(num,odd); print(num,even); cout << endl; } return 0; }이게 조금더 C++ 다운걸까?
~cpp #include <iostream> #include <string> using namespace std; int main(){ int num; cout << "plz number of asterisk:"; cin >> num; string odd, odd_s, even, even_s; string oddf,evenf; for (int i=1, j=num;i<=num;i++,j--){ oddf = odd.assign(i,'*') + odd_s.assign(j,' '); evenf = even.assign(j,'*')+ even_s.assign(i,' '); cout << oddf << evenf << endl; } return 0; }
8. zennith ¶
~cpp #include <iostream> char buffer[] = "* ***** * *****"; const char star = '*'; const char space = ' '; int star_ptr1 = 1; int star_ptr2 = 14; int space_ptr1 = 10; int space_ptr2 = 17; void printBuffer(void) { std::cout << buffer << std::endl; } int main(void) { int i = 5; do { printBuffer(); buffer[star_ptr1++] = buffer[star_ptr2--] = star; buffer[space_ptr1--] = buffer[space_ptr2++] = space; } while(--i); return 0; }