aboutsummaryrefslogtreecommitdiff
path: root/challenge-226/ulrich-rieke/cpp
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-07-31 15:49:22 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-07-31 15:49:22 +0800
commite7b6313261ef4541d4dcc303c46ef0d886649b70 (patch)
tree8cff819a2036dd8d0172b6343b0a199d7b81721a /challenge-226/ulrich-rieke/cpp
parent4fda4a4a398e64921020704733556a2ec6dae78a (diff)
parente511966ce2280dbedb2c916d9e6254708800639e (diff)
downloadperlweeklychallenge-club-e7b6313261ef4541d4dcc303c46ef0d886649b70.tar.gz
perlweeklychallenge-club-e7b6313261ef4541d4dcc303c46ef0d886649b70.tar.bz2
perlweeklychallenge-club-e7b6313261ef4541d4dcc303c46ef0d886649b70.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-226/ulrich-rieke/cpp')
-rwxr-xr-xchallenge-226/ulrich-rieke/cpp/ch-1.cpp43
-rwxr-xr-xchallenge-226/ulrich-rieke/cpp/ch-2.cpp48
2 files changed, 91 insertions, 0 deletions
diff --git a/challenge-226/ulrich-rieke/cpp/ch-1.cpp b/challenge-226/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..6b0c8c12bb
--- /dev/null
+++ b/challenge-226/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,43 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <utility>
+#include <algorithm>
+
+std::vector<std::string> split( const std::string & startline ,
+ const std::string & sep ) {
+ std::vector<std::string> separated ;
+ std::string::size_type start { 0 } ;
+ std::string::size_type pos ;
+ do {
+ pos = startline.find_first_of( sep , start ) ;
+ separated.push_back( startline.substr(start , pos - start )) ;
+ start = pos + 1 ;
+ } while ( pos != std::string::npos ) ;
+ return separated ;
+}
+
+int main( ) {
+ std::cout << "Enter a string!\n" ;
+ std::string str ;
+ std::getline( std::cin , str ) ;
+ std::cout << "Enter indices into the string!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> numberstrings( split( line, " " ) ) ;
+ std::vector<int> numbers ;
+ for ( auto s : numberstrings )
+ numbers.push_back( std::stoi( s ) ) ;
+ std::vector<std::pair<int, char>> allPairs ;
+ for ( int i = 0 ; i < numberstrings.size( ) ; i++ ) {
+ allPairs.push_back( std::make_pair( numbers[ i ] , str[ i ] ) ) ;
+ }
+ std::sort( allPairs.begin( ) , allPairs.end( ) , []( const auto & p1 ,
+ const auto & p2 ) { return p1.first < p2.first ; } ) ;
+ std::string solution ;
+ for ( const auto p : allPairs )
+ solution += p.second ;
+ std::cout << solution << std::endl ;
+ return 0 ;
+}
+
diff --git a/challenge-226/ulrich-rieke/cpp/ch-2.cpp b/challenge-226/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..d80e8da2a5
--- /dev/null
+++ b/challenge-226/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,48 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <algorithm>
+
+std::vector<std::string> split( const std::string & startline ,
+ const std::string & sep ) {
+ std::vector<std::string> separated ;
+ std::string::size_type start { 0 } ;
+ std::string::size_type pos ;
+ do {
+ pos = startline.find_first_of( sep , start ) ;
+ separated.push_back( startline.substr(start , pos - start )) ;
+ start = pos + 1 ;
+ } while ( pos != std::string::npos ) ;
+ return separated ;
+}
+
+int mySubtr( int n , int min ) {
+ n -= min ;
+ if ( n < 0 )
+ n = 0 ;
+ return n ;
+}
+
+int main( ) {
+ std::cout << "Enter some positive integers, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> numberstrings ( split ( line , " " ) ) ;
+ std::vector<int> numbers ;
+ for ( auto s : numberstrings ) {
+ numbers.push_back( std::stoi( s ) ) ;
+ }
+ int rounds = 0 ;
+ while ( ! std::all_of( numbers.begin( ) , numbers.end( ) , []( int n ) {
+ return n == 0 ; } ) ) {
+ std::vector<int> positives ;
+ std::copy_if( numbers.begin( ) , numbers.end( ) , std::back_inserter(
+ positives ) , []( int i ) { return i > 0 ; } ) ;
+ int mini = *std::min_element( positives.begin( ) , positives.end( ) ) ;
+ rounds++ ;
+ std::transform( numbers.begin( ) , numbers.end( ) , numbers.begin( ) ,
+ [mini]( int i ) { return mySubtr( i , mini ) ; } ) ;
+ }
+ std::cout << rounds << std::endl ;
+ return 0 ;
+}