**
简洁版本:
**
#include<stdio.h>
#define MAX_size 100000
int Findmaxsum(int array[], int numsize);
int main()
{
int array[MAX_size];
int size;
int maxsum = 0;
scanf("%d", &size);
getchar();
for (int i = 0;i < size;i++)
{
scanf("%d", &array[i]);
getchar();
}
maxsum = Findmaxsum(array, size);
printf("%d\n", maxsum);
return 0;
}
int Findmaxsum(int array[], int numsize)
{
int temp_array[MAX_size];
int temp = 0;
int maxsum = 0;
int count = 0;
for (int i = 0;i < numsize;i++)
{
if (array[i] >= 0)
break;
else count++;
}
if (count == numsize)
return 0;
for (int i = 0;i < numsize;i++)
{
temp <= 0 ? (temp_array[i] = array[i], temp = array[i]) : (temp + array[i] > 0 ? (temp_array[i] = temp + array[i], temp = temp_array[i]) : (temp_array[i] = array[i], temp = array[i]));
}
for (int i = 0;i < numsize;i++)
{
maxsum = temp_array[i] > maxsum ? temp_array[i] : maxsum;
}
return maxsum;
}
好理解版本:
#include<stdio.h>
#define MAX_size 100000
int Findmaxsum(int array[], int numsize);
int main()
{
int array[MAX_size];
int size;
int maxsum = 0;
scanf("%d", &size);
getchar();
for (int i = 0;i < size;i++)
{
scanf("%d", &array[i]);
getchar();
}
maxsum = Findmaxsum(array, size);
printf("%d\n", maxsum);
return 0;
}
int Findmaxsum(int array[], int numsize)
{
int temp_array[MAX_size];
int temp = 0;
int maxsum = 0;
int count = 0;
for (int i = 0;i < numsize;i++)
{
if (array[i] >= 0)
break;
else count++;
}
if (count == numsize)
return 0;
for (int i = 0;i < numsize;i++)
{
if (temp <= 0)
{
temp_array[i] = array[i];
temp = array[i];
}
else {
if (temp + array[i] > 0)
{
temp_array[i] = temp + array[i];
temp = temp_array[i];
}
else {
temp_array[i] = array[i];
temp = array[i];
}
}
}
for (int i = 0;i < numsize;i++)
{
maxsum = temp_array[i] > maxsum ? temp_array[i] : maxsum;
}
return maxsum;
}
代码实现的效果是一样的,而且代码也是“一样”的,只是把if语句都换成了三目运算了。虽然不好看,但是简洁呀,哈哈哈哈嗝~。