숫자세로

조회수 495회

만약 숫자를 153으로 입력하면

1

5

3

으로 출력을 어떻게 하는지 아시나요?(c언어)

int main(void)
{
int num1, num2, num3;

scanf("%d %d %d", &num1, &num2, &num3);

printf("%d\n", num1);
printf("%d\n", num2);
printf("%d", num3);

return 0;
}

1 3 5를 띄어쓰고 하면 저렇게 출력이 되는데 135를 붙이고 출력을 하면 안됩니다!!

  • 밑에 제가 했던 방법을 썼습니다!! HelloWorld 2020.9.9 15:19

2 답변

  • 처음에 숫자로 입력받은 값을 문자열로 변경이후 처리샘플..

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(){
        int a, b;
        int size = 0;
        char* c;
        a = 164; //scanf("%d", &a);
        b = a;
        while(b>0)
        {
            size++;
            b /= 10;
        }
    
        c = (char *)malloc(sizeof(char)*size);
        itoa(a, c, 10);
    
        for(int i=0; i < strlen(c);i++)
        {
            printf("%c",c[i]);
            if (i != strlen(c)-1)
            {
                printf("\n");   
            }
        }
    
        free(c);
        c = NULL;
        return 0;
    }
    
    

    처음부터 입력값을 문자열로 받을 경우 예제.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        char* buffer;
        buffer = (char *)malloc(sizeof(char) * 100);
        scanf("%s", buffer);
        for(int i=0;i<strlen(buffer);i++)
        {
            printf("%c", buffer[i]);    
            if (i != strlen(buffer)-1)
            {
                printf("\n");   
            }
        }
    
        free(buffer);
        buffer = NULL;
    }
    
    • 댓글에 시도했던 방법을 썼습니다! HelloWorld 2020.9.9 15:19
    • 저는 동적할당했지만 정적할당하셔서 스택 활용하셔도 되요... 김호원 2020.9.9 15:35
  • 
    int main(void)
    {   
        int num1, num2, num3;
    
        scanf("%d %d %d", &num1, &num2, &num3);
    
        printf("%d\n", num1);
        printf("%d\n", num2);
        printf("%d", num3);
    
        return 0;
    }
    

    요렇게 시도를 했습니다! 1 3 5를 띄어쓰면 위에 출력대로 되는데 135를 붙여 쓰니깐 출력이 안되더라고요!!

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)