diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2021-06-16 21:13:20 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2021-06-16 21:13:20 +0800 |
| commit | 5e631f4b325d6ef41562ec1ef955e49e7da4ab75 (patch) | |
| tree | 77cd0ba501ebc5d0c0fcb36764e55c98b4869b3c /challenge-116/ulrich-rieke/cpp | |
| parent | 1dd7eedea237292a3fb563ecff37d004c3bbc772 (diff) | |
| parent | 0aa46d7e327eb82bd447279a1891f0d1873c46ff (diff) | |
| download | perlweeklychallenge-club-5e631f4b325d6ef41562ec1ef955e49e7da4ab75.tar.gz perlweeklychallenge-club-5e631f4b325d6ef41562ec1ef955e49e7da4ab75.tar.bz2 perlweeklychallenge-club-5e631f4b325d6ef41562ec1ef955e49e7da4ab75.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-116/ulrich-rieke/cpp')
| -rw-r--r-- | challenge-116/ulrich-rieke/cpp/ch-2.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-116/ulrich-rieke/cpp/ch-2.cpp b/challenge-116/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..8073680d19 --- /dev/null +++ b/challenge-116/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include <vector> +#include <algorithm> +#include <numeric> +#include <cstdlib> +#include <cmath> + +int main( int argc , char * argv[] ) { + if ( argc != 2 ) { + std::cerr << "Usage: challenge116_2 <number>!\n" ; + return 1 ; + } + int n = std::atoi( argv[ 1 ] ) ; + if ( n < 10 ) { + std::cerr << "Number must be greater than 9!\n" ; + return 2 ; + } + std::vector<int> digits ; + while ( n != 0 ) { + digits.push_back( n % 10 ) ; + n /= 10 ; + } + std::transform( digits.begin( ) , digits.end( ) , digits.begin( ) , + []( int a ) { return a * a ; } ) ; + int squaresum = std::accumulate( digits.begin( ) , digits.end( ) , 0 ) ; + double theRoot = std::sqrt( static_cast<double>( squaresum ) ) ; + if ( std::floor( theRoot ) == theRoot ) + std::cout << 1 << std::endl ; + else + std::cout << 0 << std::endl ; + return 0 ; +} |
