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 fishing or drying the net with C language

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to solve the problem of fishing or drying the net in C language". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn "C language how to solve the problem of fishing or drying the net". Let's study and learn together.

1. Problem description

As a Chinese saying goes, "fishing for three days and drying the net for two days". Starting from January 1, 1990, someone began to "fish for three days and dry the net for two days" and asked the person whether he was "fishing" or "drying the net" in the future.

two。 Topic analysis

According to the meaning of the problem, the problem-solving process can be divided into three steps:

(1) to calculate the total number of days from 1 January 1990 to the appointed day.

(2) since the period of "fishing" and "drying the net" is 5 days, the calculated number of days will be removed by 5.

(3) to judge whether he is "fishing" or "drying the net" according to the remainder.

If the remainder is 1, 2, 3, he is "fishing", otherwise he is "drying the net".

3. Algorithm design

The algorithm is a numerical algorithm, which uses a cycle to calculate the number of days between the specified date and January 1, 1990, and takes into account the leap year in the cycle. February is 29 days in the leap year and 28 days in the usual year.

The method to judge a leap year is as follows:

If (divisible by 4 and not divisible by 100) or (divisible by 400), then the year is a leap year; otherwise it is not a leap year.

Tip: the congruence operator% can be used to judge whether an integral division can be done in C language.

4. Process framework

? Calculate the distance from a specified date

Here is the core part of the whole algorithm. After analysis, we can get: (the number of days from the specified date to January 1, 1990) totalDay = the total number of days from 1990 to the year before the specified year + the number of days from the specified year to the specified date.

Because the number of days in each month is different, you can set a month array int perMonth [13] to store the number of days in each month.

The program uses the year as a cyclic variable to judge whether each year before the specified year is a leap year, and if it is a leap year, execute totalDay=totalDay+366, otherwise execute totalDay=totalDay+365

For a specified year, it is also determined whether it is a leap year, and then the number of days per month is added to the totalDay based on the number of months.

The initialization settings for the perMonth array are shown in the following figure

The perMonth array setting contains 13 elements, and the perMonth [0] element is not used.

The reason is that this setting can make the array subscript correspond to the month, and it is convenient for programming to set cyclic variables. The number of February days in the array is initially set to 28. If the current year is boudoir year, you need to perform perMonth [2] + + operation.

Design a function int run Year (int year) to determine whether it is a leap year or not.

The realization of the total number of days function int countDay (Date currentDay).

5. Code implementation

Complete code?

# include / / define the date structure typedef struct DATE {int year; int month; int day;} DATE;// to judge the leap year function int runYear (int year) {if ((year% 4 = = 0 & & year% 100! = 0) | | (year% 400 = = 0) return 1; else return 0 } / / calculate the number of days from the specified date to January 1, 1990 int countDay (DATE currentDay) {/ / define an array of days per month int perMonth [13] = {0min31min28 for 31lmlt 31lmlt 31lt 31}; int totalDay = 0; int I = 0; int year = 0; / / find the sum of the days in each year before the specified date (year = 1990; year)

< currentDay.year; year++) { if (runYear(year)) { totalDay = totalDay + 366; } else { totalDay = totalDay + 365; } } //如果为闰年,则2月份为29天 if (runYear(currentDay.year)) { perMonth[2]++; } //将本年内的天数累加到totalDay中 for (i = 1; i < currentDay.month; i++) { totalDay += perMonth[i]; } //将本月内的天数累加到totalDay中 totalDay += currentDay.day; return totalDay;}int main(){ DATE today; //指定日期 int totalDay; //指定日期距离1990年1月1日的天数 int result; //totalDay对5取余的结果 printf("请输入指定日期,包括年,月,日,例如:1999 1 31\n"); printf("请输入>

: "); scanf ("% d%d%d ", & today.year, & today.month, & today.day); totalDay = countDay (today); / / find the number of days from the specified date to January 1, 1990 result = totalDay% 5 / / days% 5 to judge whether to fish or if (result > 0 & & result < 4) {printf ("fishing today\ n");} else {printf ("fishing today\ n");}}

Run the result?

How can we tell that this program is correct? It is very simple, because the topic began on January 1, 1990, so 1 and 3 must be "fishing" and 4 and 5 must be "drying the net".

Thank you for your reading. the above is the content of "how to solve the problem of fishing or drying the net with C language". After the study of this article, I believe you have a deeper understanding of how to solve the problem of fishing or drying the net with C language. the specific use of the situation also needs to be verified in 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

Development

Wechat

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

12
Report