0

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

3 2 0 - 减少 5 5 5 - 均匀 8 9 - 增加

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

#include <stdio.h>
#include <stdlib.h>

main() {

    int array[100];
    int flag[100];
    int num, i;

    printf("Enter how many numbers you want to type: ");
    scanf("%d",&num);

    for(i=0;i<num;i++) {
        scanf("%d",&array[i]);
    }
    for(i=0;i<num;i++){
        if((array[i]<array[i+1])) {
            flag[i]=flag[i+1]=1;
        }
        if(array[i]>array[i+1]) {
            flag[i]=flag[i+1]=-1;
        }
    }

    for(i=0;i<num;i++) {
        if(array[i]==array[i+1]) {
            flag[i]=flag[i+1]=0;
        }
    }
    for(i=0;i<num;i++){
        printf("%d ",flag[i]);
    } 
    printf("\n");

    for(i=0;i<num;i++) {
        if(flag[i]==1) {
            do{
                if(flag[i]==1){
                    printf("%d ",array[i]);
                    i++;
                }
            }while(flag[i]==1);
            printf(" - increasing\n");
        }

        if(flag[i]==0) {
            do{
                if(flag[i]==0){
                    printf("%d ",array[i]);
                    i++;
                }
            }while(flag[i]==0);
            printf(" - evenly\n");
        }

        if(flag[i]==-1) {
            do{
                if(flag[i]==-1) {
                    printf("%d ",array[i]);
                    i++;
                }
            }while(flag[i]==-1);
            printf(" - decreasing\n");
        }

    } 

    system("pause");
    return 0;
}
4

2 回答 2

0

想法:

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

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

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

就像是

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 */
于 2013-04-14T22:35:00.123 回答
0
#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;
}
于 2013-04-14T23:13:34.563 回答