Function-loop-array

Source: Internet
Author: User
Tags pow

I. PTA LAB WORK

Topic one. 7-42 function Programming-piecewise functions: Write a function to calculate the value of the following segment function.

1. PTA Submission List

2. Design Ideas

The main function defines and enters X, calls the child function sum (x), and the child function sum (x) has a different Y value for the different values of x. Finally calculates and outputs Y.

3. Problems encountered in debugging process and PTA Submission List situation description.

(1). Compile error: Not familiar with the use of functions, the EXP function is written as epx function, resulting in compilation errors.

(2). Answer error: Or answer format error, the format of the problem is output y= answer, and I directly output the answer.

4. Code.

Topic two. 7-1 Combinatorial number: The subject requires programming, according to the formulaC?N?M??=?M!(N?M)!??n! ?? calculated from < Span class= "Mord mathit" >n a different element remove m elements (m≤n.

It is recommended to define and invoke function fact(n) calculations n! , where n the type is int , the function type is double .

1. PTA Submission List

2. Design Ideas

Define the M,n in the main function and enter the values for both, and call the child function fact (n) when the output is evaluated. The child functions in fact (n) define sum and I to initialize to 1 when I<=n, Sum*=i, to the class of N.

3. Problems encountered in debugging process and PTA Submission List situation description.

There is no problem with the code, but the answer to the output is an integer, which is easy to get out of the pit as a basic problem. The output should be%.0LF.

4. Code.

Topic three. 7-3 the sum of power: To write a program, calculateSUM=2?1??+2?2? ? +2< Span class= "Fontsize-ensurer reset-size5 size5" >? 3 ?? +? +2? n ??。 The POW function can be called to exponentiation.

1. PTA Submission List

2. Design Ideas

Define and enter N in the main function and use the sub-function QM (n) to find out the answer at the output. The i=1,sum=0 is defined in the child function, each time i<=n,sum +=pow (2,i).

3. Problems encountered in debugging process and PTA Submission List situation description.

This is the basic problem, and there is no difficulty. So there was no unexpected situation at the time of submission.

4. Code.

If it is shown.

Second, peer code peer evaluation

1. Mutual evaluation classmate: Bryan Chen Bing Yan

2. My Code, mutual evaluation of the student code

Bryan Chen Bing Yan Classmate's code:

My Code:

3. What is the difference between my Bryan Chen Bing and my classmate code? What are the respective advantages? What style of code do you prefer?

The difference is: Bryan Chen Bing Yan classmate According to the topic will n into I and (n-i), and then use the sub-function to determine whether it is a prime number, if return 1, otherwise return 0, so that when I and (n-i) are prime, if the conditional statement is established. My idea and Bryan Chen Bing classmate some similar, my sub-function is also to determine whether the number passed over is a prime,

If the number itself is returned, it is not returned. But my child function type is int, not a prime number when no return value is vulnerable. Only to find out this bug, others have not seen, hope there is no error.

Respective advantages: Bryan Chen Bing Yan Classmate's code is simple and understandable, and my code is just a semi-finished product.

I prefer the code of Bryan Chen Bing-yan classmate.

Third, this week's topic set PTA Final ranking.

Iv. Summary of this week's study (2 points)

1. I learned a simple function programming question

2. This week's content, you are not what?

Pta:7-2,7-7,7-9,7-10.

3. Function Exam Summary

(1). Which problem is wrong, how to change it?

7-4 do wrong, the idea is a bit biased. : Note the type of the child function, whether you want to return a value.

(2). Are you satisfied with the exam results, how to improve?

Not satisfied, many of the topics are smattering, I hope that they can in this respect and exercise, more hearts.

Cycle

I. PTA LAB WORK

Topic one. 7-1 to find the first n of the staggered sequence and: To write a program, to calculate the staggered sequence 1-2/3+3/5-4/7+5/9-6/11+ ... The sum of the first n items.

1. PTA Submission List

2. Design Ideas

The observed molecule is N, the denominator is 2*n-1, and the sign is constantly changing, so the flag=1 is defined, and the symbol is changed once every sum+.

3. Problems encountered in debugging process and PTA Submission List situation description.

As the problem was simple and clear, no difficulty was encountered.

4. Code.

As shown in.

Topic two. 7-7 calculates the factorial and: for a given positive integer N, you need to calculate S=1! +2! +3! +. . . +N!.

1. PTA Submission List

2. Design Ideas

Enter a positive integer n, a two-tier loop, X calculates the class of I, and S calculates the class of N. Note that x=1 must be made before each class is calculated.

3. Problems encountered in debugging process and PTA Submission List situation description.

(1). The answer is wrong: think it over, clearly the code is written to the same idea. Finally found that the topic requires direct output of the answer, and I output is s= answer. Well, the format is important.

4. Code.

Topic three. 7-14 Special a series summation: given two positive integers not exceeding 9A andN, requires a program to be writtena +aa+aaa++?< Span class= "Mbin" >+aa? A (n a)

1. PTA Submission List

2. Design Ideas

First, the positive integers a and n are entered, so that the sum,num is initialized to 0,i to initialize the values of each of the sum=sum*10+a in the 1,for loop, and the sum of each is calculated num+=sum.

3. Problems encountered in debugging process and PTA Submission List situation description.

3. Problems encountered in debugging process and PTA Submission List situation description.

3. Problems encountered in debugging process and PTA Submission List situation description.

3. Problems encountered in debugging process and PTA Submission List situation description.

The error point is the same as the second question, the output format is s= answer, and I am writing the sum= answer. Long snack bar, Sao year!

4. Code.

Second, peer code peer evaluation

1. Mutual Evaluation Students: Leihui

2. My Code, mutual evaluation of the student code

Leihui the code of the classmate:

My Code:

3. What is the difference between my Leihui and my classmates ' code? What are their strengths? What style of code do you prefer?

The difference is: in judging the number of rows Leihui classmate's method is to determine whether the square side length n is even, if even the number of columns D is N/2, otherwise the number of columns is (n+1)/2; And my method is to compare the floating point n/2.0 with the integer number N/2, if the floating point and the integer number is greater than 0.5, then the number 1, otherwise the number of columns B is N/2.

Other places are not different.

Respective advantages: Different thinking constructs different methods, I think two methods are very good, but Leihui classmate's code more concise, worth learning.

I prefer the code of Leihui classmates.

Third, this week's topic set PTA Final ranking.

Iv. Summary of this week's study

1. I learned simple cycle topics, such as seeking from 1 to 100.

2. This week's content, you are not what?

Too complex topic without train of thought, for example 3 to 7 Narcissus number, PTA on the 7-8,7-9,7-24,7-34,7-38.

3. Circular Examination Summary

(1). Which problem is wrong, how to change it?

7-17. Look for 250: The first time with a for loop without a perfect score, later with the while statement can be, but still do not know why wrong. Probably because the for loop is more appropriate for the number of known loops.

(2). Are you satisfied with the exam results, how to improve?

Not satisfied, the efficiency of doing the problem is very low, many programs are written too cumbersome.

Array

I. PTA LAB WORK

Topic one. 7-2 the maximum value and its subscript: the main problem is to write a program to find the maximum value in the given n number and its corresponding minimum subscript (subscript starting from 0).

1. PTA Submission List

2. Design Ideas

The child function min (n) is defined, so the array a[10] is defined as a global variable. First, enter the number n in the main function, enter the n number with a For loop, and save in the array a[10]. Call the sub-function min (n) to find the largest number and make it subscript index. The output subscript is index number and subscript index.

3. Problems encountered in debugging process and PTA Submission List situation description.

(1) Paragraph error: Logically look at this code is not a problem, has not understood the submission to PTA why is wrong. Later, I rewrote it with a sub-function, and found that if I changed the definition of a[n] to a[10] it would be correct. So the conjecture should be that the version of PTA does not support arrays of variables in parentheses, rather than a constant.

To verify the above conjecture, convert the first a[n in the code] to a[10] and submit again.

So the conjecture is probably set up.

4. Code.

As shown in the following two images in the 3rd.

Topic two. 7-17 judgment on the upper triangular matrix: the upper triangular matrix refers to the matrix below the main diagonal, which is 0, and the main diagonal is the connection from the upper left corner of the matrix to the lower right corner.

The subject asks to write a program to determine whether a given phalanx is on a triangular matrix.

1. PTA Submission List

2. Design Ideas

The child functions are defined as fact (n) and two-dimensional array a[10][10] (global variables), the main function input the total number of matrices to be tested T, and then enter the number of T, the number of rows per matrix (equal to the number of columns). Each time you enter a number of rows, the child function fact (n) is called once. The Sub-function fact (n) implements the input and calculation of the N-order matrix with a number of zeros below the diagonal. Finally, the actual zero count is compared to the theoretical Zero N (n-1)/2, if the output is yes, otherwise the output is no.

3. Problems encountered in debugging process and PTA Submission List situation description.

The problem is too sloppy, in judging whether the following diagonal number is zero and counting, in if there is more than a break, resulting in an error. Remind me to do the problem in the future, especially pay attention to break and continue use is appropriate.

In addition, the reference to the practice of the students only found that my ideas, although true but too cumbersome. Their thinking is simplified to: first define a flag=0, directly determine whether the number below the diagonal is not zero, if not zero then flag=1,break out of the loop directly output no, otherwise flag=0, output yes. No, this method is much better than mine, the flowchart is also simplified, I also learned from it, that is, more open their own ideas, find the best solution to do the problem.

4. Code. (The picture on the left is my complete code, and the image on the right is the modified part)

Topic three. 7-22 the sum of the elements of each line of the matrix: the requirement to write a program to find a given mxn matrix The sum of the elements of each row.

1. PTA Submission List

2. Design Ideas

A child function is defined as fact (n,m) and a two-dimensional array global variable a[6][6], and after entering the value of a two-dimensional array in the main function, the child function is called fact (n,m). The first layer in the child function fact (N,M) is the number of rows, guaranteeing that each line begins to compute the sum of the elements sum=0, the second loop is the number of columns, and the a[i][j] is added to sum, each time the second layer loop ends, a sum is output for each element of the row.

3. Problems encountered in debugging process and PTA Submission List situation description.

(1) This problem is the basis of the problem, the first time to submit the correct use of the function, found the compilation error. It is found that the number of columns n is used in the sub-function, but it is not passed from the main function to the child function. The correct answer is then modified.

4. Code. (The picture on the left is the general program, the right figure uses the function)

Second, peer code peer evaluation

1. Mutual evaluation classmate: Bryan Chen Bing Yan

2. My Code, mutual evaluation of the student code

Bryan Chen Bing Yan Classmate's code:

My Code:

3. What is the difference between my Bryan Chen Bing and my classmate code? What are the respective advantages? What style of code do you prefer?

The difference is: Bryan Chen Bing Yan classmate's idea is to insert the title of the x as an existing array of the next bit of a new array, and then by arranging to get an ordered array. And my idea is to compare X with the adjacent two numbers, if X is exactly in between these two numbers, then the previous number is copied to the new array b[10],x the position after the replacement, from the beginning of the next one, the position of the number of the original array is shifted one bit. To get a new array.

In addition, in the transfer of sub-functions, Bryan Chen Bing-yan classmate processing better. She passed the initial address of the original array from the main function to the child function, and the global variables I defined, although feasible, took up more memory and was not appropriate.

Respective advantages: Bryan Chen Bing Yan classmate's code more concise, but also more ornamental.

I prefer the code of Bryan Chen Bing-yan classmate.

Third, this week's topic set PTA Final ranking.

Iv. Summary of this week's study

1. I have learned to use arrays to store multiple bits of data, in the transfer of the child function and the main function to participate in the argument, with the array name to the first address can be passed, do not persist in global variables.

2. This week's content, you are not what?

Simple array problems can be solved, but encountering a slightly harder array always forgets to store first and then output. Make oneself easy to turn niu Jiao Jian, can't come out.

Some of the topics are hard to come by, but the next time they meet again will get into trouble, feeling completely without thinking. Think the topic is not careful enough, often can not get full marks, in the PTA and Dev debugging, only to find that the code is flawed.

3. Array Exam Summary

(1). Which problem is wrong, how to change it?

7-5, the character array to leave one more position to the, or the wrong is very hidden.

(2). Are you satisfied with the exam results, how to improve?

Not satisfied, there are many problems did not do. One to the exam on the Fanhun, some of the topics usually in the PTA on the dealings, but one to the exam on the brain blank, easy to make mistakes, the results more tense, less time. Of course, even if the time is sufficient, I may not be able to write, but at least not regret.

Function-loop-array

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.