loop with bigNo

Pages: 12
counting down ... 10 line
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <algorithm>
int main () {
int ar[9] ={1,2,3,4,5,6,7,8,9};
do {
if( (ar[0]*10000 + ar[1]*1000 + ar[2]*100 + ar[3]*10 + ar[4]*1)/double(ar[5]*1000 + ar[6]*100 + ar[7]*10 + ar[8]*1) == 9) 
	std::cout << ar[0]*10000 + ar[1]*1000 + ar[2]*100 + ar[3]*10 + ar[4]*1 << " / " << ar[5]*1000 + ar[6]*100 + ar[7]*10 + ar[8]*1 << " = 9 \n"; 
} while ( std::next_permutation( ar, ar+9 ) );
return 0;
}



.... 3 lines!
1
2
3
#include <iostream> 
#include <algorithm> 
int main () {int ar[9] ={1,2,3,4,5,6,7,8,9}; do { if( (ar[0]*10000 + ar[1]*1000 + ar[2]*100 + ar[3]*10 + ar[4]*1)/double(ar[5]*1000 + ar[6]*100 + ar[7]*10 + ar[8]*1) == 9) 	std::cout << ar[0]*10000 + ar[1]*1000 + ar[2]*100 + ar[3]*10 + ar[4]*1 << " / " << ar[5]*1000 + ar[6]*100 + ar[7]*10 + ar[8]*1 << " = 9 \n"; } while ( std::next_permutation( ar, ar+9 ) ); return 0; }
Well, now - those horses are really running wild, maybe even off the track!
quickest horse goes destination offtrack
now you cant make it shorter :)
please don't try in 1 line
Two, then, ;0

1
2
#include "myhdr.h" // contains these: #include <iostream>  #include <algorithm> 
int main () {int ar[9] ={1,2,3,4,5,6,7,8,9}; do { if( (ar[0]*10000 + ar[1]*1000 + ar[2]*100 + ar[3]*10 + ar[4]*1)/double(ar[5]*1000 + ar[6]*100 + ar[7]*10 + ar[8]*1) == 9) 	std::cout << ar[0]*10000 + ar[1]*1000 + ar[2]*100 + ar[3]*10 + ar[4]*1 << " / " << ar[5]*1000 + ar[6]*100 + ar[7]*10 + ar[8]*1 << " = 9 \n"; } while ( std::next_permutation( ar, ar+9 ) ); return 0; }
Last edited on
header file lines must be counted..
now: 257 bytes, including standard headers

1
2
3
#include<iostream>
#include<algorithm>
int main(){int d,n,a[]={1,2,3,4,5,6,7,8,9};do{d=10*(10*(10*(a[0])+a[1])+a[2])+a[3];n=10*(10*(10*(10*(a[4])+a[5])+a[6])+a[7])+a[8];if(d*9==n)std::cout<<n<<"/"<<d<<"=9\n";}while(std::next_permutation(a,a+9));return 0;}


or 247:

1
2
3
4
#include<iostream>
#include<algorithm>
#include<string>
int main(){int n,d;std::string a="123456789";do{n=stoi(a.substr(0,5));d=stoi(a.substr(5));if(d*9==n)std::cout<<n<<"/"<<d<<"=9\n";}while(std::next_permutation(a.begin(),a.end()));return 0;}
Last edited on
good work with last one. can save few more bytes by removing return 0;
true, but we've got to draw the style consideration line somewhere! :)

Besides, I'm trying to upgrade my admittedly little knowledge of "C" from way back into a little knowledge about "C++," and I have seen that the old "C" style:

1
2
3
void main{
//do stuff;
}


is long out-of-style, and has to go away, I guess.
Last edited on
Topic archived. No new replies allowed.
Pages: 12