一道一维数组的题目,我编的渣代码求修改!
/*数组中按顺序存放着下列数据11,19,9,12,5,30,1,18,4,16,6,10,15,2,17,3,14,7,13,15,8,你可以逻辑上认为尾部的8和头部的1首尾相连,编程找出相邻的4个数,其相加之和最大,找出这四个数,和他们的起始位置(用数组下标表示)*/
#include <stdio.h>
int main()
{
int number[21]={11,19,9,12,5,20,1,18,4,16,6,10,15,2,17,3,14,7,13,15,8};
int i,x,y,m,n,t,k,c;
int max1,max2,max3,max4,max5;
max1=0;max2=0;max3=0;max4=0;max5=0;
i=0;
m=0;
while(i<18)
{
m=number[i]+number[i+1]+number[i+2]+number[i+3];
max1=(max1>m)?max1:m;
i++;
}
t=18;
n=0;
x=0;
y=0;
n=number[t]+number[t+1]+number[t+2]+number[i-18];
k=19;
x=number[k]+number[k+1]+number[k-19]+number[k-18];
c=20;
y=number[c]+number[c-20]+number[c-19]+number[c-18];
max2=(n>x)?n:x;
max3=(y>max2)?y:max2;
max4=(c>max3)?c:max3;
max5=(max1>max4)?max1:max4;
if(max5==m)
{
printf("%d %d %d %d %d %d %d %d",number[i],number[i+1],number[i+2],number[i+3],i,i+1,i+2,i+3);
}
else if(max5==n)
{
printf("%d %d %d %d %d %d %d %d",number[t],number[t+1],number[t+2],number[t-18],t,t+1,t+2,t-18);
}
else if(max5==x)
{
printf("%d %d %d %d %d %d %d %d",number[k],number[k+1],number[k-19],number[k-18],k,k+1,k-19,k-18);
}
else if(max5==y)
{
printf("%d %d %d %d %d %d %d %d",number[c],number[c-20],number[c-19],number[c-18],c,c+1,c+2,c-18);
}
return 0;
}