BOJ(백준)
[C언어] 백준 - while문(10952, 10951, 1110)
ruming
2020. 8. 30. 04:10
#10952 A+B
A, B를 입력받고 합을 출력, 마지막에 0 0이 입력되면 종료
#include <stdio.h>
int main(void){
int a, b;
while(1){
scanf("%d%d", &a, &b);
if(a==0&&b==0) return 0;
printf("%d\n", a+b);
}
}
#include <stdio.h>
int main(void){
int a, b;
while(1){
scanf("%d%d", &a, &b);
if(!a&&!b) return 0;
printf("%d\n", a+b);
}
}
#10951 A+B
EOF
#include <stdio.h>
int main(void){
int a, b;
while(scanf("%d%d", &a, &b) != EOF){
printf("%d\n", a+b);
}
return 0;
}
종료 조건을 주지 않아 EOF를 발생시켜 종료한다.
#1110 더하기 사이클
0 <= N <= 99
각 자릿수를 더해 N의 끝자릿수와 자릿수를 더한 합의 끝자릿수를 붙이는 방법으로 새로운 수를 만든다.
그렇게 N으로 돌아갈 때까지의 횟수를 출력하는 것이 목표다.
(한자릿수일때는 앞에 0을 붙여 두자리 수로 만듦)
예) 26 -> 2+6 = 8; 68 -> 6+8 = 14; 84 -> 8+4 = 12; 42 -> 4+2 = 6; 26 (다시 돌아올때까지 4번)
#include <stdio.h>
int main(void){
int n, a, b, temp, cnt = 0;
scanf("%d", &n);
temp = n;
while(1){
a = n/10;
b = n%10;
n = b*10 + (a+b)%10;
cnt++;
if(n==temp){
printf("%d", cnt);
return 0;
}
}
}