| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1511 人关注过本帖
标题:怎么找出一个一维数组中有几个对子?
只看楼主 加入收藏
chen181
该用户已被删除
收藏
 问题点数:0 回复次数:20 
怎么找出一个一维数组中有几个对子?
提示: 作者被禁止或删除 内容自动屏蔽
搜索更多相关主题的帖子: 对子 
2008-05-29 12:08
chen181
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2008-05-29 12:09
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
什么是对子?

Fight  to win  or  die...
2008-05-29 18:38
漫游者李李西
Rank: 1
等 级:新手上路
帖 子:110
专家分:0
注 册:2007-11-11
收藏
得分:0 
可以设置一个栈。用栈记录

2008-05-30 12:44
漫游者李李西
Rank: 1
等 级:新手上路
帖 子:110
专家分:0
注 册:2007-11-11
收藏
得分:0 
想复杂了,看看这个。
#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;
}

2008-05-30 14:24
chen181
该用户已被删除
收藏
得分:0 
错的!~
提示: 作者被禁止或删除 内容自动屏蔽
2008-05-30 17:24
chen181
该用户已被删除
收藏
得分:0 
什么是对子·~
提示: 作者被禁止或删除 内容自动屏蔽
2008-05-30 17:26
漫游者李李西
Rank: 1
等 级:新手上路
帖 子:110
专家分:0
注 册:2007-11-11
收藏
得分:0 
哦,是我理解错了,再写一个看看:
#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;
}

2008-05-30 21:30
zzy840208
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2008-4-16
收藏
得分: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+=1;

        i=j;
    }

    cout<<"The number of pair of the vector:"<<count<<endl;
    
    return 0;
}
2008-05-30 22:07
zzy840208
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2008-4-16
收藏
得分: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;
}
2008-05-30 22:22
快速回复:怎么找出一个一维数组中有几个对子?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012373 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved