|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
int main(){
int a[12] = {20,21,22,8,1,54,2,3,99,14,11,12};
int j=0,k;
while(j<12){
printf("(");
while(a[j]<a[j+1]){ //升序
printf("%d,",a[j]);
if(a[j+1]>a[j+2]){
printf("%d",a[j+1]);
j++;
}
else{
j++;
continue;
}
printf(")");
}
printf(",");
printf("(");
while(a[j]>a[j+1]){ //降序
printf("%d,",a[j]);
if(a[j+1]<a[j+2]){
printf("%d",a[j+1]);
j++;
}
else{
j++;
continue;
}
printf(")");
}
printf(",");
}
}
输出是这个:(20,21,22),(22,8,1),(1,54),(54,2),(2,3,99),(99,14,11),(11,12),(12,11),
就差一点了,请问怎么改?
本帖最后由 jhq999 于 2021-10-16 20:44 编辑 int a[12] = {20,21,22,8,1,54,2,3,99,14,11,12};
int i=1,flag1=0,flag2=0;
if (a[i]>a[i-1])
{
flag2=flag1=1;
}
printf("(");
while(1)
{
if (flag1!=flag2)
{
printf("),(");
flag1=flag2;
i--;
}
else if (i>1)
printf(",");
printf("%d",a[i-1]);
if (i>11)break;
if (a[i]>a[i-1])
{
flag2=1;
}
if (a[i]<a[i-1])
{
flag2=0;
}
i++;
}
printf(")");
|
|