Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of 24:00 games with C language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how the c language solves the game problem at 24:00". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought slowly and deeply. Let's study and learn how the c language solves the game problem at 24:00.

problem

You have four cards with numbers from 1 to 9. You need to determine whether you can get 24 through the operation of *, /, +, -, (,).

Example 1:

Enter: [4, 1, 8, 7]

Output: True

Explanation: (8-4) * (7-1) = 24

Example 2:

Enter: [1, 2, 1, 2]

Output: False

Note:

The division operator / represents real division, not integer division. For example, 4 / (1-2 impulse 3) = 12.

Each operator operates on two numbers. In particular, we can't use-as a unary operator. For example, the expression-1-1-1-1 is not allowed when [1, 1, 1, 1] is input.

You can't connect numbers together. For example, when the input is [1, 2, 1, 2], it cannot be written as 12 + 12.

Analysis

In the 24:00 game, there are a total of ✖️ 4 ✖️

✖️ 4 ✖️ ✖️ 4 = 9126 cases. If you need to give a column number to determine whether it can be satisfied by the above operation, if the result is 24, you only need to enumerate and verify.

The general solution is to enumerate and verify.

Today, I saw a piece of code that shook the body of a tiger in the discussion area of LeetCode.

Class Solution:

Def judgePoint24 (self, nums):

Bad = 'tell me that you don't know what to do with your life. I don't know. I mean, I don't know. I don't know.

Return chr (int (''.join (map (str, sorted (nums) + 19968) not in bad

The result after submission is like this.

Why is this possible?

Because in the 24:00 game, four numbers, the value range of each number is [1-9], the total number of unrepeated combinations is 495 groups, of which the following 92 groups are unsolved combinations:

1111, 1112, 1113, 1114, 1115, 1116, 1117, 1119, 1122, 1123, 1124, 1125, 1133, 1159, 1167, 1177, 1178, 1179, 1189, 1199, 1222, 1223, 1299, 1355, 1499, 1557, 1558, 1577, 1667, 1677, 1678, 1777, 1778, 1899, 1999, 2222, 2226, 2279, 2299, 2334, 2555, 2556, 2599, 2677, 2777, 2779, 2799, 2999, 3358, 3388, 3467, 3488, 3555, 3577, 4459, 4466, 4467, 4499, 4779, 4999, 5557, 5558, 5569, 5579, 5777, 5778 5799, 5899, 5999, 6667, 6677, 6678, 6699, 6777, 6778, 6779, 6788, 6999, 7777, 7778, 7779, 7788, 7789, 7799, 7888, 7899, 7999, 8888, 8889, 8899, 8999, 9999

So you just need to encode these 92 cases with Unicode, and then for a given input, sort it into a string and query whether it is encoded in these 92 cases.

Thank you for your reading. the above is the content of "how the c language solves the game problem at 24:00". After the study of this article, I believe you have a deeper understanding of the problem of how the c language solves the game problem at 24:00. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report