| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 382 人关注过本帖
标题:十万火急----老鸟速进
只看楼主 加入收藏
morose2004
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2011-5-11
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:2 
十万火急----老鸟速进
此程序就是要判断一组数里面有没有连号,有多少连号,并输出。
程序如下:

#include <iostream>
#include<vector>
#include<fstream>
using namespace std;
int main()
{
    int a[][5]={{1,3,5,6,10},{2,4,6,7,12},{3,5,6,8,9},{4,5,6,7,13},{1,3,5,7,9}};
    vector<int>temp;
    vector<vector<int> > connect;
    int connectnum=0,count=0;
    cout<<"please enter the number you wish:";
    cin>>connectnum;
    cout<<endl;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<5;j++)
            {
                if(a[i][j+1]-a[i][j]==1)  //这里的问题
                    count+=1;
                temp.push_back(a[i][j]);
            }
        if(count==connectnum)
            connect.push_back(temp);
        temp.clear();
        count=0;
    }
    ofstream output("result.txt");
    output<<"the numbers you wanna find is:";
    for(int i=0;i<connect.size();i++)
    {
        for(int j=0;j<connect[i].size();j++)
            output<<connect[i][j]<<" ";
        output<<endl;
    }
    output.close();
    return 0;
}
这个程序能正确运行,但我总感觉有问题,问题就是我注释的地方。
当j为最大的时候,a[i][j+1]就不存在,程序应该出错,但却正常运行。
希望大家帮忙解释下。
另外,如果有其他更好的办法,帮忙给我附上,哈哈哈。谢谢。
搜索更多相关主题的帖子: using number enter include please 
2011-07-12 09:51
ligang222222
Rank: 2
等 级:论坛游民
帖 子:4
专家分:19
注 册:2011-6-24
收藏
得分:10 
#include <iostream>
#include<vector>
#include<fstream>
using namespace std;
int main()
{
    int a[][5]={{1,3,5,6,10},{2,4,6,7,12},{3,5,6,8,9},{4,5,6,7,13},{1,2,3,4,9}};
    cout<<a[0][5]<<endl; //当数组时二维数组时,它相当于a[1][0],这样不会报错(Visual c++ 6.0)
    vector<int>temp;
    vector<vector<int> > connect;
    int connectnum=0,count=0;
    cout<<"please enter the number you wish:";
    cin>>connectnum;
    cout<<endl;
    for(int i=0;i<5;i++)       //这里应该是5
    {
        for(int j=0;j<4;j++)  //这里用4就可以了,也可以避免出现a[?][5]
            {
                if(a[i][j+1]-a[i][j]==1)  //这里的问题
                    count+=1;
                temp.push_back(a[i][j]);
            }
        if(count==connectnum)
            connect.push_back(temp);
        temp.clear();
        count=0;
    }
    ofstream output("result.txt");
    output<<"the numbers you wanna find is:"<<endl;
    for(int k=0;k<connect.size();k++)
    {
        for(int j=0;j<connect[k].size();j++)
            output<<connect[k][j]<<" ";
        output<<endl;
    }
    output.close();
    return 0;
}
2011-07-12 11:14
morose2004
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2011-5-11
收藏
得分:0 
你这样验证了没有,好像也只能输出4个数,而不是5个
2011-07-12 12:06
快速回复:十万火急----老鸟速进
数据加载中...
 
   



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

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