2 zhen xiao123 ZHEN_XIAO123 于 2014.06.27 11:28 提问

做一道题,遇到一些疑问。public static int[] 函数 ;

取出整型数据中出现次数最多的元素,并按照升序排列返回。

要求实现方法:
public static int[] calcTimes(int[] num, int len);

【输入】 num:整型数组;

     len 输入的整数个数

【返回】 按照升序排列返回整型数组中出现次数最多的元素

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例
输入:num = {1,1,3,4,4,4,9,9,9,10} len = 10

返回:{4,9}

这是我用 c++编写的代码

#include
#include
using namespace std;
/*
在数组arr_num中查找 num,如果有,将对应的arr_count 加1,如果没有,在arr_num中添加,
设置arr_count 设置为1,并且相应的计数器加1;

*/
void find (int & num_,int *arr_num,int *arr_count,int &count)
{
for(int j =0 ;j <count ;j++)
if(arr_num[j] == num_)
arr_count[j]++;
arr_num[count] = num_;
arr_count[count]= 1;
count++;
}

int calcTimes (int num ,int len)
{
if (len == 0)
return 0;
int count = 0,dex =0 ;
//创建两个数字,一个用来存放数字,一个用来存放该数字出现的次数
int *arr_count = new int[len];
int *arr_num = new int [len];
for(dex =0 ;dex <len ;dex++)
find (num[dex],arr_num,arr_count,count);
int max = -1;
int numb ;
//arr_cout出现的最大值,该最大值出现的次数。
for(dex = 0 ;dex < count ;dex++)
if(max < arr_count[dex])
{
max = arr_count[dex];
numb =1;
}
else
if(max == arr_count[dex] )
numb++;
int *out_out = new int [numb];
int i = 0;

//将出现次数最多的赋值给另外一个数组。
for(dex = 0 ;dex < count ;dex++)
if (arr_count[dex] == max)
{
out_out[i] = arr_num[dex];
i++;
}

delete [] arr_count;
delete [] arr_num;
//对出现次数最多的数字进行排序 
sort(out_out,out_out+numb);
//自己检验下  
for(int jj =0 ;jj<numb;jj++)
    cout<<out_out[jj]<<"   "<<jj<<endl;
return out_out;

}

int main()
{
int num[] = {1,1,3,4,4,4,9,9,9,10}, len = 10;
int * out = calcTimes(num,len);
//这边该如何 sizeof (out) 是一个指针的长度,它不能用来判断数组的个数
int n = sizeof(out)/sizeof(int);
for(int i =0 ;i<n ;i++)
cout<<out[i]<<ends;
cout<<endl;

}

最后输出的时候,出现问题,不知道是最后sizeof(out)/size(int)出错,还是没有按照要求pbulic static int [] calcTimes()写,我对这个public static 不太了解 还是这本来就是java中的,不是C++的?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
java参数传递一道简单面试题
下面代码输出的结果: class Value{ public int i=15; } public class Test{ public static void main(String argv[]){ Test t=new Test( ); t.first( ); } public void first( ){ int i
android开发程序
public final class R { public static final class attr { } public static final class color { public static final int blue=0x7f040001; public static final int btb=0x7f040009; public static final int green=0x7f040006; public static final int hese=0x7f040008; public static final int lightblue=0x7f040004; public static final int lightpink=0x7f040002; public static final int pink=0x7f040000; public static final int purple=0x7f040005; public static final int white=0x7f040007; public static final int yellow=0x7f040003; }
88个Android应用程序.zip
public final class R { public static final class attr { } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int editText=0x7f050002; public static final int getBtn=0x7f050000; public static final int postBtn=0x7f050001; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } }
Java中的陷阱题
1、找奇数:public static boolean isOdd(int i){ return i % 2 == 1; }上面的方法真的能找到所有的奇数么?A:没有考虑到负数问题,如果i为负则不正确。应该return i%2 == 02、浮点数相减System.out.println(2.0-1.9);A:Java中的简单浮点数类型float和double不能够进行运
c#小数化最简分数算法
c#小数化最简分数算法: //小数化分数主函数 public static string XXtoBL(decimal XX) //求最大公约数的函数 public static int MaxY(int firstNumber, int secondNumber)
ANDROID 黑白棋
ANDROID黑白棋的代碼 /* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package com.game.blackwhite; public final class R { public static final class attr { } public static final class drawable { public static final int black=0x7f020000; public static final int empty=0x7f020001; public static final int icon=0x7f020002; public static final int white=0x7f020003; } public static final class id { public static final int View01=0x7f050001; public static final int text=0x7f050000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; public static final int my_turn=0x7f040003; public static final int thinking=0x7f040004; public static final int your_turn=0x7f040002; } }
java面试题一
企业Java面试题整理(200道附详细答案) Java 1+2+3……+100构建一个函数。(易) 答案:     public int Sum(int n){     if(n == 1)     return 1;     else     return n + Sum(n – 1);     }   描述冒泡算法。(易) 答案:
解缩文件 zipFile支持中文
使用Java自带的类 java.util.zip进行文件/目录的压缩的话,有一点不足,不支持中文的名件/目录命名,如果有中文名,那么打包就会失败。本人经过一段时间的摸索和实践,发现在一般的Ant.jar类库中也有zip类,import org.apache.tools.zip.*,能够解决不支持中文文件目录的问题,同时,Ant的获得途径也比较多,一般的应用服务器中有这个包,实在不行去下载个tomc
一道逻辑推理题的程序实现(纯属娱乐)
http://t1174779123.iteye.com/blog/2008357
关于Java中public static final int 用法的解答
public class FinalOriginalTest {       private final int a;       private String name;       public FinalOriginalTest(){           a = 3;        }       public FinalOriginalTest(String name){