给定一个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
登录后复制
算法
STARTStep 1 -> input array with elements in sorting orderStep 2 -> calculate size of an array by sizeof(a)/sizeof(a[0]Step 3 -> store size in a variable say enStep 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 EndStep 5 -> print a[0] and toSTOP
登录后复制
Example
的中文翻译为:
示例
#includeint 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",a[i],to); en=i; } } to=en; printf("%d occurs: %d
",a[0],to);}
登录后复制
输出
如果我们运行上述程序,它将生成以下输出
7 occurs: 26 occurs: 15 occurs: 14 occurs: 13 occurs: 22 occurs: 31 occurs: 3
登录后复制
以上就是按照降序打印数字及其频率的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2584468.html