😎 공부하는 징징알파카는 처음이지?
[inflearn 강의] 기본 문법 - 예제 문제 (다양하게 별 찍기, 구구단) 본문
728x90
반응형
<본 블로그는 어소트락 게임아카데미 님의 유튜브를 참고해서 공부하며 작성하였습니다 :-)>
=> C++ Let's Make Games
🫧 별 찍기
// Chapter1_9
#include <iostream>
using namespace std;
int main()
{
// 별찍기
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i + 1; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
🫧 거꾸로 별 찍기
// Chapter1_9
#include <iostream>
using namespace std;
int main()
{
// 거꾸로 별찍기
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
🫧 거꾸로 별 찍기
// Chapter1_9
#include <iostream>
using namespace std;
int main()
{
// 삼각형 별 찍기
// 공백 별
//3 1
//2 3
//1 5
//0 7
for (int i = 0; i < 4; i++) {
// 공백을 위한 문
for (int j = 0; j < 3 - i; j++) {
cout << " ";
}
// *을 위한 문
for (int j = 0; j < i * 2 + 1; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
🫧 2단부터 9단 구구단 하기
// Chapter1_9
#include <iostream>
using namespace std;
int main()
{
// 2단부터 9단까지 구구단
for (int i = 2; i < 10; i++) {
cout << i << "구구단" << endl;
for (int j = 1; j < 10; j++) {
cout << i << " * " << j << " = " << (i * j) << endl;
}
}
return 0;
}
🫧 다이아몬드로 별 찍기
// Chapter1_9
#include <iostream>
using namespace std;
int main()
{
// 다이아몬드로 별 찍기
int iLine = 7;
int iCnt = 0;
for (int i = 0; i < iLine; i++) {
// 공백 : 3 2 1 0 1 2 3
// 별 : 1 3 5 7 5 3 1
iCnt = i;
// i값이 4, 5, 6 일때만 들어감
if (i > iLine / 2) {
iCnt = iLine - 1 - i;
}
// i값이 0, 1, 2, 3 일때는 그대로 iCnt 대입
// i값이 4, 5, 6 일때는 iCnt 2, 1, 0 대입
// 즉, i는 0, 1, 2, 3, 2, 1, 0
for (int j = 0; j < 3 - iCnt; j++) {
cout << " ";
}
for (int j = 0; j < iCnt * 2 + 1; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
728x90
반응형
'👩💻 IoT (Embedded) > C++' 카테고리의 다른 글
[inflearn 강의] 기본 문법 - 배열 (0) | 2023.06.29 |
---|---|
[inflearn 강의] 기본 문법 - do while (0) | 2023.06.29 |
[inflearn 강의] 기본 문법 - for문과 중첩for문 (0) | 2023.06.27 |
[inflearn 강의] 기본 문법 - switch문과 열거체 (0) | 2023.06.27 |
[inflearn 강의] 기본 문법 - 난수와 확률 & if문의 활용 (0) | 2023.06.27 |
Comments