Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Smallest of Three

Return the smallest of three integers.

Bạn sẽ học được gì

  • Keep track of the best value so far
  • Compare values with if statements
  • Handle ties and negative numbers

Đề bài

Xem bản dịch

Three friends compare how many minutes they were late this week and the one who was late the least gets to pick lunch. Given the three numbers, find the smallest. Some values can be equal, and they can be negative (early).

Implement smallest_of_three(a, b, c).

Input

a: The first number.

b: The second number.

c: The third number.

Output

Return the smallest of the three values. If several are equal and smallest, return that value.

Ràng buộc

−10^18 ≤ a, b, c ≤ 10^18

Ví dụ

Ví dụ 1
Đầu vào:
a = 7
b = 15
c = 3

Kết quả mong đợi:
expected = 3
Đầu vào:
long long a = 7;
long long b = 15;
long long c = 3;

Kết quả mong đợi:
long long expected = 3;
Đầu vào:
long a = 7;
long b = 15;
long c = 3;

Kết quả mong đợi:
long expected = 3;

Giải thích

3 is less than both 7 and 15.

Ví dụ 2
Đầu vào:
a = -4
b = -4
c = 10

Kết quả mong đợi:
expected = -4
Đầu vào:
long long a = -4;
long long b = -4;
long long c = 10;

Kết quả mong đợi:
long long expected = -4;
Đầu vào:
long a = -4;
long b = -4;
long c = 10;

Kết quả mong đợi:
long expected = -4;

Giải thích

The two −4 values tie for smallest, so the answer is −4.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Assume the first number is the smallest.

Gợi ý 2

Compare it with the second and then the third, updating when you find a smaller one.

Gợi ý 3

Use a 64-bit integer type, the values can be very large.

Độ phức tạp mong đợi

O(1) time, O(1) extra space

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.