一组百度笔试题

来源:互联网 发布:绘制架空世界地图软件 编辑:程序博客网 时间:2024/04/28 23:55

1. 有一箱苹果,3个一包还剩2个,5个一包还剩3个,7个一包还剩2个,求N个满足以上条件的苹果个数。

#include <stdlib.h>void find(int n){int i;for(i = 0; i <= n; i++){if((i - 2) % 3 == 0 &&           (i - 3) % 5 == 0 &&           (i - 2) % 7 == 0){printf("%d \n", i);}}}int main(){find(1000);} 

2. 用递归算法写一个函数,求字符串最长连续字符的长度,比如aaaabbcc的长度为4,aabb的长度为2,ab的长度为1。

#include <stdlib.h>char c[100] = "aabbbasdfadfsdfdddssasddddd";int max = 0;void findMax(char *c){int i = strlen(c) - 1;int tempMax = 1;char temp = c[i];if(i < 0){return;}while(i-- >= 0){if(c[i] == temp){++tempMax;}else{max = max < tempMax ? tempMax : max;c[++i] = '\0';break;}}findMax(c);}int main(){findMax(c);printf("%d",max);} 


原创粉丝点击