The problem
Two pink beads are positioned between each two blue beads. There are N blue beads. After wanting on the association beneath work out the variety of pink beads.
@ @@ @ @@ @ @@ @ @@ @ @@ @
Implement count_red_beads(n)
(countRedBeads(n)
) in order that it returns the variety of pink beads.
If there are lower than 2 blue beads return 0.
The answer in C
Choice 1:
int countRedBeads(n) {
if(n<=1) return 0;
return 2*(n-1);
}
Choice 2:
int countRedBeads(n) {
if (n < 2) {
return 0;
} else {
return n + (n-2);
}
}
Choice 3:
int countRedBeads(n)
{
return n<2 ? 0 : 2*n-2;
}
Check circumstances to validate our answer
#embody <criterion/criterion.h>
int countRedBeads (int n);
Check(sample_tests, should_pass_all_the_tests_provided)
{
cr_assert_eq(countRedBeads(0), 0);
cr_assert_eq(countRedBeads(1), 0);
cr_assert_eq(countRedBeads(3), 4);
cr_assert_eq(countRedBeads(5), 8);
}