嵌入式软件开发笔试题 C语言
2009-11-21 09:05:14| 分类:
c/c++
| 标签:
|字号大中小 订阅
这里是一个同学应聘一个嵌入式软件开发职位时遇到的一道笔试题。
题目:在一个给定的序列中找到一个子序列。使得这个子序列的所有元素和最大。(子序列的各元素是原序列中相邻的元素)
例如:*s ={3 , 2 ,-1 , 2 ,-10 , 3}
子序列应是 sub ={3 , 2 ,-1 , 2}
结果最大值应是 6
以下代码就是这个程序了:(并不是很完美的程序,还望大家不吝赐教。)
#include<stdio.h>
#include<stdlib.h>
/*FUNCTION:max_subseq(int *s,int n,int *start,int *end)
*This function will search a subsequence from the sequence named "s",and the elements
* of the subsequence hava the max sum.
* The parameter: s the pointer of the original subsequence.
* n the length of the original subsequence.
* start the front position of the subsequence in the original one.
* end the back position of the subsequence in the original one.
* Result: return the maxvalue of the subsequence had been select.
*/
int max_subseq(int *s , int n ,int *start ,int *end)
{}
if(sum > maxvalue) // compare the sum and the maxvalue and record the start
// positioin and the end position
{}
else
{}
}
}
return maxvalue; // return the result
}
int main()
{}
*/
s =(int *)malloc(n * sizeof(int));
printf("please enter the %d elements of the array:",n);
for(i = 0;i < n;i++)
{}
printf("\n");
printf("the array you entered is :");
for(i = 0;i < n;i++)
{}
printf("\n");
result=max_subseq( s , n , start , end);
printf("the start position is :%3d,and the end is :%3d,the result is :%3d\n",*start,*end,result);
free(s);
return 0;
}
评论这张
转发至微博
转发至微博
0人 |
分享到:
阅读(1377)|
评论()|
引用 (0)
|举报