按降序打印数字及其频率
给定一个int元素数组,任务是按降序排列元素并查找它们的出现。
Input : arr[]={1,1,1,2,2,2,3,3,4,5,6,7,7}
Output : 7 occurs: 2
6 occurs: 1
5 occurs: 1
4 occurs: 1
3 occurs: 2
2 occurs: 3
1 occurs: 3算法
START
Step 1 -> input array with elements in sorting order
Step 2 -> calculate size of an array by sizeof(a)/sizeof(a[0]
Step 3 -> store size in a variable say en
Step 4 -> Loop For i=siz-1 and i>0 and i==
IF a[i]!=a[i-1]
Set to=en-1
Print a[i] and to
Set en=i
End
Step 5 -> print a[0] and to
STOP示例
#include<stdio.h>
int main() {
int a[]={1,1,1,2,2,2,3,3,4,5,6,7,7};
int siz,i,en,st,to;
siz=sizeof(a)/sizeof(a[0]);
en=siz;
for(i=siz-1;i>0;i--) {
if(a[i]!=a[i-1]) {
to=en-i;
printf("%d occurs: %d\n",a[i],to);
en=i;
}
}
to=en;
printf("%d occurs: %d\n",a[0],to);
}输出结果
如果我们运行上面的程序,那么它将生成以下输出
7 occurs: 2 6 occurs: 1 5 occurs: 1 4 occurs: 1 3 occurs: 2 2 occurs: 3 1 occurs: 3
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短