BOJ(백준)

[C언어] 백준 - 입출력과 사칙연산(1000, 1001, 10998, 1008, 10869, 10430, 2588)

ruming 2020. 8. 27. 05:40

사칙연산

 

#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