입력 코드
#include <stdio.h>
#include <string.h>
void score() {
char arr[81];
int t, i;
int j, temp = 0, total = 0;
scanf("%d", &t);
for (i = 0; i < t; i++) {
scanf("%s", arr);
for (j = 0; j < strlen(arr); j++) {
if (arr[j] == 'O') {
total += 1 + temp;
temp++;
}
else
temp = 0;
}
printf("%d\n", total);
temp = 0;
total = 0;
}
return 0;
}
main() {
score();
return 0;
}
코드 설명
배열, 반복문, 조건문, 증감 연산자를 이용하는 문제
이것도 2675번처럼 몇 번 테스트할건지 먼저 입력하고 그다음에 문자열을 입력하는 문제이다.
2021/01/03 - [C] - #6. [백준_C언어] 2675번 : 문자열 반복
#include <stdio.h>
void score(char s[80]) {
int i = 0, j, temp, total = 0;
for (j = 0; j < 80; j++) {
if (s[j] == "O")
++i;
if (s[j] == s[j - 1])
temp = i;
++total;
else if (s[j] == "X")
total = total;
}
}
main() {
int i, t;
char s;
for (i = 0; i < t; i++) {
scanf("%s", s);
}
score(s);
}
처음에는 어떻게 해야할지 감이 안 잡혔다.
#include <stdio.h>
void score(char s[80]) {
int j, temp=0, total = 0;
scanf("%s", &s);
for (j = 0; j < 80; j++) {
if (s[j] == "O") {
++temp;
total = total + temp;
}
else if (s[j] == "X")
total = total;
}
printf("%d", total);
}
main() {
int i, t;
char s[80];
scanf("%d", &t);
for (i = 0; i < t; i++) {
scanf("%s", s);
}
score(s);
}
"O"가 있는 것 중에 연속인 "O"를 찾으려고 조건문 안에 조건문을 작성했는데 변수 초기화하는 부분에서 뭔가 잘못되었는지 자꾸 오류가 났다. 결국 시간 초과로 찾아보았다.
자꾸 경고가 떠서 #define _CRT_SECURE_NO_WARNINGS을 해줬는데도 자꾸 값이 이상하게 나왔다.
↓↓↓ 참고한 사이트
squire3131.egloos.com/v/4349122
#include <stdio.h>
#include <string.h>
void score() {
char arr[81];
int t, i;
int j, temp = 0, total = 0;
scanf("%d", &t);
for (i = 0; i < t; i++) {
scanf("%s", arr);
for (j = 0; j < strlen(arr); j++) {
if (arr[j] == 'O') {
total += 1 + temp;
temp++;
}
else
temp = 0;
}
printf("%d\n", total);
temp = 0;
total = 0;
}
return 0;
}
main() {
score();
return 0;
}
연속된 "O"의 개수를 증감 연산자를 이용해서 세는 게 중요한 것 같은데 전위/후위 증감 연산자가 헷갈렸다.
↓↓↓ 참고한 사이트
dojang.io/mod/page/view.php?id=96
다음 줄 테스트 점수를 출력하기 위해서도 전의 값을 초기화해야 하는 것 같았다. 그리고 문자열 크기가 80이라고 해도 끝에 '\0'이 있으니까 최소한 크기를 81로 잡아야 한다.
↓↓↓ 참고한 사이트
★★아직도 잘 모르겠는 문제★★
문제 출처
'C' 카테고리의 다른 글
#11. [백준_C언어] 10869번 : 사칙연산 (0) | 2021.01.05 |
---|---|
#10. [백준_C언어] 10818번 : 최소, 최대 (0) | 2021.01.05 |
#8. [백준_C언어] 2920번 : 음계 (0) | 2021.01.04 |
#7. [백준_C언어] 2739번 : 구구단 (0) | 2021.01.04 |
#6. [백준_C언어] 2675번 : 문자열 반복 (0) | 2021.01.03 |