c++ 求解 编程高手

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++ 求解 编程高手相关的知识,希望对你有一定的参考价值。

In statistics, the mode of a set of values is the value that occurs most often or with the greatest frequency. Write a function that accepts as arguments the following:
A) An array of integers
B) An integer that indicates the number of elements in the array
The function should determine the mode of the array. That is, it should determine which value in the array occurs most often. The mode is the value the function should return. If the array has no mode (none of the values occur more than once), the function should return -1. (Assume the array will always contain nonnegative values.)
Demonstrate your pointer prowess by using pointer notation instead of array notation in this function.

//设计上可能有点局限
#if 1
//In statistics, the mode of a set of values is the value that occurs most often or with the greatest frequency. Write a function that accepts as arguments the following:
// A) An array of integers
// B) An integer that indicates the number of elements in the array
// The function should determine the mode of the array. That is, it should determine which value in the array occurs most often. The mode is the value the function should return. If the array has no mode (none of the values occur more than once), the function should return -1. (Assume the array will always contain nonnegative values.)
// Demonstrate your pointer prowess by using pointer notation instead of array notation in this function.
#include<iostream>
#include<ctime>
const int ArSize = 10;
void createArr(int * arr,int size);/* 创建一个随机数组 */
void printArr(const int * arr,int size);/* 打印数组 */
int mode(const int * arr,int size,int * maxCount);
int main()

using namespace std;
int * arr = new int[ArSize];//申请空间
createArr(arr,ArSize);
printArr(arr,ArSize);
int maxCount=0;
int modei = mode(arr,ArSize,&maxCount);
cout<<"the value:"<<modei<<" occurs most often,total "<<maxCount<<" times\n";
delete [] arr;//释放空间
return 0;

void createArr( int * arr,int size )

int i;
srand((unsigned int)time(0));//真随机数
for (i=0;i<size;i++)

*(arr+i) = rand()%size;


void printArr(const int * arr,int size )

printf("arr = [");
for (int i=0;i<size-1;i++)

printf("%2d,",*(arr+i));
if ((i+1)%10==0)

printf("\n");

if((i+1)%10==0&&(i+1)/10>0)

printf(" ");


printf("%d]\n",arr[size-1]);


int mode(const int * arr,int size, int * maxCount)

int * temp = new int[size];//假设100个值各出现一次,用索引表示arr中的值,用temp元素值表示出现的次数
for (int k=0;k<size;k++)

temp[k]=0;//数组初始化

for (int i=0;i<size;i++)

temp[arr[i]]++;

printArr(temp,ArSize);
//#define TEST
#ifdef TEST
using namespace std;
int finds = 0;
for (int u=0;u<size;u++)

cout<<u<<" find "<<temp[u]<<endl;
finds+=temp[u];

cout<<"finds===="<<finds<<endl;
#endif
//然后找到数组中最大的值
*maxCount=temp[0];
int value = -1;
value = temp[0]>1?0:-1;
for (int j=0;j<size;j++)

if (temp[j]>(*maxCount)&&temp[j]>1)

(*maxCount) = temp[j];
value = j;


delete temp;
return value;


#endif追问

void reset_timerr ()

driven_times=0;

类中 初始化这个 对象 可以这样写么

参考技术A #include<iostream>
using namespace std;
int Mode(int n,int *p)

int position,count=1,countx=1;
int *next;
int *first=p;
for(int i=0;i<n-1;i++)

p=first+i;
next=p+1;
for(int j=i+1;j<n;j++)


if(*p==*next)
countx++;
if(countx>count)

count=countx;
position=i;

next++;

countx=1;

if(1==count)
return -1;
else
return p[position];

int main()

int n;
cin>>n;
int *a=new int[n];
for(int i=0;i<n;i++)
cin>>a[i];
cout<<Mode(n,a)<<endl;
return 0;


//看下对不对。追问

能给下你QQ么

追答

我QQ就是我名字咯。

参考技术B #include <cstdlib>
#include <iostream>

using namespace std;

int modeFinder(int*,int size);

int main(int argc, char *argv[])

int sizeOfArray = 1;
int mode = -1;
cin>>sizeOfArray;
int array[sizeOfArray];
for(int i=0;i!=sizeOfArray;i++)
cin>>array[i];
mode = modeFinder(array,sizeOfArray);
if(mode == -1)
cout<<"mode of the array is not existed"<<endl;
else
cout<<mode<<endl;
while(getchar()=='\n');
return 0;


int modeFinder(int* ptr,int size)

int freq = 0;
int mode = -1;
for(int i=0; i!=size;i++)

int tmp_freq = 0;
for(int j=0;j!=size;j++)

if(*(ptr+i) == *(ptr+j) && i!=j)
tmp_freq++;

if(tmp_freq > freq)

freq = tmp_freq;
mode = *(ptr+i);


return mode;


the array name is exactly the pointer to the array, that is, var array is the pointer of array[n];
you can use both array[i] and *(array + i) to access ith value in array[n];
hope my answer helps :-)
参考技术C 汗。代码看懂了,E文看不懂。 参考技术D 学习了

以上是关于c++ 求解 编程高手的主要内容,如果未能解决你的问题,请参考以下文章

关于MATLAB自己编程求解特征值的问题?(比如QR法,幂法,牙可比跌代法,等)请教高手

编程高手请进~~~~

如何成为编程高手

用VC编程时运行提示出错Microsoft Visual C++ Runtime Library Assertion failed!。 求高手解决啊!

第1课 - 进阶高手的大门

C++编程如何保留小数点后6位 求高手精解.....这里提前表示感谢了!!!