Fishing or drying nets

Source: Internet
Author: User

Today on the Internet to see a fun program in C + +, and then learn the knowledge inside.

There is a saying in China called "Notwith two Days". Someone started "Notwith two Days" from January 1, 1990 onwards,

ask this person in The next day is "fishing" or "Sun Net".

* Problem analysis and algorithm design
The problem solving process can be divided into three steps according to test instructions:
1) Calculate the number of days from January 1, 1990 to the specified date;
2) due to "fishing" and "Sun net" period of 5 days, so the calculated number of days with 5 to remove;
3) According to the remainder to determine whether he is in "fishing" or in the "sun Net";
If the remainder is a "one-man", he is in "fishing"
Otherwise, it's in the "sun screen."
In these three steps, the key is the first step. The number of days from January 1, 1990 to the specified date, to determine if there are leap years in the experience year,

two The month is 29 days and the common year is 28 days. Leap year methods can be described in pseudo-statements as follows:

If (the year can be removed by 4 and cannot be exhausted by 100) or can be removed by 400)
Then the year is leap years;
Otherwise, it is not a leap year.
In C language, it is possible to use the remainder operation (i.e., modulo) to determine whether the division can be divisible.

* Program Description and comments

1#include <stdio.h>2 intDaysstructdate day);3 structdate{4 intYear ;5 intmonth;6 intDay ;7 };8 9 intMain ()Ten { One structdate today,term; A intYearday,year,day; -printf"Enter Year/month/day:"); -scanf"%d%d%d", &today.year,&today.month,&today.day);/*Enter Date*/ theTerm.month= A;/*set the initial value of a variable: month*/ -term.day= to;/*set the initial value of the variable: day*/ -  for(yearday=0, year=1990; year<today.year;year++) - { +Term.year=Year ; -Yearday+=days (term);/*calculates the number of days from 1990 to the year preceding the specified year*/ + } AYearday+=days (today);/*plus the number of days in the specified year to the specified date*/ atday=yearday%5;/*Find remainder*/ - if(day>0&&day<4) printf ("He is fishing at that day.\n");/*Print Results*/ - Elseprintf"He is sleeping at that day.\n"); - } -  - intDaysstructDate Day) in { - Static intday_tab[2][ -]= to{{0, to, -, to, -, to, -, to, to, -, to, -, to,},/*average number of days per month*/ +{0, to, in, to, -, to, -, to, to, -, to, -, to,}, - }; the intI,LP; *lp=day.year%4==0&&day.year% -!=0|| day.year% -==0; $ /*Determines whether year is a leap year or common year, lp=0 is common year, and not 0 is a leap year*/Panax Notoginseng  for(i=1; i<day.month;i++)/*calculate the number of days in the year since January 1*/ -day.day+=DAY_TAB[LP]; the returnDay.day; +}

The above is the online version, I ran the next, a push error ....

So modified, and finally run successfully, this is very exciting for me, C + + Novice

  

1#include <iostream>2#include <cmath>3 using namespacestd;4 5   intDaysstructdate day);6 7   structdate{8     intYear ;9     intmonth;Ten     intDay ; One    }; A  -   intMain () - { the      structdate today,term; -      intYearday,year,day; -  -cout<<"Enter Year/month/day:"<<Endl; +cin>>today.year>>today.month>>today.day;/*Enter Date*/ -  +Term.month= A;/*set the initial value of a variable: month*/ Aterm.day= to;/*set the initial value of the variable: day*/ at  -       for(yearday=0, year=1990; year<today.year;year++) -     { -Term.year=Year ; -Yearday+=days (term);/*calculates the number of days from 1990 to the year preceding the specified year*/ -      } in  -Yearday+=days (today);/*plus the number of days in the specified year to the specified date*/ today=yearday%5;/*Find remainder*/ +  -      if(day>0&&day<4) thecout<<"He is fishing at that day.\n";/*Print Results*/ *      Elsecout<<"He is sleeping at that day.\n"; $   }Panax Notoginseng  -     intDaysstructDate Day) the { +    Static intday_tab[2][ A]= A{{0, to, -, to, -, to, -, to, to, -, to, -},/*average number of days per month*/ the{0, to, in, to, -, to, -, to, to, -, to, -}, +    }; -  $    intI,LP; $  -    if(day.year% -==0) -lp=1; the    Else -lp=0;Wuyi /*Determines whether year is a leap year or common year, lp=0 is common year, and not 0 is a leap year*/ the  -     for(i=1; i<day.month;i++)/*calculate the number of days in the year since January 1*/ WuDay.day = Day.day +Day_tab[lp][i]; -  About     returnDay.day; $}

Obsessive compulsive disorder, direct change to C + + code

Fishing or drying nets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.