如何在C中按键入顺序打印整数-递增、递减或均匀

如何在C中按键入顺序打印整数-递增、递减或均匀,c,printing,integer,typing,C,Printing,Integer,Typing,我试图编写的c程序有问题。程序必须在数组中存储整数(从键盘读取)。数字必须按输入顺序打印出来,例如,如果您输入:3 2 0 5 5 8 9,输出应为: 3.2.0-减少 5-均匀 8.9-增加 我的问题是,我不能写一个在所有情况下都能工作的算法。 我试图用另一个数组来“标记”元素(使用相同的索引,为每个整数保存一个值1-表示递增,-1-表示递减,0表示均匀),但这部分起作用。 你还有其他想法吗? 提前感谢:) #包括 #包括 main(){ 整数数组[100]; int标志[100]; int

我试图编写的c程序有问题。程序必须在数组中存储整数(从键盘读取)。数字必须按输入顺序打印出来,例如,如果您输入:3 2 0 5 5 8 9,输出应为:

3.2.0-减少 5-均匀 8.9-增加

我的问题是,我不能写一个在所有情况下都能工作的算法。 我试图用另一个数组来“标记”元素(使用相同的索引,为每个整数保存一个值1-表示递增,-1-表示递减,0表示均匀),但这部分起作用。 你还有其他想法吗? 提前感谢:)

#包括
#包括
main(){
整数数组[100];
int标志[100];
int num,i;
printf(“输入要键入的数字:”);
scanf(“%d”和&num);
对于(i=0;i思想:

  • 只有在看到“第二个”数字后,才能知道“第一个”数字属于降序、偶数还是升序

  • “第三个”数字要么属于与前两个相同的序列,要么是另一个序列的“第一个”数字

所以:检查两个数字并分配一个序列类型。
继续按相同的顺序检查数字。
如果无法分配相同的序列,请返回检查两个数字并分配一个序列

差不多

int *n1, *n2, *n3;
n1 = <first element>;
n2 = n1 + 1;
n3 = n2 + 1;

/* some kind of loop */
if ((n3 - n2) HAS_SOME_SIGN_AS (n2 - n1)) /* n3 belongs to the sequence */;
n1++;
n2++;
n3++;
/* end loop */
int*n1,*n2,*n3;
n1=;
n2=n1+1;
n3=n2+1;
/*某种循环*/
如果((n3-n2)有一些符号作为(n2-n1))/*n3属于序列*/;
n1++;
n2++;
n3++;
/*端环*/
#包括
int状态(a、b){
如果(ab)返回-1;
返回0;
}
int main(){
int数组[100]={0};
int旧_状态、新_状态;
int old_pos;
int num,i,j;
const char*status_message[]={“递减”、“均匀”、“递增”、“我不知道”};
printf(“输入要键入的数字:”);
scanf(“%d”和&num);

对于(i=0;你能再添加几个例子吗?如果用户输入
4 2 5 8 0 9 1 6
,你想要什么?是的,应该按用户输入的顺序划分4 2-减少5 8-增加0 9-增加1 6-增加6-我不知道,4 3 1 0-3-3 5 6 7 3 3 4 3 1 0-减少3-3-3-3-平均5 6 7-增加3 3 3-ev英利
int *n1, *n2, *n3;
n1 = <first element>;
n2 = n1 + 1;
n3 = n2 + 1;

/* some kind of loop */
if ((n3 - n2) HAS_SOME_SIGN_AS (n2 - n1)) /* n3 belongs to the sequence */;
n1++;
n2++;
n3++;
/* end loop */
#include <stdio.h>

int status(a, b){
    if(a < b) return 1;
    if(a > b) return -1;
    return 0;
}

int main() {
    int array[100]={0};
    int old_status, new_status;
    int old_pos;
    int num, i, j;
    const char* status_message[] = {"decreasing","evenly","increasing", "i don't know"};

    printf("Enter how many numbers you want to type: ");
    scanf("%d",&num);
    for(i=0;i<num;i++)
        scanf("%d",&array[i]);

    //{num | 1 < num <= 100}
    old_status =status(array[0], array[1]);
    old_pos = 0;
    for(i=1;i<num;++i){
        new_status = status(array[i-1], array[i]);
        if(old_status != new_status){
            for(j=old_pos;j<i;++j)
                printf("%d ", array[j]);
            printf("- %s\n", status_message[1 + old_status]);
            old_status = (i<num-1)? status(array[i], array[i+1]):2;
            old_pos = i;
        }
    }
    if(old_pos < num){ //not output section output
        for(j=old_pos;j<i;++j)
            printf("%d ", array[j]);
        printf("- %s\n", status_message[1 + old_status]);
    }

    return 0;
}