【问题标题】:How to print integers in typing order in C - increasing, decreasing or evenly如何在 C 中按键入顺序打印整数 - 递增、递减或均匀
【发布时间】:2013-04-14 21:43:40
【问题描述】:

我尝试编写的 c 程序有问题。程序必须将整数存储在数组中(从键盘读取)。数字必须按照输入的顺序打印出来,例如输入: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 5 8 0 9 1 6 6,你想要什么?
  • 是的,应该按照用户输入的顺序来划分
  • 4 2 - 减少 5 8 - 增加 0 9 - 增加 1 6 - 增加 6 - 我不知道,
  • 4 3 1 0 -3 -3 -3 5 6 7 3 3 4 3 1 0 - 减少 -3 -3 -3 - 均匀 5 6 7 - 增加 3 3 - 均匀

标签: c printing integer typing


【解决方案1】:

想法:

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

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

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

类似

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 */

【讨论】:

    【解决方案2】:
    #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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      相关资源
      最近更新 更多