loop with bigNo
Nov 25, 2014 at 12:17pm UTC
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; }
Nov 25, 2014 at 1:02pm UTC
Well, now - those horses are really running wild, maybe even off the track!
Nov 25, 2014 at 1:08pm UTC
quickest horse goes destination offtrack
now you cant make it shorter :)
Nov 25, 2014 at 1:10pm UTC
please don't try in 1 line
Nov 25, 2014 at 1:18pm UTC
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 Nov 25, 2014 at 1:30pm UTC
Nov 25, 2014 at 2:16pm UTC
header file lines must be counted..
Nov 25, 2014 at 2:19pm UTC
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 Nov 25, 2014 at 2:53pm UTC
Nov 25, 2014 at 2:48pm UTC
good work with last one. can save few more bytes by removing return 0;
Nov 25, 2014 at 2:55pm UTC
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 Nov 25, 2014 at 3:53pm UTC
Topic archived. No new replies allowed.