사칙연산
#1000 덧셈
두 정수 A, B를 입력받고 두 수의 합을 출력
#include <stdio.h>
int main(void){
int a, b;
scanf("%d%d", &a, &b);
printf("%d", a+b);
return 0;
}
#1001 뺄셈
#include <stdio.h>
int main(void){
int a, b;
scanf("%d%d", &a, &b);
printf("%d", a-b);
return 0;
}
#10998 곱셈
#include <stdio.h>
int main(void){
int a, b;
scanf("%d%d", &a, &b);
printf("%d", a*b);
return 0;
}
#1008
나눗셈
실제 정답과 출력값의 절대오차 또는 상대오차가 10.¯⁹이하이면 정답이다.
라는 조건이 있는데 처음에 잘 이해를 못했다.
float형으로 해봤는데 오차가 심해 double로 바꿈 + 6자리로는 오차를 다루기 충분하지 않다는 설명
#include <stdio.h>
int main(void){
int a, b;
scanf("%d%d", &a, &b);
printf("%.10lf", (double)a/(double)b);
return 0;
}
#10869 사칙연산(덧셈, 뺄셈, 곱셈, 나눗셈, 나머지)
#include <stdio.h>
int main(void){
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n%d\n%d\n%d\n%d", a+b, a-b, a*b, a/b, a%b);
return 0;
}
#10430 나머지
(A+B)%C는 ((A%C) + (B%C))%C 와 같을까?
(A×B)%C는 ((A%C) × (B%C))%C 와 같을까?
#include <stdio.h>
int main(void){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
printf("%d\n%d\n%d\n%d\n", (a+b)%c, ((a%c)+(b%c))%c, (a*b)%c, ((a%c)*(b%c))%c);
return 0;
}
#2588 곱셈
#include <stdio.h>
int main(void){
int a, b, i;
int arr[3] = {0};
scanf("%d%d", &a, &b);
for(i=0; i<3; i++){
arr[i] = b%10;
b/=10;
}
for(i=0; i<3; i++) printf("%d\n", a*arr[i]);
printf("%d", a*arr[0]+a*arr[1]*10+a*arr[2]*100);
return 0;
}
마지막에 출력하는 게 의도하는 바가 맞는지 잘 모르겠다.
#include <stdio.h>
int main(void){
int a, b, i, temp;
int arr[3] = {0};
scanf("%d%d", &a, &b);
temp = b;
for(i=0; i<3; i++){
arr[i] = b%10;
b/=10;
}
for(i=0; i<3; i++) printf("%d\n", a*arr[i]);
printf("%d", a*temp);
return 0;
}
이렇게도 할 수 있다.
*그냥 출력해도 되는거였다.
#include <stdio.h>
int main(void){
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n%d\n%d\n%d", a*(b%10), a*(b/10%10), a*(b/100), a*b);
return 0;
}
https://www.acmicpc.net/step/1
입출력과 사칙연산 단계
입출력과 사칙연산
www.acmicpc.net
'BOJ(백준)' 카테고리의 다른 글
[C언어] 백준 - 1차원 배열 (10818, 2562, 2577, 3052) (0) | 2020.09.30 |
---|---|
[C언어] 백준 - while문(10952, 10951, 1110) (0) | 2020.08.30 |
[C언어] 백준 - for문(11021, 11022, 2438, 2439, 10871) (0) | 2020.08.29 |
[C언어] 백준 - for문(2739, 10950, 8393, 15552, 2741, 2742) (0) | 2020.08.29 |
[C언어] 백준 - if문 (1330, 9498, 2753, 14681, 2884) (0) | 2020.08.28 |