怎么找出一个一维数组中有几个对子?
怎么找出一个一维数组中有几个对子?比如说:a[ ] ={1,2,2,3,3,4,2} 请写出完整 的C++程序!先谢了哈 什么是对子? 可以设置一个栈。用栈记录 想复杂了,看看这个。
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int total=0;
int a[7]={1,2,2,3,3,4,3};
for(int i=0;i<6;i++)
{
for(int j=i+1;j<7;j++)
{
if(a[i]==a[j])
{
total++;
}
}
}
cout << "The total is "<< total <<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
错的!~
这个程序我以前就用过了,肯定是错的!比如说这样一个数组 int a[7]={2,2,2,3,3,4,3};你的程序就是错的哈!不能正确的算出对子有几个!四个相同的数算作两对哈!三个相同的算一对噻!就想打牌里面的对子!什么是对子·~
对子就想两个相同的数啊!四个相同的数算两对,三个相同的数就只能算一对哈! 哦,是我理解错了,再写一个看看:#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int total=0;
int a[7]={1,2,2,3,3,4,3};
int x;
for(int i=0;i<6;i++)
{
for(int j=i+1;j<7;j++)
{
if(a[i]>a[j])
{
x=a[i];
a[i]=a[j];
a[j]=x;
}
}
}
for(int i=0;i<7;i++)
{
if(a[i]==a[i+1])
{
total++;
i++;
}
}
cout << "The total is "<< total <<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
我试试!
//------------------------------------------------------------------------------------// 解答:怎么找出一个一维数组中有几个对子?
// 比如说:a[ ] ={1,2,2,3,3,4,2}
// ---zzy
// 2008.5.30
//-----------------------------------------------------------------------------------
#include <iostream>
#include <vector>
using namespace std;
int main()
{
cout<<"Please input data of int and finished by any characters:"<<endl;
vector<int> ivec;
int temp;
while(cin>>temp)
ivec.push_back(temp);
int i=0,count=0;
int n=ivec.size();
vector<int>::iterator iter=ivec.begin();
while(i<n)
{
for(int j=i+1;j<n;++j)
{
if(iter[j]!=iter[i])
break;
}
if((j-i)>1)
count+=1;
i=j;
}
cout<<"The number of pair of the vector:"<<count<<endl;
return 0;
}
刚理解错误,应该是这样的!
//------------------------------------------------------------------------------------// 解答:怎么找出一个一维数组中有几个对子?
// 比如说:a[ ] ={1,2,2,3,3,4,2}
// ---zzy
// 2008.5.30
//-----------------------------------------------------------------------------------
#include <iostream>
#include <vector>
using namespace std;
int main()
{
cout<<"Please input data of int and finished by any characters:"<<endl;
vector<int> ivec;
int temp;
while(cin>>temp)
ivec.push_back(temp);
int i=0,count=0;
int n=ivec.size();
vector<int>::iterator iter=ivec.begin();
while(i<n)
{
for(int j=i+1;j<n;++j)
{
if(iter[j]!=iter[i])
break;
}
if((j-i)>1)
count+=int((j-i)/2);
i=j;
}
cout<<"The number of pair of the vector:"<<count<<endl;
return 0;
} 用vector<pair<int,int> >做好了。。前面等后面就加。。同时也比较适合pair的定义 或者2维数组也不错 用普通的穷举时间在O(n^2);用统计记数时间在O(n)... 如果是对了,看情况了。。都是小对子还好了。。如果来个超级大的对子。。数组解决也很麻烦的 我的.net终于快装好了......重装系统带来的惨痛代价[tk01] #include<iostream>
#include<vector>
#include<utility>
#include<iterator>
using namespace std;
int main()
{
vector<pair<int,int> > vp;
istream_iterator<int> p(cin),eof;
while(p!=eof)
{ pair<int,int> pl;
int flag=0;
for(vector<pair<int,int> >::iterator it=vp.begin();it!=vp.end();it++)
{
if((*it).first==*p)
{
(*it).second++;
flag=1;
break;
}
}
if(flag==0)
{ pl.first=*p;
pl.second=1;
vp.push_back(pl);
}
p++;
}
for(vector<pair<int,int> >::iterator it1=vp.begin();it1!=vp.end();it1++)
{
cout<<(*it1).first<<" "<<(*it1).second<<endl;
}
return 0;
}
不理解啊!
上面这个程序是不是应用了模板哦?但是我还没有学到那里来!可以具体解释一下吗?谢谢了哈!这个程序是错的哈!
这个程序根本就没法找出有几对!不解啊! 我只是把重复的数都找出来了。。你可以把统计的数除2n能不能具体讲解一下嘛?
麻烦仔细讲解一下嘛。不胜感激哈!页:
[1]
2
