解题思路:
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 人评分
C语言训练-字符串正反连接 (C语言代码)浏览:618 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:508 |
不容易系列 (C语言代码)浏览:664 |
A+B for Input-Output Practice (V) (C语言代码)浏览:617 |
简单的a+b (C语言代码)浏览:626 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:468 |
字符逆序 (C语言代码)浏览:455 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:504 |
图形输出 (C语言代码)浏览:1375 |
1035 题解浏览:778 |