보관함

JUNGOL 기초다지기 01 출력

niamdank 2019. 11. 11. 22:44

프로그래밍에서 가장 기초적인 내용을 다루는 기초 다지기입니다.

이번 포스팅은 기초 다지기 중 첫 번째인 출력 문제를 풀어보도록 하겠습니다.

 

기초 내용인 만큼 다른 설명없이 문제와 코드만 간단하게 작성하도록 하겠습니다.

기본적으로 다음과 같이 출력하라는 지시외에 다른 제약사항이 없으므로 단순히 동일하게 출력하는 것을 목표로 하겠습니다.

 


501 : 출력 - 자가진단1

#include <iostream>

using namespace std;

int main(void)
{
	cout << "Fun Programming!" << endl;
}

 

502 : 출력 - 자가진단2

#include <iostream>

using namespace std;

int main(void)
{
	cout << "Programming! It's fun." << endl;
}

 

503 : 출력 - 자가진단3

#include <iostream>

using namespace std;

int main(void)
{
	cout << "My name is Hong Gil Dong." << endl
		 << "I am 13 years old." << endl;
}

 

504 : 출력 - 자가진단4

#include <iostream>

using namespace std;

int main(void)
{
	cout << "(@) (@)" << endl
		 << "(=^.^=)" << endl
		 << "(-m-m-)" << endl;
}

 

505 : 출력 - 자가진단5

#include <iostream>

using namespace std;

int main(void)
{
	cout << "I can program well." << endl
		 << "Dreams come true." << endl;
}

 

506 : 출력 - 자가진단6

#include <iostream>

using namespace std;

int main(void)
{
	cout << "My height" << endl
		 << "170" << endl
		 << "My weight" << endl
		 << "68.600000" << endl;
}

 

507 : 출력 - 자가진단7

#include <iostream>

using namespace std;

int main(void)
{
	cout << "5 Dan" << endl
		 << "5 * 2 = 10" << endl;
}

 

508 : 출력 - 자가진단8

 

이 문제는 의도가 명확히 드러나 있으므로 두 가지 방법으로 코딩해보겠습니다.

 

1. cout.setf, cout.width 사용

#include <iostream>

using namespace std;

int main(void)
{
	cout.setf(ios::right);	// 우측 정렬

	// 설명 시작
	cout.width(10);
	cout << "item";

	cout.width(10);
	cout << "count";

	cout.width(10);
	cout << "price" << endl;

	// pen 시작
	cout.width(10);
	cout << "pen";

	cout.width(10);
	cout << 20;

	cout.width(10);
	cout << 100 << endl;

	// note 시작
	cout.width(10);
	cout << "note";

	cout.width(10);
	cout << 5;

	cout.width(10);
	cout << 95 << endl;

	// eraser 시작
	cout.width(10);
	cout << "eraser";

	cout.width(10);
	cout << 110;

	cout.width(10);
	cout << 97 << endl;
}

 

2. <iomanip>의 setw 사용

#include <iostream>
#include <iomanip>

using namespace std;

int main(void)
{
	cout.setf(ios::right);	// 우측 정렬

	cout << setw(10) << "item" << setw(10) << "count" << setw(10) << "price" << endl;
	cout << setw(10) << "pen"  << setw(10) << 20      << setw(10) << 100     << endl;
	cout << setw(10) << "note" << setw(10) << 5       << setw(10) << 95      << endl;
	cout << setw(10) << "eraser" << setw(10) << 110     << setw(10) << 97      << endl;
}

 

101 : 출력 - 형성평가1

#include <iostream>

using namespace std;

int main(void)
{
	cout << "My name is Hong" << endl;
}

 

102 : 출력 - 형성평가2

#include <iostream>

using namespace std;

int main(void)
{
	cout << "My hometown\nFlowering mountain" << endl;
}

 

103 : 출력 - 형성평가3

#include <iostream>

using namespace std;

int main(void)
{
	cout << "TTTTTTTTTT" << endl
		 << "TTTTTTTTTT" << endl
		 << "    TT" << endl
		 << "    TT" << endl
		 << "    TT" << endl;
}

 

104 : 출력 - 형성평가4

 

이번 문제에도 조건이 명시되었네요. 합계와 평균을 수식으로 구해서 출력하는 예제입니다.

#include <iostream>

using namespace std;

int main(void)
{
	int kor = 90;
	int mat = 80;
	int eng = 100;
	int sum = kor + mat + eng;
	int avg = sum / 3;

	cout << "kor " << kor << endl;
	cout << "mat " << mat << endl;
	cout << "eng " << eng << endl;
	cout << "sum " << sum << endl;
	cout << "avg " << avg << endl;
}

 

105 : 출력 - 형성평가5

 

이번에는 setw를 이용한 코드만 올리겠습니다. 다른 방법은 자가진단8을 참고하세요.

#include <iostream>
#include <iomanip>

using namespace std;

int main(void)
{
	cout.setf(ios::right);	// 우측 정렬

	cout << setw(15) << "Seoul" << setw(15) << "10,312,545"
    				<< setw(15) << "+91,375" << endl;
	cout << setw(15) << "Pusan" << setw(15) << "3,567,910"
    				<< setw(15) << "+5,868" << endl;
	cout << setw(15) << "Incheon" << setw(15) << "2,758,296"
    				<< setw(15) << "+64,888" << endl;
	cout << setw(15) << "Daegu" << setw(15) << "2,511,676"
    				<< setw(15) << "+17,230" << endl;
	cout << setw(15) << "Gwangju" << setw(15) << "1,454,636"
    				<< setw(15) << "+29,774" << endl;
}

 

http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&sca=1010

 

JUNGOL | 문제은행 1 페이지

 

www.jungol.co.kr