一、代码如下:
#include <iostream> using namespace std; #define N 101 int n=0;//n个数 int b[N];//存储Bn的值 int MaxSum=0;//存储最大和 int MinSum=0;//存储最小和 void InputBn(){ for(int i=0;i<n;i++){ cin>>b[i]; if(i==0){//初始化MaxSum,MinSum MaxSum=MaxSum+b[i]; MinSum=MinSum+b[i]; } else if(b[i]==b[i-1]){//b[i]==b[i-1]时,MaxSum再加一次此值,MinSum+0 MaxSum=MaxSum+b[i]; MinSum=MinSum+0;//方便理解 } else{//否则,MaxSum+b[i],MinSum+b[i] MaxSum=MaxSum+b[i]; MinSum=MinSum+b[i]; } } } void OutputBn(){ for(int i=0;i<n;i++){ cout<<b[i]<<" "; } } void Output(){ cout<<MaxSum<<endl; cout<<MinSum<<endl; } int main(){ cin>>n;//输入n InputBn();//输入各个Bn值,同时为MaxSum,MinSum赋值 Output(); return 0; }
二、题目如下:
编辑