剑指offer 和为S的连续正数列

2021-08-18 14:38:30 浏览数 (1)

题目描述

小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!

输出描述:

输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序

解题思路

代码

代码语言:javascript复制
class Solution {
public:
 void getfactor(int sum,vector<vector<int> > &result){
        int temp=sum/2;

        while(temp!=0){
            if(sum%temp==0){
                //cout<
                vector<int> pair;
                pair.push_back(temp);
                pair.push_back(sum/temp);
                result.push_back(pair);
            }else{
                while(temp!=1&&sum%temp!=0){
                    temp--;
                }
                vector<int> pair;
                pair.push_back(temp);
                pair.push_back(sum/temp);
                result.push_back(pair);
            }
            temp=temp/2;
        }

        return ;
  }
  vector<int> getseq1(int length,int middle){
    vector<int> seq1;
      //形式1  a-1,a,a 1  
      if(length%2==0) return seq1;
      cout<<"length:"<", middle"<if((length/2)for(int i=middle-(length/2);i<=middle (length/2);i  ){
            cout<<"pushing back:"<return seq1;
}
vector<int> getseq2(int length,int middle){
    vector<int> seq2;
    //形式2 a-1,a,a 1,a 2 
      length=length*2;
      if(middle%2==0) return seq2;
      int smaller=middle/2;
      //检验是否会扩展到负数
      cout<<"seq2 length: "<" middle:"<if(smaller>=length/2){
          for(int i=smaller-(length/2) 1;i<=smaller (length/2);i  ){
              seq2.push_back(i);
          }
      }
      return seq2;
}

   vector<vector<int> > FindContinuousSequence(int sum) {
        vector<vector<int> > factors;
        vector<vector<int> > result;
        getfactor(sum,factors);

        cout<<"size of factor vectors:"<for(vector<vector<int> >::iterator factoriter =factors.begin();factoriter!=factors.end();factoriter  ){
            vector<int> factor=*factoriter;
            vector<int > tmp1=getseq1(factor[0],factor[1]);
            if(tmp1.size()>1) result.push_back(tmp1) ;

            vector<int > tmp12=getseq1(factor[1],factor[0]);
            if(tmp12.size()>1) result.push_back(tmp12) ;

            vector<int> tmp2=getseq2(factor[1],factor[0]);
            if (tmp2.size()>1) result.push_back(tmp2);

            vector<int> tmp22=getseq2(factor[0],factor[1]);
            if (tmp22.size()>1) result.push_back(tmp22);

        }
        //对结果按照开始数字从大到小排序,selection sort
        int resultlen=result.size();
        cout<<"resultlen: "<if(resultlen>0){
            for(int i=0;i1;i  ){
                int minfront=i;
                for(int j=i 1;jif(result[j][0]==result[minfront][0]){
                        result.erase(result.begin() j);
                        resultlen--;
                        j--;
                    }                   
                    if(result[j][0]0]){
                        minfront=j;
                    }

                }
                vector<int> temp=result[i];
                result[i]=result[minfront];
                result[minfront]=temp;
            }
        }
        return  result;
    }

};

0 人点赞