#include <stdio.h>
#include <string.h>
int p(int a){
if(a==1) return 1;
if(a==2) return 2;
return p(a-1)+p(a-2);
}
// '0'의 아스키코드값은 48, 문자형 숫자를 실제 정수로 바꿀 때 -'0' 또는 -48을 해줌
// 예)문자 '5' - '0' = 53 - 48 = 5
int digit(char a){
int x = a - '0';
printf("a = %c, x = %d\n", a, x);
return a - '0';
}
void main() {
// printf("%d, %d,\n", '2', '2'-'0');
// printf("%d, %d,\n", '1', '1'-'0');
// printf("%d, %d,\n", '3', '3'-'0');
// printf("%d, %d,\n", '4', '4'-'0');
// printf("%d, %d,\n", '5', '5'-'0');
char a[20] = "21345"; //char를 저장할수있는 1B짜리 20개짜리 배열
int i, len, s=0;
len = strlen(a); //len=5
for(i=0;i<len;i++){ //i=1~4
s = s+p(len-i)*digit(a[i]);
printf("s = %d\n", s);
}
printf("%d\n", s);
}
To embed this project on your website, copy the following code and paste it into your website's HTML: