الأجوبة
/*Write a C program to find the maximum sum of a contiguous subsequence from a given sequence of numbers a1, a2, a3, ... an ( n = number of terms in the sequence)*/
#include <stdio.h>
int main() {
int n;
long a[5000];
long long max, tmp;
int i, j;
printf("Input number of terms in the sequence:\n");
scanf("%d", & n);
printf("\nInput the terms of the said sequence:\n");
for (i = 0; i < n; i++) scanf("%ld", & (a[i]));
max = a[0];
tmp = 0;
for (i = 0; i < n; i++) {
for (j = i; j < n; j++) {
tmp += a[j];
if (max < tmp)
max = tmp;
}
tmp = 0;
}
printf("Maximum sum of a contiguous subsequence:\n");
printf("%lld\n", max);
return 0;
}أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال