# 8์ฅ Programming ํ์ด (2)
337p_8๋ฒ)
#include<stdio.h>
#include<math.h>
void cal(double a, double b, double c)
{
if (b * b - 4 * a * c > 0)
{
double one = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
double two = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
printf("์ฒซ ๋ฒ์งธ ๊ทผ = %.4lf\n", one);
printf("๋ ๋ฒ์งธ ๊ทผ = %.4lf\n", two);
}
else
{
printf("๊ทผ์ด ์กด์ฌํ์ง ์์ต๋๋ค.");
}
}
int main()
{
double x, y, z;
printf("2์ฐจ ๋ฐฉ์ ์์ ๊ณ์๋ฅผ ์
๋ ฅํ์์ค:\n");
printf("a: ");
scanf_s("%lf", &x);
printf("b: ");
scanf_s("%lf", &y);
printf("c: ");
scanf_s("%lf", &z);
cal(x, y, z);
return 0;
}
338p_9๋ฒ)
#include <stdio.h>
#include <stdlib.h>
int monte()
{
srand(time(NULL));
int num = rand() % 2;
return num;
}
int main()
{
while (1)
{
int user = 0;
printf("์(0) ๋๋ ๋ค(1)๋ฅผ ์ ํํ์์ค(์ข
๋ฃ๋ -1): ");
scanf_s("%d", &user);
if (user == -1)
{
printf("ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.\n");
break;
}
int a = monte();
if (a == user)
{
printf("์ปดํจํฐ ์น๋ฆฌ\n");
}
else
{
printf("์ฌ์ฉ์ ์น๋ฆฌ\n");
}
}
return 0;
}
338p_10๋ฒ)
#include <stdio.h>
#include <stdlib.h>
int dice()
{
int num = rand() % 6;
return num;
}
int main()
{
srand(time(NULL));
int a = dice();
int b = dice();
int c = dice();
int d = dice();
int e = dice();
int f = dice();
int user_sum = a + b + c;
int com_sum = d + c + f;
printf("์ฌ์ฉ์ ์ฃผ์ฌ์ = (%d, %d, %d) = %d\n", a, b, c, user_sum);
printf("์ปดํจํฐ ์ฃผ์ฌ์ = (%d, %d, %d) = %d\n", d, e, f, com_sum);
if (user_sum > com_sum)
{
printf("์ฌ์ฉ์ ์น๋ฆฌ\n");
}
else if (user_sum == com_sum)
{
printf("๋น๊ฒผ์ต๋๋ค.\n");
}
else
{
printf("์ปดํจํฐ ์น๋ฆฌ\n");
}
return 0;
}
338p_11๋ฒ)
#include <stdio.h>
static int add = 1;
static int sub = 1;
static int mul = 1;
static int div = 1;
void calculator(int a, char k, int b)
{
if (k == '+')
{
printf("๋ง์
์ ์ด %d๋ฒ ํธ์ถ๋์์ต๋๋ค.\n", add++);
printf("์ฐ์ฐ ๊ฒฐ๊ณผ: %d\n", a + b);
}
else if (k == '-')
{
printf("๋บ์
์ ์ด %d๋ฒ ํธ์ถ๋์์ต๋๋ค.\n", sub++);
printf("์ฐ์ฐ ๊ฒฐ๊ณผ: %d\n", a - b);
}
else if (k == '/')
{
printf("๋๋์
์ ์ด %d๋ฒ ํธ์ถ๋์์ต๋๋ค.\n", div++);
printf("์ฐ์ฐ ๊ฒฐ๊ณผ: %d\n", a / b);
}
else
{
printf("๊ณฑ์
์ ์ด %d๋ฒ ํธ์ถ๋์์ต๋๋ค.\n", mul++);
printf("์ฐ์ฐ ๊ฒฐ๊ณผ: %d\n", a * b);
}
}
int main()
{
while (1)
{
int x = 0;
int y = 0;
char cal;
printf("\n์ฐ์ฐ์ ์
๋ ฅํ์์ค.(์ข
๋ฃ๋ Q): ");
scanf_s("%d", &x);
scanf_s("%c", &cal);
scanf_s("%d", &y);
if (cal == 'Q')
{
printf("ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.\n");
break;
}
calculator(x, cal, y);
}
return 0;
}
339p_12๋ฒ)
#include <stdio.h>
static int sum = 0;
void save(int amount)
{
sum += amount;
printf("์ง๊ธ๊น์ง์ ์ ์ถ์ก์ %d์
๋๋ค.\n", sum);
}
void draw(int amount)
{
sum -= amount;
printf("์ง๊ธ๊น์ง์ ์ ์ถ์ก์ %d์
๋๋ค.\n", sum);
}
int main()
{
int num = 0;
while (1)
{
int choose = 0;
printf("\n์ ์ถ(1)๊ณผ ์ธ์ถ(0) ์ค ๋ฌด์์ ํ์๊ฒ ์ต๋๊น?(์ข
๋ฃ๋ -1): ");
scanf_s("%d", &choose);
if (choose == -1)
{
printf("ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.\n");
break;
}
else if (choose == 0)
{
printf("์ผ๋ง๋ฅผ ์ธ์ถํ์๊ฒ ์ต๋๊น?(์ข
๋ฃ๋ -1): ");
scanf_s("%d", &num);
draw(num);
}
else
{
printf("์ผ๋ง๋ฅผ ์ ์ถํ์๊ฒ ์ต๋๊น?(์ข
๋ฃ๋ -1): ");
scanf_s("%d", &num);
save(num);
}
}
return 0;
}
339p_13๋ฒ)
#include <stdio.h>
int show_digit(int x)
{
int num = x % 10;
if (num == 0 && x == 0) // x๊ฐ 10์ ๋ฐฐ์์ผ ๋๋ ๋๋จธ์ง๋ 0์ด๋ ์ด์ ๊ฐ์ ๊ฒฝ์ฐ๋ฅผ ์ ์ธํจ
{
return;
}
else
{
show_digit((x - num) / 10);
}
printf("%d ", num);
}
int main()
{
int n = 0;
printf("์ ์๋ฅผ ์
๋ ฅํ์์ค: ");
scanf_s("%d", &n);
show_digit(n);
return 0;
}
340p_14๋ฒ)
#include <stdio.h>
int sum = 0;
int cal(int m)
{
if (m >= 1)
{
sum += m * m * m;
cal(m - 1);
}
else
{
return sum;
}
}
int main()
{
int num = 0;
printf("์ ์๋ฅผ ์
๋ ฅํ์์ค: ");
scanf_s("%d", &num);
printf("%d\n", cal(num));
return 0;
}