注册 登录
编程论坛 C++教室

怎么找出一个一维数组中有几个对子?

发布于 2008-05-29 12:08, 1511 次点击
怎么找出一个一维数组中有几个对子?
比如说:a[ ] ={1,2,2,3,3,4,2}
20 回复
#2
2008-05-29 12:09
请写出完整 的C++程序!先谢了哈
#3
aipb20072008-05-29 18:38
什么是对子?
#4
漫游者李李西2008-05-30 12:44
可以设置一个栈。用栈记录
#5
漫游者李李西2008-05-30 14:24
想复杂了,看看这个。
#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;
}
#6
2008-05-30 17:24
错的!~
这个程序我以前就用过了,肯定是错的!比如说这样一个数组 int a[7]={2,2,2,3,3,4,3};你的程序就是错的哈!不能正确的算出对子有几个!四个相同的数算作两对哈!三个相同的算一对噻!就想打牌里面的对子!
#7
2008-05-30 17:26
什么是对子·~
对子就想两个相同的数啊!四个相同的数算两对,三个相同的数就只能算一对哈!
#8
漫游者李李西2008-05-30 21:30
哦,是我理解错了,再写一个看看:
#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;
}
#9
zzy8402082008-05-30 22:07
我试试!
//------------------------------------------------------------------------------------
//   解答:怎么找出一个一维数组中有几个对子?
//        比如说: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;
}
#10
zzy8402082008-05-30 22:22
刚理解错误,应该是这样的!
//------------------------------------------------------------------------------------
//   解答:怎么找出一个一维数组中有几个对子?
//        比如说: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;
}
#11
sunkaidong2008-05-30 22:43
用vector<pair<int,int> >做好了。。前面等后面就加。。同时也比较适合pair的定义
#12
sunkaidong2008-05-30 22:48
或者2维数组也不错
#13
中学者2008-05-30 22:49
用普通的穷举时间在O(n^2);用统计记数时间在O(n)...
#14
sunkaidong2008-05-30 22:54
如果是对了,看情况了。。都是小对子还好了。。如果来个超级大的对子。。数组解决也很麻烦的
#15
中学者2008-05-30 22:57
我的.net终于快装好了......重装系统带来的惨痛代价
#16
sunkaidong2008-05-31 09:32
#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;
}
#17
2008-05-31 22:14
不理解啊!
上面这个程序是不是应用了模板哦?但是我还没有学到那里来!可以具体解释一下吗?谢谢了哈!
#18
2008-05-31 22:17
这个程序是错的哈!
这个程序根本就没法找出有几对!不解啊!
#19
sunkaidong2008-05-31 22:19
我只是把重复的数都找出来了。。你可以把统计的数除2
#20
2008-05-31 22:20
n能不能具体讲解一下嘛?
麻烦仔细讲解一下嘛。不胜感激哈!
#21
sunkaidong2008-05-31 22:23
两个模板。。一个是vector和pair。。你学到就明白了要输入ctrl+z结束。如果不能结束就在输入一次。。只要结束就好了

[[it] 本帖最后由 sunkaidong 于 2008-5-31 22:54 编辑 [/it]]
1