苏州笔试题目

今日去苏州某公司面试,笔试题目。

 

一、猴子吃桃问题

    一个猴子摘了一些桃子,每天是吃一半加一个,第十天的时候剩下1个。一共有多少个桃子?

/* parameters: day: the number of days. left: the count of left peaches on the @day day. */ unsigned int count_peach(unsigned int day, unsigned int left) { if (day == 1) { return left; } return count_peach(--day, (left + 1) * 2); }

二,将一个字符串中的‘/t’替换成四个a

char* string_replace(char* str) { int count = 0; int len = strlen(str); int index = 0; int new_len = 0; for (int i = 0; i < len; i++) { if (str[i] == '/t') { count++; } } new_len = count * 3 + len + 1; char* p = (char*)malloc(new_len * sizeof(char)); for (int i = 0; i < len; i++) { if (str[i] == '/t') { for (int j = 0; j < 4; j++) { p[index + j] = ' '; } index += 3; } else { p[index] = str[i]; } index++; } p[index] = '/0'; return p; }

 

调用实例

int main() { printf("count of peaches = %d/n", count_peach(10, 1)); char* p = "ab/tcab/tc"; char* pp = string_replace(p); printf("%s/n", pp); free(pp); return 0; }

你可能感兴趣的:(String,面试,Parameters)