神秘菜鸟

好吧其实一点也不神秘,但菜是真的

Codecademy C++ 练习题

大纲
  1. 1. CODE CHALLENGE: C++ FUNCTIONS
    1. 1.1. Introduction
    2. 1.2. Average
    3. 1.3. Tenth Power
    4. 1.4. First Three Multiples
    5. 1.5. Water Plant
    6. 1.6. Palindrome

CODE CHALLENGE: C++ FUNCTIONS

Introduction

1. Write a function introduction() with no return value that has:

The function should print the last_name, followed by a comma, a space, first_name another space, and finally last_name again.

For example, introduction("James", "Bond"); should print the following:

1
Bond, James Bond

Answer :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

// Define introduction() here:
void introduction(std::string first_name, std::string last_name) {

std::cout << last_name << ", " << first_name << " " << last_name << "\n";

}


int main() {

introduction("Beyonce", "Knowles");

}

Average

1. Write a function average() that takes:

The function should return a double that is the average of the arguments passed in.

Answer :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

// Define average() here:
double average(double num1, double num2) {

double total = 0;

total = (num1 + num2) / 2;

return total;

}

int main() {

std::cout << average(42.0, 24.0) << "\n";
std::cout << average(1.0, 2.0) << "\n";

}

Tenth Power

1. Write a function named tenth_power() that has:

The function should return num raised to the 10th power.

Answer :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cmath>

// Define tenth_power() here:
int tenth_power(int num) {

num = num * num * num * num * num * num * num * num * num * num;

return num;
//按理说最优解应该为return pow(num, 10); 但此时还没有学到调用其他文件的函数。

}

int main() {

std::cout << tenth_power(0) << "\n";
std::cout << tenth_power(1) << "\n";
std::cout << tenth_power(2) << "\n";

}

First Three Multiples

1. Write a function named first_three_multiples() that has:

The function should return an std::vector of the first three multiples of num in ascending order.

For example, first_three_multiples(7) should return a vector with 7, 14, and 21.

Answer :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>

// Define first_three_multiples() here:
std::vector<int> first_three_multiples(int num) {

return std::vector<int> {num, num * 2, num * 3};

}

int main() {

for (int element : first_three_multiples(8)) {
std::cout << element << "\n";
}

}

Water Plant

1. Define a function needs_water() that accepts:

Inside the function, you’ll need some conditional logic:

Note: Don’t print the strings; return them from the function.

Answer :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

// Define needs_water() here:
std::string needs_water(int days, bool is_succulent) {

if (days > 3 && is_succulent == false) {
return "Time to water the plant.";
} else if (days <= 12 && is_succulent == true) {
return "Don't water the plant!";
} else if (days >= 13 && is_succulent == true) {
return "Go ahead and give the plant a little water.";
}

}

int main() {

std::cout << needs_water(10, false) << "\n";

}

Palindrome

1. Define a function is_palindrome() that takes:

The function should return:

(A palindrome is any text that has the same characters backwards as it does forwards. For example, “hannah” and “racecar” are palindromes, while “menu” and “ardvark” are not.)

We will not test for edge cases such as capitalization or spaces.

Answer :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>

//关于获取字符串长度的方法除了用vector还可以用string的length
// Define is_palindrome() here:
bool is_palindrome(std::string text) {

std::string flips;

for (int i = text.size() - 1; i >= 0; i--) {
flips = flips + text[i];
}

return flips == text;

}

int main() {

std::cout << is_palindrome("madam") << "\n";
std::cout << is_palindrome("ada") << "\n";
std::cout << is_palindrome("lovelace") << "\n";

}

至此关于函数篇的六个小练习题完结。