题目描述:
Description
给你一个正整数组成的数组 nums
,返回 nums
中一个 降序 子数组的最大可能元素和。子数组是数组中的一个连续数字序列。
已知子数组[numsl,numsl+1,...,numsr−1,numsr],若对所有 i
(l <= i < r
),numsi>numsi+1都成立,则称这一子数组为 降序 子数组。注意,大小为1
的子数组也视作 降序 子数组。
Input
第一行输入数组长度 n
第二个输入数组元素:1, 2, 3, ... (n个)
0 <= n<= 1000
0 <= nums[i] <= 10000
Output
降序子数组最大和
Sample Input1
6
10 20 30 5 10 50
Sample Output1
50
解释:[50] 是元素和最大的降序子数组,最大元素和为 50 。
Sample Input2
1
10
Sample Output2
10
Sample Input3
0
Sample Output3
0
算法:
代码实现:
# include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int arr[n];
int *ptr=arr;
int temp=0,sum=0;
//数组初始化
for(int i=0;i<n;i++) scanf("%d",&arr[i]);
//ptr-->用来指示下标(指针)
while(ptr<=arr+n-1){
temp=*ptr;
while(*ptr>*(ptr+1)){//将某个连续的降序子数组求和
ptr++;
temp+=*ptr;
}
ptr++;
//比较求出最大和
if(sum<temp) sum=temp;
}
printf("%d\n",sum);
}