본문 바로가기
IT/프로그래밍

C언어 본색 파트1 Chapter 5 연습문제 솔루션

by nutrient 2021. 5. 31.
728x90
728x170

 

1번

#include <stdio.h>

void main() {
	int x, y, z;
	int result;

	printf("input x : ");
	scanf("%d", &x);
	printf("input y : ");
	scanf("%d", &y);
	printf("input z : ");
	scanf("%d", &z);

	result = (x+y) * (x+z) / (y%z);

	printf("( x + y ) * ( x + z ) / ( y %% z ) = %d \n", result);
}

/*

x y z를 입력 받아 연산 결과를 출력하는 단순한 문제입니다.

*/

 

2번

#include <stdio.h>

int main() {
	int a, b;
	int result;

	printf("input : ");
	scanf("%d", &a);
	printf("input : ");
	scanf("%d", &b);

	(a==b) ? printf("같음 \n") : (a>b) ? printf("Smaller Number : %d \n", b) : printf("Smaller Number : %d \n", a);

	return 0;
}

/*

조건 연산자를 이용하여 문제를 해결합니다.
if-else를 이용하면 가독성이 높겠지만,
연산자에 대해 다루는 단원의 연습문제이므로 조건 연산자를 적극 이용하여 문제를 이용합니다.

*/

 

3번

#include <stdio.h>

int main() {
	int x=3, y=5, z=3, k=2;
	int a;

	a = x < y || x < z && z < k;
	printf("결과 값 1 a : %d \n", a);

	a = (x < y || x < z) && z < k;
	printf("결과 값 2 a : %d \n", a);

	return 0;
}

/*

결과값을 예상하고 분석해보는 문제입니다.

첫번째 a는 x가 y보다 작고 x가 z보다 작지 않으며, z가 k보다 작지 않으므로
"참 or 거짓 and 거짓"이 되어 참이 되며 값은 1을 가지게 됩니다.
(and 연산자가 우선순위를 가지며 결과적으로 "참 또는 거짓"이 되어 참이 됩니다.)

두번째 a는 
"(참 or 거짓) and 거짓)" 이 되어 거짓이 되며 값은 1을 가지게 됩니다.

*/

 

4번

#include <stdio.h>

int main() {
	int a, b, c;
	int result;
	int result1;
	int result2;

	printf("input a : ");
	scanf("%d", &a);
	printf("input b : ");
	scanf("%d", &b);
	printf("input c : ");
	scanf("%d", &c);

	result1 = (a > b) ? a : b;
	result2 = (a > c) ? a : c;
	result = (result1 > result2) ? result1 : result2 ;

	printf("Result : %d \n", result);

	return 0;
}

/*

조건 연산자인 ?를 통해 if문을 사용하지 않고 간단하게 특정값이 선택되도록 합니다.
a b c 를 비교하기 위해 a와 b, a와 c를 비교한 뒤 그 결과를 다시 비교하여 출력합니다.

*/

 

5번

#include <stdio.h>

int main() {
	double exchange_rate = 1095.3;
	int dollar=0;

	printf("환전할 금액을 입력해주세요. : ");
	scanf("%d", &dollar);
	printf("환전된 금액 : %.2lf 원\n", dollar * exchange_rate);

	return 0;
}

/*

현재의 환율을 double형 변수에 저장하고
입력받은 금액을 환전 금액으로 출력하는 간단한 프로그램입니다.

*/

 

6번

#include <stdio.h>

int main() {
	double year=0;
	double life=0;
	double life_normal=100;
	double cigar=0;

	printf("흡연 기간?(단위 : 년) : ");
	scanf("%lf", &year);

	printf("하루에 몇 개피 핌? : ");
	scanf("%lf", &cigar);
	
	life = life_normal - 0.0014 * cigar * 365 * year;
	printf("기대 수명 (평균=100세) : %.4lf 세 \n", life);

	return 0;
}

/*

문제에서는 담배 한 개피에 대한 힌트만 있으므로,
흡연기간뿐만 아니라 하루에 몇개피를 피는 지도 물어봅니다.
이를 통해 수명을 계산하고 출력합니다.
%.4lf에서 .4를 붙인 것은 소수점이하로 4자리까지만 출력하기 위함입니다.

아래는 참고할 수 있는 또 다른 답입니다.

#include <stdio.h>

int main(void)
{
	int cigarette;
	int day, year, total, time, daytime, result;
	int rest, restmonth, restday;

	printf("수명은 100살까지이고 20살부터 20년동안 담배를\n");
	printf("하루에 담배 한갑을 핀다고 가정하였을때\n\n");
 
	cigarette = 2; //한가치에 2분
	day = cigarette *20; //하루에 한갑
	year = day *365; //365일 핀 담배량
	total = year * 20; //20년동안 핀 담배량

	printf("단축된 수명 분량 : %d분\n", total);
	time = total/60;
	printf("단축된 수명 시간량 : %d시간 \n", time);
	daytime = time/24;
	printf("단축된 수명 일 : %d일\n", daytime);
 
	rest = 365-daytime;
	printf("살수 있는 수명 : 99살 %d일\n", rest);
	restmonth = rest/30;
	restday = rest%30;
	printf("살수 있는 수명 : 99살 %d달 %d일\n\n", restmonth, restday);
	return 0;
}

*/

 

7번

#include <stdio.h>

int main() {
	int a;
	int result;
	int result1;
	int result2;
	int result3;
	int final;


	printf("0000과 1111사이의 2진수를 입력하시오 : ");
	scanf("%d", &a);
	
	result = (a / 1000) * 8;
	result1 = ((a % 1000) / 100) * 4;
	result2 = ((a % 100) / 10) * 2;
	result3 = (a % 10) * 1;

	final = result + result1 + result2 + result3;

	printf("이진수 %d를 10진수로 바꾸면 %d입니다.\n", a, final);

	return 0;
}

/*

이진수값이 입력되었다고 가정하고 이를 십진수로 계산합니다.
각 자리수를 저장하기 위한 result, result1, result2, result3으로 저장하고 최종값을 final로 저장합니다.
이진수값이 입력되지 않았을 때의 경우에 대한 유효성 검사는 포함되지 않은 소스 코드입니다.

*/

 

8번

#include <stdio.h>

int main() {
	int h, m, s;
	int result;

	printf("시, 분, 초를 차례로 입력 : ");
	scanf("%d %d %d", &h, &m, &s);

	result = h * 60 * 60 + m * 60 + s;

	printf("%d 초 \n", result);

	return 0;
}

/*

시 분 초를 입력 받아 이를 단위에 맞게 모두 계산하여 출력합니다.
연산자 우선순위가 적용되므로 괄호 연산 없이 풀어서 계산합니다.

*/

 

9번

#include <stdio.h>
#define POUND 0.45


int main() {
	int wheat = 150;
	int result;

	result = wheat * POUND;

	printf("밀가루 %d 파운드는 %d kg입니다. \n", wheat, result);

	return 0;
}

/*

1파운드에 대한 kg 값정을 상수로 미리 의해두고
이를 계산하여 출력합니다.

*/

 

10번

#include <stdio.h>
#define FEET 0.3048

int main() {
	int feet;
	int result;

	printf("FEET  : ");
	scanf("%d", &feet);

	result = feet * FEET;

	printf("%d m\n", result);

	return 0;
}

/*

1피트의 값을 미리 정의해두고 입력 값에 따라 몇 미터인지 출력합니다.

*/
728x90
그리드형

댓글