Upside-down numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Wren}}: Changed to Wren S/H)
No edit summary
Line 221: Line 221:
494616
494616
56546545
56546545
</pre>

=={{header|C++}}==
<syntaxhighlight lang="C++">#include <iostream>
#include <vector>
#include <algorithm>

bool isUpsideDown( int n ) {
std::vector<int> digits ;
while ( n != 0 ) {
digits.push_back( n % 10 ) ;
n /= 10 ;
}
if ( std::find ( digits.begin( ) , digits.end( ) , 0 ) != digits.end( ) )
return false ;
int forward = 0 ;
int backward = digits.size( ) - 1 ;
while ( forward <= backward ) {
if ( digits[forward] + digits[backward] != 10 )
return false ;
forward++ ;
if ( backward > 0 ) {
backward-- ;
}
}
return true ;
}

int main( ) {
int current = 0 ;
int sum = 0 ;
std::vector<int> solution ;
while ( sum != 5000 ) {
current++ ;
if ( isUpsideDown( current ) ) {
solution.push_back( current ) ;
sum++ ;
}
}
std::cout << "The first 50 upside-down numbers:\n" ;
std::cout << "(" ;
for ( int i = 0 ; i < 50 ; i++ )
std::cout << solution[ i ] << ' ' ;
std::cout << ")\n" ;
std::cout << "The five hundredth such number: " << solution[499] << '\n' ;
std::cout << "The five thousandth such number: " << solution[4999] << '\n' ;
return 0 ;
}</syntaxhighlight>
{{out}}
<pre>
The first 50 upside-down numbers:
(5 19 28 37 46 55 64 73 82 91 159 258 357 456 555 654 753 852 951 1199 1289 1379 1469 1559 1649 1739 1829 1919 2198 2288 2378 2468 2558 2648 2738 2828 2918 3197 3287 3377 3467 3557 3647 3737 3827 3917 4196 4286 4376 4466 )
The five hundredth such number: 494616
The five thousandth such number: 56546545
</pre>
</pre>