2014 Huawei machine trial in group A of Xi'an

Source: Internet
Author: User
Tags integer division
2014 Huawei machine trial in group A of Xi'an

Question 1: Apple
The number of ways in which m of identical apples are put in N identical baskets.
1 <= m <= 10, 1 <= n <= 10
For example, three baskets of five apples, 3, 1, 1, and 3 are the same method.

Enter 7 3
Output 8
Question Analysis:
This question is similar to the question of Integer Division. This is a long-standing ACM question. The main idea is to test recursion.
① When the number of apples or the number of baskets is 1, there is only one possibility.
② When the number of apples is smaller than the number of baskets, the number of apples is allocated.
③ When the number of apples is greater than the number of baskets, empty one plate + put one for each plate first and then M-n randomly put
③ When the number of apples is equal to the number of baskets, put one + empty plate on each plate

========================================================== ======================================
Reference code:

// Apple. CPP // 2014.7.12 hepanhui # include <iostream> using namespace STD; int sharingapple (int m, int N) {If (M = 1 | n = 1) return 1; if (M <n) return sharingapple (M, m); else if (M> N) return sharingapple (M, n-1) + sharingapple (m-N, N ); else return 1 + sharingapple (M, n-1);} int main () {int M, N; CIN> m> N; cout <sharingapple (m, n) <Endl; return 0 ;}

Errors During debugging:
① When the number of apples is smaller than the number of baskets, they are directly allocated by Apple. Not sharingapple (N, N.
② M = 1 cannot be written by mistake =

Question 2: Date Calculation
Enter a date and output the day of the year. (The definition of a leap year is not provided in the question)

Enter 20131231
365 output
Question Analysis:
This topic mainly examines the use of the switch statement and the judgment of the runyear. It also needs to be noted that there is no space in the input, indicating that we do not know which is the year, which is the month, and which is the day, so we should look at the input of a string.

========================================================== ====================================
Reference code:

// Calculate the date. CPP // 2014.7.12 hepanhui # include <iostream> # include <string> using namespace STD; int calculatingdate (INT year, int month, int day) {int flag = 0; if (Year % 400 = 0 | (Year % 4 = 0 & year % 100! = 0) Flag = 1; int CNT = 0; For (INT I = 1; I <month; I ++) {Switch (I) {Case 1: Case 3: case 5: Case 7: Case 8: Case 10: CNT + = 31; break; Case 2: CNT + = 28 + flag; break; Case 4: Case 6: Case 9: case 11: CNT + = 30; break;} CNT + = day; return CNT;} int main () {string STR; int year, month, day; cin> STR; year = (STR [0]-'0') * 1000 + (STR [1]-'0 ') * 100 + (STR [2]-'0') * 10 + (STR [3]-'0'); month = (STR [4]-'0 ') * 10 + STR [5]-'0'; Day = (STR [6]-'0') * 10 + STR [7]-'0 '; cout <calculatingdate (year, month, day) <Endl; return 0 ;}

Errors that are easy to make during debugging:
① Pay attention to the writing of the switch statement. Remember to add break to different situations.
② Note that the input must be treated as a string and then converted into an integer.
③ Note that the header file # include <string> must be added during string input.

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.