본문 바로가기
Baekjoon/단계별로 풀어보기

[단계07] 문자열 (10문제)

by 해적거북 2020. 12. 20.
728x90

www.acmicpc.net/step/7

 

● [문제번호 11654] 아스키 코드

#include <stdio.h>

int main()
{
    char temp;
    scanf("%c", &temp);
    
    printf("%d", temp);
    return 0;
}

 

● [문제번호 11720] 숫자의 합

#include <stdio.h>

int main()
{
    int N;
    scanf("%d", &N);
    
    char temp[101];
    scanf("%s", temp);
    
    int total = 0;
    for(int i = 0; i < N; i++)
        total += temp[i] - 48;
    
    printf("%d", total);
    
    return 0;
}

 

● [문제번호 10809] 알파벳 찾기

#include <stdio.h>
#include <string.h>

int main()
{
    char str[101];
    scanf("%s", str);
    
    int num[26];
    
    for(int i = 0; i < 26; i++)
        num[i] = -1;
        
    for(int i = 0; i < strlen(str); i++)
    {
        int temp = str[i] - 97;
        if(num[temp] != -1) continue;
        else num[temp] = i;
    }
    
    for(int i = 0; i < 26; i++)
        printf("%d ", num[i]);
    
    return 0;
}

 

● [문제번호 2675] 문자열 반복

#include <stdio.h>
#include <string.h>

int main()
{
    int T;
    scanf("%d", &T);
    
    for(int i = 0; i < T; i++)
    {
        int R;
        scanf("%d", &R);
        
        char S[21];
        scanf("%s", S);
        
        for(int a = 0; a < strlen(S); a++)
            for(int b = 0; b < R; b++)
                printf("%c", S[a]);
        
        printf("\n");
    }
    
    return 0;
}

 

● [문제번호 1157] 단어 공부

#include <stdio.h>
#include <string.h>

int main()
{
    char S[1000000];
    scanf("%s", S);
    
    int num[26] = {0};
    int max = -1;
    int flag = 0;
    int len = strlen(S);
    
    for(int i = 0; i < len; i++)
    {
        int temp;
        if('A' <= S[i] && S[i] <= 'Z')
            temp = S[i] - 'A';
        else if('a' <= S[i] && S[i] <= 'z')
            temp = S[i] - 'a';
        
        num[temp]++;
        
        if(max < num[temp])
        {
            max = num[temp];
            flag = temp;
        }
        else if(max == num[temp])
            flag = -1;
    }
    
    if(flag == -1)
        printf("?");
    else
        printf("%c", flag + 'A');
    
    return 0;
}

 

● [문제번호 1152] 단어의 개수

#include <stdio.h>
#include <string.h>

int main()
{
    char str[1000000] = {NULL};
    fgets(str, 1000000, stdin);
    
    int count = 0;
    int len = strlen(str);
    
    // 첫 입력이 공백인 경우
    int i = 0;
    if(str[0] == ' ')
        i = 1;
        
    for( ; i <= len; i++)
        if(str[i] == '\0' || str[i] == ' ')
        	// 입력이 공백공백 인 경우를 대비
            if(str[i - 1] != ' ')
                count++;
    
    // 마지막 입력이 공백인 경우
    if(str[len - 2] == ' ')
        count--;
    
    printf("%d", count);
    
    return 0;
}

// fgets(문자열주소, 입력받을 길이, 표준입력);
// C++14부터 gets가 사라져서 이렇게 사용해야한다.

 

● [문제번호 2908] 상수

#include <stdio.h>

int change(int n)
{
    int temp = 0;
    int ten = 100;
    for(int i = 0; i < 3; i++)
    {
        temp += (n % 10) * ten;
        n /= 10;
        ten /= 10;
    }
    return temp;
}

int main()
{
    int A, B;
    scanf("%d %d", &A, &B);
    
    A = change(A);
    B = change(B);
    
    if(A > B)
        printf("%d", A);
    else
        printf("%d", B);
        
    return 0;
}

 

● [문제번호 5622] 다이얼

#include <stdio.h>
#include <string.h>

int main()
{
    char temp[16];
    scanf("%s", temp);
    
    int len = strlen(temp);
    int total = len;
    for(int i = 0; i < len; i++)
    {
        if('A' <= temp[i] && temp[i] <= 'C')
            total += 2;
        else if('D' <= temp[i] && temp[i] <= 'F')
            total += 3;
        else if('G' <= temp[i] && temp[i] <= 'I')
            total += 4;
        else if('J' <= temp[i] && temp[i] <= 'L')
            total += 5;
        else if('M' <= temp[i] && temp[i] <= 'O')
            total += 6;
        else if('P' <= temp[i] && temp[i] <= 'S')
            total += 7;
        else if('T' <= temp[i] && temp[i] <= 'V')
            total += 8;
        else if('W' <= temp[i] && temp[i] <= 'Z')
            total += 9;
    }
    
    printf("%d", total);
    return 0;
}

 

● [문제번호 2941] 크로아티아 알파벳

#include <stdio.h>
#include <string.h>

int main()
{
    char temp[100];
    scanf("%s", temp);
    
    char list[8][4] = {"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="};
    int listlen[8] = {2, 2, 3, 2, 2, 2, 2, 2};
    
    int count = 0;
    int len = strlen(temp);
    for(int i = 0; i < len; i++)
    {
        int flag = 0;
        for(int j = 0; j < 8; j++)
            if(strncmp(list[j], temp + i, listlen[j]) == 0)
            {
                count++;
                i += (listlen[j] - 1);
                flag = 1;
                break;
            }
        if(flag == 0)
            count++;
    }
    
    printf("%d", count);
    
    return 0;
}

 

● [문제번호 1316] 그룹 단어 체커

#include <stdio.h>
#include <string.h>

int main()
{
    int  N;
    scanf("%d", &N);
    
    int count = N;
    for(int i = 0; i < N; i++)
    {
        char temp[101];
        scanf("%s", temp);
        
        int check[26] = {0};
        int len = strlen(temp);
        for(int j = 0; j < len; j++)
        {
            if(check[temp[j] - 'a'] == 0)
                check[temp[j] - 'a'] = 1;
            else if(temp[j - 1] !=  temp[j])
            {
                count--;
                break;
            }
        }
    }
    
    printf("%d", count);
    
    return 0;
}
728x90

'Baekjoon > 단계별로 풀어보기' 카테고리의 다른 글

[단계10] 재귀 (4문제)  (0) 2021.01.12
[단계09] 기본 수학2 (11문제)  (0) 2021.01.09
[단계08] 기본 수학1 (9문제)  (0) 2020.12.20
[단계06] 함수 (3문제)  (0) 2020.12.18
[단계05] 1차원 배열 (7문제)  (0) 2020.12.17
[단계04] while문 (3문제)  (0) 2020.12.17
[단계03] for문 (11문제)  (0) 2020.12.17

댓글