利用c++实现在数组中寻找相同元素的个数

来源:百度知道 编辑:UC知道 时间:2024/06/25 16:31:20
例如:
1 4 8 7 6 5
1 4 10 10 8 9
3 5 10 5 7 8
3 9 10 5 7 8
1 2 3 4 10 7
这个二维数组中的10相连的数有4个如何编程:
我的算法如下有什么样的错误?
#include<iostream.h>
int count=1;
int put(int **str,int R,int W)
{
while(R<=5&&R>=0&&W<=6&&W>=0)
{
if(str[R][W]==str[R][W-1])
{
count++;
str[R][W]=-1;
return put(str,R,W-1);
}
if(str[R][W]==str[R][W+1])
{
count++;
str[R][W]=-1;
return put(str,R,W+1);
}
if(str[R][W]==str[R-1][W])
{
count++;
str[R][W]=-1;
return put(str,R-1,W);
}
if(str[R][W]==str[R+1][W])
{
count++;
str[R][W]=-1;
return put(str,R+1,W);
}
else
{
return count;
}
}
}

void main()
{
int **p=new int*[6];
for(int i=0;i<5;i++)
{
p[i]=new int[6];
}

你的代码很恐怖,让我失去读下去的兴趣……

简单说就是双循环,一行一行扫描~~~~
用两个for不好么?又清晰又简洁~~~~

有本书上说:在可以的情况下,尽可能用向量代替数组……
虽然在C中多维数组固然强大,强大到让人不知道哪是哪,但在C++中有必要延续么?

拜托各位不要再简单把C++当做C的升级版本了,C++中实现了很多在C中复杂的操作,加之强大的模板设计,泛型算法,到底有什么理由让我们在C++中残留如此多C痕迹……

什么意思?没懂你想做什么呢?