编程论坛's Archiver

chen181 发表于 2008-5-29 12:08

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

怎么找出一个一维数组中有几个对子?
比如说:a[ ] ={1,2,2,3,3,4,2}

chen181 发表于 2008-5-29 12:09

请写出完整 的C++程序!先谢了哈

aipb2007 发表于 2008-5-29 18:38

什么是对子?

漫游者李李西 发表于 2008-5-30 12:44

可以设置一个栈。用栈记录

漫游者李李西 发表于 2008-5-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;
}

chen181 发表于 2008-5-30 17:24

错的!~

这个程序我以前就用过了,肯定是错的!比如说这样一个数组 int a[7]={2,2,2,3,3,4,3};你的程序就是错的哈!不能正确的算出对子有几个!四个相同的数算作两对哈!三个相同的算一对噻!就想打牌里面的对子!

chen181 发表于 2008-5-30 17:26

什么是对子·~

对子就想两个相同的数啊!四个相同的数算两对,三个相同的数就只能算一对哈!

漫游者李李西 发表于 2008-5-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;
}

zzy840208 发表于 2008-5-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;
}

zzy840208 发表于 2008-5-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;
}

sunkaidong 发表于 2008-5-30 22:43

用vector<pair<int,int> >做好了。。前面等后面就加。。同时也比较适合pair的定义

sunkaidong 发表于 2008-5-30 22:48

或者2维数组也不错

中学者 发表于 2008-5-30 22:49

用普通的穷举时间在O(n^2);用统计记数时间在O(n)...

sunkaidong 发表于 2008-5-30 22:54

如果是对了,看情况了。。都是小对子还好了。。如果来个超级大的对子。。数组解决也很麻烦的

中学者 发表于 2008-5-30 22:57

我的.net终于快装好了......重装系统带来的惨痛代价[tk01]

sunkaidong 发表于 2008-5-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;
}

chen181 发表于 2008-5-31 22:14

不理解啊!

上面这个程序是不是应用了模板哦?但是我还没有学到那里来!可以具体解释一下吗?谢谢了哈!

chen181 发表于 2008-5-31 22:17

这个程序是错的哈!

这个程序根本就没法找出有几对!不解啊!

sunkaidong 发表于 2008-5-31 22:19

我只是把重复的数都找出来了。。你可以把统计的数除2

chen181 发表于 2008-5-31 22:20

n能不能具体讲解一下嘛?

麻烦仔细讲解一下嘛。不胜感激哈!

页: [1] 2

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.