zoukankan      html  css  js  c++  java
  • 存在一个升序的整形数组,里面的元素不重复,然后找出其中所有现需的数字区间

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <vector>
    using namespace std;
    
    int main()
    {
        char str[10010];
        vector<int> num;
        gets(str);
        char *ptr;
        int a,b;
        ptr=strtok(str, " ");
        //cout << ptr << endl;
        a=atoi(ptr);
        num.push_back(a);
        //cout << '['<<a;
        while(ptr!=NULL)
        {
            ptr = strtok(NULL, " ");
            if(ptr!=NULL)
            {
                a=atoi(ptr);
                num.push_back(a);
            }
        }
        int i=0;
        vector<int>::iterator it = num.begin();
        while(it != num.end())
        {
            //cout << num[i] << " ";
            if(it==num.begin()&&*(it+1)-*(it)!=1)
            {
                num.erase(it);
            }
            else if(it==num.end()&&*it-*(it-1)!=1)
            {
                num.erase(it);
                break;
            }
            else if(*it-*(it-1)!=1&&*(it+1)-*it!=1)
            {
                num.erase(it);
            }
            else
            {
                it++;
            }
        }
    //    for(int i=0; i<num.size(); i++)
    //    {
    //        cout << num[i] << " ";
    //    }
    //    cout << endl;
        it = num.begin();
        cout << '['<<*it << "->";
        it++;
        while(it != num.end())
        {
            if(*it-*(it-1)!=1)
            {
                cout << *(it-1);
                if(it!=num.end()-1)
                {
                    cout << ',' << *it << "->";
    
                }
            }
            it++;
    
        }
        cout <<*(it-1) << "]" << endl;
        return 0;
    }

    只过了9%,说实话没找到问题出在哪儿,大概率是不连续的单个数字的输出方式有问题,例如有 1 2 5 7 8几个数字

    现在的方式是输出连续区间[1->2,7->8], 难道是要输出[1->2,5->5,7->8]或者[1->2,5,,7->8].经验证反正[1->2,5->5,7->8]也不对

  • 相关阅读:
    Python多进程编程
    Cython学习
    cProfile——Python性能分析工具
    Python垃圾回收机制:gc模块
    新纪元
    类模版的static成员
    我的2015plan
    Linux之sed
    getenv, _wgetenv
    vs2010下如何调试带输入参数的程序
  • 原文地址:https://www.cnblogs.com/vactor/p/9800657.html
Copyright © 2011-2022 走看看