CodeWars系列题目(持续更新)

本文最后更新于:2024年7月25日 晚上

<8kyu> Opposites Attract

题目描述:

Timmy & Sarah think they are in love, but around where they live, they will only know once they pick a flower each. If one of the flowers has an even number of petals and the other has an odd number of petals it means they are in love.

Write a function that will take the number of petals of each flower and return true if they are in love and false if they aren’t.

三目运算符一行解:

1
2
3
bool lovefunc(int f1, int f2) {
return(((f1%2)&&!(f2%2))|(!(f1%2)&&(f2%2))) ? true : false;
}

懒死我得了……

<7kyu> String ends with?

题目描述:

Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).

基础小逻辑

1
2
3
4
5
6
7
8
#include <string>
bool solution(std::string const &str, std::string const &ending) {
unsigned int len1 = str.length(), len2 = ending.length();
for(int i = 1;i <= len2; i++) {
if(str[len1-i] != ending[len2-i]) return false;
}
return true;
}

<8kyu> Convert number to reversed array of digits

题目描述:

Convert number to reversed array of digits

Given a random non-negative number, you have to return the digits of this number within an array in reverse order.

就是一个小特判

1
2
3
4
5
6
7
8
9
10
std::vector<int> digitize(unsigned long n) 
{
std::vector<int> m;
while(n != 0) {
m.push_back(n % 10);
n /= 10;
}
if(m.size()==0) m.push_back(0);
return m;
}

<7 kyu> Find the stray number

题目描述:

You are given an odd-length array of integers, in which all of them are the same, except for one single number.

Complete the method which accepts such an array, and returns that single different number.

The input array will always be valid! (odd-length >= 3)

投机取巧

1
2
3
4
5
6
#include <algorithm>
int stray(std::vector<int> numbers) {
sort(numbers.begin(), numbers.end());
if(numbers[0] != numbers[1]) return numbers[0];
else return numbers[numbers.size()-1];
};

<8 kyu> Beginner - Reduce but Grow

题目描述:

Given a non-empty array of integers, return the result of multiplying the values together in order. Example:

1
[1, 2, 3, 4] => 1 * 2 * 3 * 4 = 24

选用C++支持Clang 8 C++17,用点特性

1
2
3
4
5
6
7
8
#include <vector>
int grow(std::vector<int> nums) {
int s = 1;
for(auto i : nums) {
s *= i;
}
return s;
}

<8 kyu> Student’s Final Grade

题目描述:

Create a function finalGrade, which calculates the final grade of a student depending on two parameters: a grade for the exam and a number of completed projects.

This function should take two arguments: exam - grade for exam (from 0 to 100); projects - number of completed projects (from 0 and above);

This function should return a number (final grade). There are four types of final grades:

  • 100, if a grade for the exam is more than 90 or if a number of completed projects more than 10.
  • 90, if a grade for the exam is more than 75 and if a number of completed projects is minimum 5.
  • 75, if a grade for the exam is more than 50 and if a number of completed projects is minimum 2.
  • 0, in other cases

Examples(Inputs–>Output):

1
2
3
4
5
6
7
8
9
10
100, 12 --> 100
99, 0 --> 100
10, 15 --> 100

85, 5 --> 90

55, 3 --> 75

55, 0 --> 0
20, 2 --> 0

*Use Comparison and Logical Operators.

呃,判断,分支结构

1
2
3
4
5
6
int final_grade(int exam, int projects){
if(exam > 90 || projects > 10) return 100;
if(exam > 75 && projects >= 5) return 90;
if(exam > 50 && projects >= 2) return 75;
return 0;
}

<8 kyu> Transportation on vacation

题目描述:

After a hard quarter in the office you decide to get some rest on a vacation. So you will book a flight for you and your girlfriend and try to leave all the mess behind you.

You will need a rental car in order for you to get around in your vacation. The manager of the car rental makes you some good offers.

Every day you rent the car costs $40. If you rent the car for 7 or more days, you get $50 off your total. Alternatively, if you rent the car for 3 or more days, you get $20 off your total.

Write a code that gives out the total amount for different days(d).

这就纯炫技题()

1
2
3
int rental_car_cost(int d){
return (d < 3) ? (d * 40) : (d < 7) ? (d * 40 - 20) : (d * 40 - 50);
}

CodeWars系列题目(持续更新)
http://example.com/2022/11/17/CodeWars系列题目(持续更新)/
作者
OSLike
发布于
2022年11月17日
许可协议