Non-continuous subsequences: Difference between revisions

Content added Content deleted
(Added Kotlin)
(/* {{header|C++}} * maybe best code/)
Line 473: Line 473:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>
/*
* Nigel Galloway, July 19th., 2017 - Yes well is this any better?
*/
class N{
uint n,i,g,e,l;
public:
N(uint n): n(n-1),i{},g{},e(1),l(n-1){}
bool hasNext(){
g=(1<<n)+e;for(i=l;i<n;++i) g+=1<<i;
if (l==2) {l=--n; e=1; return true;}
if (e<((1<<(l-1))-1)) {++e; return true;}
e=1; --l; return (l>0);
}
uint next() {return g;}
};
</lang>
Which may be used as follows:
<lang cpp>
int main(){
N n(4);
while (n.hasNext()) std::cout << n.next() << "\t* " << std::bitset<4>(n.next()) << std::endl;
</lang>
{{out}}
<pre>
9 * 1001
10 * 1010
11 * 1011
13 * 1101
5 * 0101
</pre>
<lang cpp>/*
<lang cpp>/*
Not best code, wrote it really quick. Will add updated code using more C++11 features soon!
Not best code, wrote it really quick. Will add updated code using more C++11 features soon!