Programming/C

[백준] 단계별로 풀어보기 > 문자열 (C언어)

코딩뽀시래기 2021. 8. 19. 19:51
728x90

+) 풀이 코드

https://github.com/jung0115/CodingTestPractice.git

 

GitHub - jung0115/CodingTestPractice: Practice Coding Test with Beakjoon, programmers, etc.

Practice Coding Test with Beakjoon, programmers, etc. - GitHub - jung0115/CodingTestPractice: Practice Coding Test with Beakjoon, programmers, etc.

github.com

 

11654번 - 2021.08.19.목

#include <stdio.h>

int main(void){
  char input;
  
  scanf("%c", &input);

  printf("%d", input);
  
  return 0;
}

 

11720번 - 2021.08.19.목

#include <stdio.h>

int main(void){
  char num[101];
  int sum=0, i, n;

  scanf("%d", &n);
  scanf("%s", num);

  for(i=0; i<n; i++){
    sum+=(num[i]-48);
  }

  printf("%d", sum);

  return 0;
}

 

10809번 - 2021.08.20.금

#include <stdio.h>

int main(void){
  char S[101];
  int i, Check[27]={0}, num;

  scanf("%s", S);

  for(i=0; S[i]!='\0'; i++){
    num=S[i]-97;
    if(Check[num]==0)
      Check[num]=i+1;
  }

  for(i=0; i<26; i++)
    printf("%d ", Check[i]-1);

  return 0;
}

 

2675번 - 2021.08.20.금

#include <stdio.h>

int main(void){
  int T, R, i, j, k;
  char S[21];

  scanf("%d", &T);

  for(i=0; i<T; i++){
    scanf("%d", &R);
    scanf("%s", S);
    for(j=0; S[j]!='\0'; j++){
      for(k=0; k<R; k++){
        printf("%c", S[j]);
      }
    }
    printf("\n");
  }
  return 0;
}

 

1157번 - 2021.08.20.금

#include <stdio.h>

int main(void){
  char S[1000001];
  int check[26]={0}, i, num, max=0, sameCount=0;

  scanf("%s", S);
  
  for(i=0; S[i]!='\0'; i++){
    if('a'<=S[i] && S[i]<='z') //소문자인 경우
      num=S[i]-'a';
    else if('A'<=S[i] && S[i]<='Z') //대문자인 경우
      num=S[i]-'A';
    check[num]++;
  }

  for(i=1; i<26; i++){
    if(check[max]<check[i]){
      max=i;
      sameCount=0;
    }
    else if(check[max]==check[i])
      sameCount++;
  }

  if(sameCount>0) //가장 많이 사용된 알파벳이 여러개 존재하는 경우
    printf("?");
  else
    printf("%c", max+'A');

  return 0;
}

 

1152번 - 2021.08.21.토

#include <stdio.h>

int main(void){
  char S[1000001];
  int i, cnt=0;

  scanf("%[^\n]s", S); //공백 포함해서 입력 받음

  for(i=0; S[i]!='\0'; i++){
    if(S[i]==' '&&S[i+1]!='\0')
      cnt++;
  }

  if(S[0]!=' ')
    cnt++;

  printf("%d", cnt);

  return 0;
}

 

2908번 - 2021.08.21.토

#include <stdio.h>

int main(void){
  char num1[4], num2[4];
  int i, backNum1=0, backNum2=0, check;

  scanf("%s %s", num1, num2);

  for(i=2; i>=0; i--){
    check=num1[i]-'0';
    backNum1=(backNum1*10)+check;

    check=num2[i]-'0';
    backNum2=(backNum2*10)+check;
  }

  if(backNum1>=backNum2)
    printf("%d", backNum1);
  else
    printf("%d", backNum2);

  return 0;
}

 

5622번 - 2021.08.21.토

#include <stdio.h>

int main(void){
  char number[16];
  int alphabet[26]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9}, i, time=0, num;

  scanf("%s", number);

  for(i=0; number[i]!='\0'; i++){
    num=number[i]-'A';
    time+=(1+alphabet[num]);
  }

  printf("%d", time);

  return 0;
}

 

2941번 - 2021.08.22.일

#include <stdio.h>

int check(char a, char b, char c);
int main(void){
  char S[101];
  int i, cnt=0, checkResult;

  scanf("%s", S);

  for(i=0; S[i]!='\0'; i++){
    cnt++;

    if(S[i+1]=='\0')
      break;
    
    checkResult=check(S[i], S[i+1], S[i+2]);
    i+=checkResult;
  }

  printf("%d", cnt);

  return 0;
}
int check(char a, char b, char c){
  if(a=='d' && b=='z' && c=='=')
    return 2;
  
  else if(a=='c' && b=='=')
    return 1;
  
  else if(a=='c' && b=='-')
    return 1;
  
  else if(a=='d' && b=='-')
    return 1;
  
  else if(a=='l' && b=='j')
    return 1;
  
  else if(a=='n' && b=='j')
    return 1;

  else if(a=='s' && b=='=')
    return 1;
  
  else if(a=='z' && b=='=')
    return 1;
  
  return 0;
}

 

1316번 - 2021.08.22.일

#include <stdio.h>

int check(char *S, int *alphabet);
void setting(int *alphabet);
int main(void){
  char S[101];
  int N, alphabet[26]={0}, i, c, cnt=0;

  scanf("%d", &N);

  for(i=0; i<N; i++){
    scanf("%s", S);
    c=check(S, alphabet);
    setting(alphabet);
    if(c==1)
      cnt++;
  }

  printf("%d", cnt);

  return 0;
}
int check(char *S, int *alphabet){
  int i, num;

  for(i=0; S[i]!='\0'; i++){
    num=S[i]-'a';
    if(alphabet[num]!=0 && S[i-1]!=S[i])
      return 0;
    else if(alphabet[num]==0)
      alphabet[num]++;
  }
  return 1;
}
void setting(int *alphabet){
  int i;

  for(i=0; i<26; i++)
    alphabet[i]=0;
}

 

728x90