解题思路:
1.先排序
2.比较相邻的元素是否相等,以下两种情况
相等:count++;跳出循环
不等:输出,count重置为1
注意事项:
#include<stdio.h> #include<stdlib.h> int cmp(const void *a, const void *b) { return *(int*)a-*(int*)b; } int main() { int n; scanf("%d",&n); int *arr=malloc(sizeof(int) * n); for(int i=0;i<n;i++) scanf("%d",&arr[i]); qsort(arr,n,sizeof(int),cmp); // 2 2 2 4 4 5 100 100 int count=1; for(int i=0;i<n;i++){ if(arr[i]==arr[i+1]){ count++; continue; } if(arr[i]!=arr[i+1]){ printf("%d %d\n",arr[i],count); count=1; } } return 0; }
参考代码:
0.0分
1 人评分
Pascal三角 (C语言代码)浏览:1188 |
2006年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:468 |
用筛法求之N内的素数。 (C++代码)浏览:692 |
Tom数 (C语言代码)浏览:494 |
震宇大神的杀毒软件 (C语言代码)浏览:1080 |
数组输出 (C语言代码)浏览:703 |
C语言程序设计教程(第三版)课后习题5.8 (C语言代码)浏览:663 |
C语言程序设计教程(第三版)课后习题9.1 (C语言代码)浏览:565 |
C语言程序设计教程(第三版)课后习题9.8 (C语言代码)浏览:521 |
简单的a+b (C语言代码)浏览:557 |