//C++로 시작하는 객체지향프로그래밍 p.218 예제 5.40 - (시뮬레이션: 앞면 또는 뒷면) 동전을 백만 번 던지는 것을 시뮬레이션하고 앞면과 뒷면의 수를 출력하는 프로그램을 작성하여라.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
long x;
int count1 =0, count2=0;
srand(time(0));
for (int i = 0; i < 1000000; i++){
x = rand()%2 + 1;
if (x == 1)
count1++;
else if (x==2)
count2++;
}
cout << "앞면의 수는" << count1 << endl;
cout << "뒷면의 수는" << count2 << endl;
system("pause");
return 0;
}
6.30.2017
합계 찾기
//C++로 시작하는 객체지향프로그래밍 p.218 예제 5.37 - 합계 - 다음 합계를 계산하는 프로그램을 작성하여라.
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double sum = 0.0;
for (int i = 1; i <625; i++){
sum += 1 / (sqrt(1.0*i)+sqrt(1.0*(i+1)));
}
cout << "합계는 " << sum << "이다." <<endl;
system("pause");
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double sum = 0.0;
for (int i = 1; i <625; i++){
sum += 1 / (sqrt(1.0*i)+sqrt(1.0*(i+1)));
}
cout << "합계는 " << sum << "이다." <<endl;
system("pause");
return 0;
}
윤년표시
//C++로 시작하는 객체지향프로그래밍 p.217 예제 5.29 - 윤년표시
#include <iostream>
using namespace std;
int main() {
int number;
for (number = 2001; number < 2100;number++){
if (number%400 == 0 || number%4 ==0 && number%100 != 0) {
cout <<number << " " ;
if (number % 10==0)
cout << "\n";
}
}
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main() {
int number;
for (number = 2001; number < 2100;number++){
if (number%400 == 0 || number%4 ==0 && number%100 != 0) {
cout <<number << " " ;
if (number % 10==0)
cout << "\n";
}
}
system("pause");
return 0;
}
정수의 소인수 찾기
//C++로 시작하는 객체지향프로그래밍 p.213 예제 5.18 - 정수의 소인수 찾기
#include <iostream>
using namespace std;
int main() {
int number;
cout << " 수 입력하기" << endl;
cin >> number;
cout << "The factors for " << number << " is " ;
int factor = 2;
while (factor <=number)
{
if (number %factor == 0 )
{ number = number /factor;
cout << factor << " " ;
}
else
factor++;
}
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main() {
int number;
cout << " 수 입력하기" << endl;
cin >> number;
cout << "The factors for " << number << " is " ;
int factor = 2;
while (factor <=number)
{
if (number %factor == 0 )
{ number = number /factor;
cout << factor << " " ;
}
else
factor++;
}
system("pause");
return 0;
}
5와 6으로 나누어지는 수 찾기
//C++로 시작하는 객체지향프로그래밍 p.213 예제 5.12 - 5와 6으로 나누어지는 수 찾기
#include <iostream>
using namespace std;
int main(){
int i, count = 0;
for (int i = 100; i <1000; i++) {
if ( i%5 ==0 && i%6==0)
{ count++;
cout << i << " " ;
if (count %10 ==0)
cout << "\n";
}
}
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main(){
int i, count = 0;
for (int i = 100; i <1000; i++) {
if ( i%5 ==0 && i%6==0)
{ count++;
cout << i << " " ;
if (count %10 ==0)
cout << "\n";
}
}
system("pause");
return 0;
}
가위바위보 게임 출력하기
//C++로 시작하는 객체지향 프로그래밍 p. 218 5.36 가위바위보 게임 출력하기 (교수님 답안)
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main(){
int limit, me, computer, meWin =0, comWin = 0;
string cStr;
cout << "게임을 할 횟수를 정해주세요." << endl;
cin >> limit;
srand(time(0));
computer = rand()%3 + 1;
switch(computer) {
case 1:
cStr = "가위";
break;
case 2:
cStr = "바위";
break;
case 3:
cStr = "보";
break;
}
cout << "당신의 선택은 : " ;
cin >> me;
if (me == 1) {
switch(computer) {
case 1 :
cout << "same" << endl;
break;
case 2 :
cout << "computer win" << endl;
break;
case 3 :
cout << "you win" << endl;
break;
}
}
else if (me == 2) {
switch(computer) {
case 1 :
cout << "you win" << endl;
break;
case 2 :
cout << "same" << endl;
break;
case 3 :
cout << "computer win" << endl;
break;
}
}
if (me == 3) {
switch(computer) {
case 1 :
cout << "computer win" << endl;
break;
case 2 :
cout << "you win" << endl;
break;
case 3 :
cout << "same" << endl;
break;
}
}
system("pause");
return 0;
}
=====================================================
//가위바위보 게임 출력하기 - 교수님 답안2
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main(){
int limit, me, computer, meWin =0, comWin = 0;
string cStr, meStr;
cout << "게임을 할 횟수를 정해주세요." << endl;
cin >> limit;
srand(time(0));
computer = rand()%3 + 1;
for (int i = 0; i < limit ; i++){
switch(computer) {
case 1:
cStr = "가위";
break;
case 2:
cStr = "바위";
break;
case 3:
cStr = "보";
break;
}
cout << "당신의 선택은 : " ;
cin >> meStr;
if (meStr == "가위") {
switch(computer) {
case 1 :
cout << "same" << endl;
break;
case 2 :
cout << "computer win" << endl;
break;
case 3 :
cout << "you win" << endl;
break;
}
}
else if (meStr == "바위") {
switch(computer) {
case 1 :
cout << "you win" << endl;
break;
case 2 :
cout << "same" << endl;
break;
case 3 :
cout << "computer win" << endl;
break;
}
}
if (meStr == "보") {
switch(computer) {
case 1 :
cout << "computer win" << endl;
break;
case 2 :
cout << "you win" << endl;
break;
case 3 :
cout << "same" << endl;
break;
}
}
}
system("pause");
return 0;
}
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main(){
int limit, me, computer, meWin =0, comWin = 0;
string cStr;
cout << "게임을 할 횟수를 정해주세요." << endl;
cin >> limit;
srand(time(0));
computer = rand()%3 + 1;
switch(computer) {
case 1:
cStr = "가위";
break;
case 2:
cStr = "바위";
break;
case 3:
cStr = "보";
break;
}
cout << "당신의 선택은 : " ;
cin >> me;
if (me == 1) {
switch(computer) {
case 1 :
cout << "same" << endl;
break;
case 2 :
cout << "computer win" << endl;
break;
case 3 :
cout << "you win" << endl;
break;
}
}
else if (me == 2) {
switch(computer) {
case 1 :
cout << "you win" << endl;
break;
case 2 :
cout << "same" << endl;
break;
case 3 :
cout << "computer win" << endl;
break;
}
}
if (me == 3) {
switch(computer) {
case 1 :
cout << "computer win" << endl;
break;
case 2 :
cout << "you win" << endl;
break;
case 3 :
cout << "same" << endl;
break;
}
}
system("pause");
return 0;
}
=====================================================
//가위바위보 게임 출력하기 - 교수님 답안2
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main(){
int limit, me, computer, meWin =0, comWin = 0;
string cStr, meStr;
cout << "게임을 할 횟수를 정해주세요." << endl;
cin >> limit;
srand(time(0));
computer = rand()%3 + 1;
for (int i = 0; i < limit ; i++){
switch(computer) {
case 1:
cStr = "가위";
break;
case 2:
cStr = "바위";
break;
case 3:
cStr = "보";
break;
}
cout << "당신의 선택은 : " ;
cin >> meStr;
if (meStr == "가위") {
switch(computer) {
case 1 :
cout << "same" << endl;
break;
case 2 :
cout << "computer win" << endl;
break;
case 3 :
cout << "you win" << endl;
break;
}
}
else if (meStr == "바위") {
switch(computer) {
case 1 :
cout << "you win" << endl;
break;
case 2 :
cout << "same" << endl;
break;
case 3 :
cout << "computer win" << endl;
break;
}
}
if (meStr == "보") {
switch(computer) {
case 1 :
cout << "computer win" << endl;
break;
case 2 :
cout << "you win" << endl;
break;
case 3 :
cout << "same" << endl;
break;
}
}
}
system("pause");
return 0;
}
0~100사이의 숫자맞추기 게임
//0과 100 사이에 있는 숫자를 맞추는 게임
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
int number, guess, x=0;
srand(time(0));
number = rand()%100;
do {
cout << "0에서 100 사이의 숫자를 입력해주세요." << endl;
cin >> guess;
if(number == guess)
{
cout << "수와 입력하신 값이 일치합니다." << endl;
x++;
cout << "시도한 횟수는 " << x << "입니다. " << endl;
}
else if (number > guess)
{
cout << "수가 입력하신 값보다 큽니다." << endl;
x++;
}
else if (number < guess)
{ cout << "수가 입력하신 값보다 작습니다." << endl;
x++;
}
} while (number !=guess);
system("pause");
return 0;
}
======================================================
//교수님 답안 - 0과 100 사이에 있는 숫자를 맞추는 게임
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
int answer, guess, tryCnt=0;
srand(time(0));
answer = rand()%101;
while(1) {
tryCnt++;
if(guess == answer)
{
cout << "축하합니다... 정답이예요." << endl;
break;
}
else if (guess > answer)
{
cout << "더 작은 수를 말해봐" << endl;
}
else if (guess < answer)
{ cout << "더 큰 수를 말해 봐." << endl;
}
cout << "Enter a Number : ";
cin >> guess;
}
cout << "시도한 횟수 " << tryCnt << "입니다. " << endl;
system("pause");
return 0;
}
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
int number, guess, x=0;
srand(time(0));
number = rand()%100;
do {
cout << "0에서 100 사이의 숫자를 입력해주세요." << endl;
cin >> guess;
if(number == guess)
{
cout << "수와 입력하신 값이 일치합니다." << endl;
x++;
cout << "시도한 횟수는 " << x << "입니다. " << endl;
}
else if (number > guess)
{
cout << "수가 입력하신 값보다 큽니다." << endl;
x++;
}
else if (number < guess)
{ cout << "수가 입력하신 값보다 작습니다." << endl;
x++;
}
} while (number !=guess);
system("pause");
return 0;
}
======================================================
//교수님 답안 - 0과 100 사이에 있는 숫자를 맞추는 게임
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
int answer, guess, tryCnt=0;
srand(time(0));
answer = rand()%101;
while(1) {
tryCnt++;
if(guess == answer)
{
cout << "축하합니다... 정답이예요." << endl;
break;
}
else if (guess > answer)
{
cout << "더 작은 수를 말해봐" << endl;
}
else if (guess < answer)
{ cout << "더 큰 수를 말해 봐." << endl;
}
cout << "Enter a Number : ";
cin >> guess;
}
cout << "시도한 횟수 " << tryCnt << "입니다. " << endl;
system("pause");
return 0;
}
6.29.2017
금융문제 : 미래의 등록금 계산
//C++로 시작하는 객체지향 프로그래밍 p.213 예제 5.9 - 미래의 등록금 계산하기
#include <iostream>
using namespace std;
int main() {
int x = 10000;
int sum = 0;
for (int i = 0; i < 10; i++)
{
x = x * 1.05;
}
cout << "10년 후의 등록금은 $" << x << "이다." << endl;
for ( int j = 0 ; j < 4; j++)
{
sum += x;
x = x*1.05;
}
cout << "등록금 총액은 $" << sum << "입니다." << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main() {
int x = 10000;
int sum = 0;
for (int i = 0; i < 10; i++)
{
x = x * 1.05;
}
cout << "10년 후의 등록금은 $" << x << "이다." << endl;
for ( int j = 0 ; j < 4; j++)
{
sum += x;
x = x*1.05;
}
cout << "등록금 총액은 $" << sum << "입니다." << endl;
system("pause");
return 0;
}
최고점 학생 출력하기
//C++로 시작하는 객체지향 프로그래밍 p.213 예제 5.10 - 최고점 학생 출력하기
#include <iostream>
#include <string>
using namespace std;
int main() {
int studentCnt, score, max =-999;
string name, maxName;
cout << "학생 수를 입력하시오." << endl;
cin >> studentCnt;
for (int i = 0; i < studentCnt; i++){
cout << "학생 이름과 점수를 입력하시오." << endl;
cin >> name >> score;
if (score > max) {
max = score;
maxName = name;
}
}
cout << maxName << max << endl;
system("pause");
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
int studentCnt, score, max =-999;
string name, maxName;
cout << "학생 수를 입력하시오." << endl;
cin >> studentCnt;
for (int i = 0; i < studentCnt; i++){
cout << "학생 이름과 점수를 입력하시오." << endl;
cin >> name >> score;
if (score > max) {
max = score;
maxName = name;
}
}
cout << maxName << max << endl;
system("pause");
return 0;
}
6.28.2017
생일 맞추기
#include <iostream>
using namespace std;
int main(){
int day = 0;
char answer;
cout << "When is your birthday?" <<endl;
cout << " <CARD 1>\n 1 3 5 7\n 9 11 13 15\n17 19 21 23\n25 27 29 31\n" << endl;
cout << "Enter N/n for No and Y/y for Yes: ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
day += 1;
// Prompt the user for Set 2
cout << " <CARD 2>\n 2 3 6 7\n10 11 14 15\n18 19 22 23\n26 27 30 31\n" << endl;
cout << "Enter N/n for No and Y/y for Yes: ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
day += 2;
// Prompt the user for Set 3
cout << " <CARD 3>\n 4 5 6 7\n12 13 14 15\n20 21 22 23\n28 29 30 31\n" << endl;
cout << "Enter N/n for No and Y/y for Yes: ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
day += 4;
// Prompt the user for Set 4
cout << " <CARD 4>\n 8 9 10 11\n12 13 14 15\n24 25 26 27\n28 29 30 31\n" << endl;
cout << "Enter N/n for No and Y/y for Yes: ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
day += 8;
// Prompt the user for Set 5
cout << " <CARD 5>\n16 17 18 19\n20 21 22 23\n24 25 26 27\n28 29 30 31\n" << endl;
cout << "Enter N/n for No and Y/y for Yes: ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
day += 16;
cout << day;
system("pause");
return 0;
}
using namespace std;
int main(){
int day = 0;
char answer;
cout << "When is your birthday?" <<endl;
cout << " <CARD 1>\n 1 3 5 7\n 9 11 13 15\n17 19 21 23\n25 27 29 31\n" << endl;
cout << "Enter N/n for No and Y/y for Yes: ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
day += 1;
// Prompt the user for Set 2
cout << " <CARD 2>\n 2 3 6 7\n10 11 14 15\n18 19 22 23\n26 27 30 31\n" << endl;
cout << "Enter N/n for No and Y/y for Yes: ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
day += 2;
// Prompt the user for Set 3
cout << " <CARD 3>\n 4 5 6 7\n12 13 14 15\n20 21 22 23\n28 29 30 31\n" << endl;
cout << "Enter N/n for No and Y/y for Yes: ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
day += 4;
// Prompt the user for Set 4
cout << " <CARD 4>\n 8 9 10 11\n12 13 14 15\n24 25 26 27\n28 29 30 31\n" << endl;
cout << "Enter N/n for No and Y/y for Yes: ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
day += 8;
// Prompt the user for Set 5
cout << " <CARD 5>\n16 17 18 19\n20 21 22 23\n24 25 26 27\n28 29 30 31\n" << endl;
cout << "Enter N/n for No and Y/y for Yes: ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
day += 16;
cout << day;
system("pause");
return 0;
}
6.26.2017
아이디와 비밀번호 일치 여부 확인하기
*#include <iostream>
using namespace std;
int main() {
int id, pwd;
cout << "아이디와 비밀번호를 입력해주세요." << endl;
cin >> id >> pwd;
if (id != 1234)
cout << "아이디가 틀렸습니다." << endl;
else {
if ( pwd != 987)
cout << "비밀번호가 틀렸습니다." << endl;
else cout << "로그인 되었습니다." << endl;
}
system("pause");
return 0;
}
using namespace std;
int main() {
int id, pwd;
cout << "아이디와 비밀번호를 입력해주세요." << endl;
cin >> id >> pwd;
if (id != 1234)
cout << "아이디가 틀렸습니다." << endl;
else {
if ( pwd != 987)
cout << "비밀번호가 틀렸습니다." << endl;
else cout << "로그인 되었습니다." << endl;
}
system("pause");
return 0;
}
함수 이용하여 최댓값 구하기
*#include <iostream>
using namespace std;
int max(int x,int y)
{ if (x > y)
return x;
else return y;
}
int main(){
int x, y, z, MAx=0;
cin >> x>> y >> z;
max(x,y);
if (x>y)
MAx = max(x,z);
else MAx = max(y,z);
cout << "최댓값은 무엇인가요오오오?" << MAx << endl;
system("pause");
return 0;
}
using namespace std;
int max(int x,int y)
{ if (x > y)
return x;
else return y;
}
int main(){
int x, y, z, MAx=0;
cin >> x>> y >> z;
max(x,y);
if (x>y)
MAx = max(x,z);
else MAx = max(y,z);
cout << "최댓값은 무엇인가요오오오?" << MAx << endl;
system("pause");
return 0;
}
피드 구독하기:
글 (Atom)