C- 输入整数,统计次数

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

int main()
{
    const int number=10;
    int  count[number];
    int i;
    int x;
    for(i=0;i<count[number];i++){//初始化数组
        count[i]=0;
    }
    scanf("%d ",&x);
    while(x!=-1){
    if(x>=0&&x<=9){
        count[x]++;
    }
        scanf("%d",&x);
    }
     for(i=0;i<10;i++){
        printf("%d:%d\n",i,count[i]);//遍历数组

     }
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例代码,使用了嵌套循环比较四个整数的大小,并记录比较次数: ```c #include <stdio.h> int main() { int a, b, c, d; printf("Enter four integers: "); scanf("%d %d %d %d", &a, &b, &c, &d); int count = 0; // 记录比较次数 if (a > b) { printf("%d is greater than %d\n", a, b); count++; } else if (a < b) { printf("%d is less than %d\n", a, b); count++; } else { printf("%d is equal to %d\n", a, b); } if (a > c) { printf("%d is greater than %d\n", a, c); count++; } else if (a < c) { printf("%d is less than %d\n", a, c); count++; } else { printf("%d is equal to %d\n", a, c); } if (a > d) { printf("%d is greater than %d\n", a, d); count++; } else if (a < d) { printf("%d is less than %d\n", a, d); count++; } else { printf("%d is equal to %d\n", a, d); } if (b > c) { printf("%d is greater than %d\n", b, c); count++; } else if (b < c) { printf("%d is less than %d\n", b, c); count++; } else { printf("%d is equal to %d\n", b, c); } if (b > d) { printf("%d is greater than %d\n", b, d); count++; } else if (b < d) { printf("%d is less than %d\n", b, d); count++; } else { printf("%d is equal to %d\n", b, d); } if (c > d) { printf("%d is greater than %d\n", c, d); count++; } else if (c < d) { printf("%d is less than %d\n", c, d); count++; } else { printf("%d is equal to %d\n", c, d); } printf("Total comparisons: %d\n", count); return 0; } ``` 注意,这个代码只比较了两两之间的大小关系,没有对四个数进行排序。如果需要对四个数进行排序,可以使用类似冒泡排序或选择排序的算法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值