/*

[카카오 코딩테스트]1. 자연수 자릿수의 합 구하기

 

문제이미지

*/

 

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int main(int argc, char *argv[])

{

    char *str = "345678374755";

char tmp[10];

int ii,kk, summation=0;

 

printf("str:(%s)\n", str);

 

for(kk=0; kk<strlen(str); kk++)

{

memset(tmp,0x00,sizeof(tmp));

 

sprintf(tmp, "%c", str[kk]);

 

if(kk != strlen(str) -1)

{

printf("%d + ", atoi(tmp));

}

else

{

printf("%d = ", atoi(tmp));

}

summation = summation + atoi(tmp);

}

printf("%d\n", summation);

return(0);

}

 

/*결과

bash-3.1$ gcc -c 24.c

bash-3.1$ gcc -o 24 24.o

bash-3.1$ 24

str:(345678374755)

3 + 4 + 5 + 6 + 7 + 8 + 3 + 7 + 4 + 7 + 5 + 5 = 64

bash-3.1$

*/

 

+ Recent posts