aboutsummaryrefslogtreecommitdiff
path: root/challenge-080/adam-russell/cpp/ch-2.cpp
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2020-10-10 18:34:15 +0800
committer冯昶 <seaker@qq.com>2020-10-10 18:34:15 +0800
commitf0a88ea2365d8ae7dfb90c969d83368a18b53f9a (patch)
tree64665b115f1023519b48f3f366bb743ab68375d8 /challenge-080/adam-russell/cpp/ch-2.cpp
parent353a66fe9f75298e44b27d692109259646e7d09f (diff)
parenta8ea1576ec8f1801e4c90d906c5d3c18ebde5ca6 (diff)
downloadperlweeklychallenge-club-f0a88ea2365d8ae7dfb90c969d83368a18b53f9a.tar.gz
perlweeklychallenge-club-f0a88ea2365d8ae7dfb90c969d83368a18b53f9a.tar.bz2
perlweeklychallenge-club-f0a88ea2365d8ae7dfb90c969d83368a18b53f9a.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-080/adam-russell/cpp/ch-2.cpp')
-rw-r--r--challenge-080/adam-russell/cpp/ch-2.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-080/adam-russell/cpp/ch-2.cpp b/challenge-080/adam-russell/cpp/ch-2.cpp
new file mode 100644
index 0000000000..f3e26491c9
--- /dev/null
+++ b/challenge-080/adam-russell/cpp/ch-2.cpp
@@ -0,0 +1,30 @@
+#include <iostream>
+/*
+* You are given rankings of @N candidates.
+* Write a script to find out the total candies needed for all candidates.
+* You are asked to follow the rules below:
+* a) You must given at least one candy to each candidate.
+* b) Candidate with higher ranking get more candies than their immediate
+* neighbors on either side.
+*/
+int count_candies(int candidates[]){
+ int candies = 4;
+ for(int i = 0; i < 3; i++){
+ if((i - 1) >= 0){
+ if(candidates[i] > candidates[i - 1]){
+ candies++;
+ }
+ }
+ if((i + 1) < 4){
+ if(candidates[i] > candidates[i + 1]){
+ candies++;
+ }
+ }
+ }
+ return candies;
+}
+int main(int argc, char** argv){
+ int N[4] = {1, 4, 3, 2};
+ int i = count_candies(N);
+ std::cout << "the number of candies is " << i << std::endl;
+} \ No newline at end of file