最大字串和(动态规划)
要笔试了,写个求最大字串和练练手,网上搜索了一下感觉都是错误的,就自己写了个自己觉得正确的解法,代码如下:
#include<stdio.h>
int main()
{
int i,temp,max;
int a[10];
printf("输入dao10个数内为容:\n");
for(i=0;i<10;i++){
scanf("%d",&a[i]);
}
max = temp = a[0];
for(i=1;i<10;i++)
{
if(a[i] < 0 && temp < 0){
if(a[i]>temp){
temp = a[i];
max = a[i];
continue;
}
}
if(temp < 0 && a[i] >= 0){
temp = a[i];
max = a[i];
continue;
}
temp+=a[i];
if(temp>=0)
{
if(temp>max)
{
max=temp;
}
}else{
temp = -1;
}
}
printf("%d\n", max);
}