Two points (proposed to be low and high )both start to point the begining of the array,
If current subsequence sum is more than the need sum,low++;
If current subsequence sum is less than the need sum,high++;
If current subsequence sum is equal to the need sum,print the current subsequence;
At the end,if we can't find the corresponding result is equal to the need sum,just find the minimum sum(minimumMax) of the subsequence which is more than the need sum.
Then,search the subsequence which is equal to minimumMax as previously described.
// 1044. Shopping in Mars.cpp: 主项目文件。#include "stdafx.h"#includeconst int INF=0x7fffffff;const int N=100003;int diamond[N];int arrNum,needSum;bool selectChain(){ bool tag=false; int low=0,high=0,minimumMax=INF,curSum=diamond[low]; while(low <=high){ if(curSum>needSum){ if(curSum needSum){ low++,high++; curSum=diamond[low]; continue; } if(curSum==needSum){ tag=true; printf("%d-%d\n",low+1,high+1); curSum=curSum-diamond[low++]+diamond[++high]; } else if(curSum