언어/c, c++

*(백준BAEKJOOB )* 11721 열 개씩 끊어 출력하기

깡 딱 2023. 5. 9. 21:48
728x90
문제 출처

https://www.acmicpc.net/problem/11721

 

11721번: 열 개씩 끊어 출력하기

첫째 줄에 단어가 주어진다. 단어는 알파벳 소문자와 대문자로만 이루어져 있으며, 길이는 100을 넘지 않는다. 길이가 0인 단어는 주어지지 않는다.

www.acmicpc.net

 

문제풀이

 

 

코드
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100

int main() {
    char str[MAX_LEN];
    int sum = 0;

    scanf("%s", str);

    for (int i = 0; i < strlen(str); i++) {
        printf("%c", str[i]);
        sum++;
        if (sum == 10) {
            printf("\n");
            sum = 0;
        }
    }
}

나의 코드는 조금 가독성이 떨어진다.

 

 

+

 

하지만 내 코드와 다르게 string 으로 입력 받아서 그냥 i%10 으로 짤 수 있다는것도 알게 되었다.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
    string s;
    cin >> s;

    for ( int i = 0; i < s.length(); i++ ) {
        cout << s[i];
        if ( i % 10 == 9 ) cout << '\n';
    }
}

 

 

 

  설명 : 간단한 문제였다.

 

728x90