C Language Blog Job--function

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise logical operators

First, the PTA laboratory work problem 1:6-6 use function output Narcissus number 1. PTA Submission List


2. Design Ideas

int narcissistic (int number)//function definition

    • 1. Define integer variable A.I to hold number separated numbers and number of cycles for digit power operation
    • 2. Define x=number,b=1,c=number,sum=0, define integer variable digit to hold the number of bits
    • 3. Use the loop to find the number of digits for (digit=1;; digit++) {x=x/10;if (x==0) {break;}}
    • 4.A=C%10 separates C, number, and stores it in a
    • 5. Using the For loop, the separated number A is digit to a power operation and stored in B
    • 6.sum=sum+b the sum of the digit power of the numbers on each bit
    • 7.C=C/10 to remove the separated numbers.
    • 8.b=1 clears the value of B and continues into the next loop to find the result of the digit power of a
    • 9. Repeat step 4 until the c=0
    • 10. Determine the relationship between sum and number: equal return 1, unequal return 0

void Printn (int m, int n)//function definition

    • 1. Define the integer variable I to hold the number of cycles
    • 2.for (i=m+1;i<=n-1;i++) {//List all numbers in open interval (m,n)
      if (narcissistic (i)) {//Call the previous function if the number of daffodils is met
      printf ("%d\n", I); Output Narcissus number I
      }
      }
3. Problems encountered in debugging process and PTA Submission List situation description.
    • 1. At the outset, I met
      The code given in the original topic did not call # include ' MATH.H '
      It then uses the For loop to repeat the accumulation and raises the question: Is it possible to call the # include ' math.h ' answer in the function definition: no

    • 2. After debugging, we find that we have not entered the loop, and after monitoring the variable, we forget to assign the initial value to C .... (Horrible bad habits)
    • 3. Intentionally reducing the definition of unknowns to improve operational efficiency causes the value of number to be changed to affect subsequent comparisons

    • Workaround: Define a number C to enclose the value of numbers and then detach the C without changing the value of # to avoid any impact on subsequent work

    • 4. Find multiple outputs a number of itself
    • Workaround: Go back to examining find is open interval so

    • 5. Finally run the same answer as the example, but .... Sure enough PTA is not a vegetarian ...
    • Workaround: According to the PTA tips
    • (1) Add a condition
      , but the output
      That is, you cannot return anything in a function that is not returned.
    • (2) Maximum interval answer error: After entering a large number of the output only 3 digits of the number, but mistakenly think four digits and no Narcissus number, helpless went down to ask the classmate after the discovery
      The error of their own examining, not all the numbers are each single digit three times but the sum of digit power
    • 6. Do not worry about this unfamiliar sentence, the solution is very simple: close the black box to

Topic 2:6-7 Use the function output to specify the number of ends in the range 1. PTA Submission List



2. Design Ideas

int factorsum (int number)//function definition

    • 1. Define the integer variable i and sum to hold the number of cycles and all factors and
    • 2. Use for (i = 2; i<number; i++) {//factor for number
      if (number%i = = 0) {
      sum + = i; Add all factors
      }
      }
    • 3. Return the value of sum

function definition void printpn (int m, int n)

    • 1. Define the integer variable I,J to store the number of cycles and all possible factors, define flag=0 storage is not complete, flag=1 storage has a number of cases, and give flag the initial value =0
    • 2. Use for (i = m; i <= N; i++) to list all numbers in (m,n)
    • 3. Call the function to determine if I is a complete number: if (factorsum (i) = = i)//If the factor is added equal to itself, that is, the number of matches
    • 4. If, then flag=1, and output the previous small expression of the expression: printf ("%d = 1", i);
    • 5. Use for (j = 2; j<i; j + +) {//factor for number
      if (i%j = = 0) {
      printf ("+%d", j); An expression that adds some factor after the output expressions
      }
    • 6. Wrap a line after completing an expression of one end
    • 7. Determine if flag is 0, if so, output No perfect number
3. Problems encountered in debugging process and PTA Submission List situation description.
    • 1. When defining two functions, you can first determine that a function is correct and then comment out and then debug another function.
    • 2. After writing the code according to your own ideas, the output found a serious error:
      What is this? Roll to debug


      Discover i=6,j=5 then result = 1 into the loop .... Is it an integer/integer = integer problem? It is not, however, that so many losses have become i*1.0/j.
      But the result of what equals 1 ah ... Re-observing the result and finding that I actually defined the result as an integer ... Get a little experience: if the result should be a decimal but there are two cases of outputting an integer:
      (1) integer/integer = integer (2) Define the result as an integer at the beginning.
    • WORKAROUND: Obviously change int to double
    • 3. Part of the answer is correct
    • Workaround: According to the PTA tip, for special points such as just enter a 1 and so on by adding if statements to improve the code
    • 4. Maximum range Run Timeout: code efficiency issues
      Because we have just learned that by int (x) You can force the X to become an integer, we use the X=int (x) method to determine the divisible
      But if it is in this large scope of the implementation of the code that requires more than one operation is undoubtedly greatly reduce the execution efficiency of the code, and from the seniors know that this method may also bring floating point error (accents)
    • The solution: Innovation without fruit and then use the most classical traditional way to take the remainder is equal to the zero Division
Topic 3:7-1 Combinatorial number 1. PTA Submission List


2. Design Ideas
    • 1. function declaration double fact (int x)
    • 2. Define two integer variables m,n define floating-point variable result to hold the result
    • 3. Enter two integers m,n
    • 4. Determine if M and n are equal, and if so, then result=1
    • 5. If not, the function is called to calculate result = Fact (n)/(Fact (M) *fact (N-M))
    • 6. Output results
      Define a function double fact (int n) to seek n!
    • 1. Define integer variable i, floating-point variable result
    • 2. Assign the initial value: Result=1 I=1
    • 3.result=result*i
    • 4.i++, repeat steps 3 until i>n
    • 5. Return the value of result
3. Problems encountered in debugging process and PTA Submission List situation description.
    • 1. Floating-point errors
    • Workaround: Seek n! like this The number of results should be defined as a double type instead of an int type
    • 2. Wrong answer
    • Workaround: The answer should be an integer output

Second, peer code pairs of peer review 1. Peer reviews Photos.

2. My Code, mutual evaluation of the student code
刘艳钦同学的代码int reverse( int number ){  int i=1,x,sum=0;         //定义整数型变量i,x,sum分别存放符号,分离出来的数字,逆序后的数  while(number){            x = number%10;         //从个位开始分离输入的数存放在x    sum = sum * 10 +x;     //把上一次分离出来的数*10然后在加上新分离出来的数组成新数    number=number/10;            //把分离出的数从number中去掉    }     sum=sum*i;               //把符号加到number上(此处针对负数)    return sum;               //返回sum的值}
林晓露同学的代码int reverse( int number ){    int i,j,k,down,sum;  k=number;  i=1;  sum=0;  while(number/10!=0){    number=number/10;    i++;}//计算number的位数  for(j=i;j>0;j--){//根据number的位数循环输出逆序数    down=k%10;    sum=sum+down*pow(10,j);    k=k/10;  }  if(sum%10==0)//当最后是0时舍去    sum=sum/10;  return sum;}
3. Where do I differ from my classmates ' code? What are the advantages? What style of code do you prefer? If the classmate code is wrong, please help to point out where the problem.
    • Different points: The design ideas are different: I-use the way to take out the number of numbers from the back to take out the digits after the number of 10, first separated out of 10 times more, that is, in the front of the new number to achieve the reverse order
      Lin Xiaolu--using a number of each number in the positive sequence and reverse 10 of the sum of the same to achieve reverse order, eg (512 5 in the positive sequence 102 times, then in reverse order 100 times, 2 in the positive sequence 100 times and then two times)
    • In this topic, I prefer my code style, because my code is relatively more efficient and tidy, but Xiao-Lu's code design ideas are easier to understand and more mathematical thinking
      Although I started with the idea is to Xiao Lu's design ideas, but the main function given by the topic did not provide us with # # #
Third, this week's topic set PTA Final Ranking

Iv. Study Summary of the Week 1. What did you learn? What data types are 1.1 C languages?

1.2 Character type data need to be aware of the place?
    • 1. Character constant: A character that is amplified by single quotation marks, eg ' a '
      Escape character: A special character constant that begins with ' ' (represents a character)
      Note: All characters can be represented by an escape character
    • 2. Character variables: Used to hold characters, and only one character can be stored
      Defined in the way that Char C
      The way to assign a value is c= ' A ' or c=65
      The input output is in the form%c
      But the character data and the integer data can be assigned to each other, that is, the character data can be output as a character, or it can be output in integer form.
1.3 Self-increment decrement operator?
    • + + is the self-increment operator, which is the monocular operator, which is the function of increasing the value of a single variable by 1. It has two use cases:
    • 1) Front: ++i, execute i=i+1 First, then use I value;
    • 2) Rear: i++, first use I value, then perform i=i+1.
      For example:
      j=3; k=++j; k=4,j=4 after execution. The above statement is equivalent to: j=3; j=j+1; k=j;
      j=3; k=j++; k=3,j=4 after execution. The above statement is equivalent to: j=3; k=j; j=j+1;

    • --is the decrement operator, which is the monocular operator, which is the function of reducing the value of a single variable by 1. It has two use cases:
    • 1) Front:-I, first execute i=i-1, then use I value;
    • 2) Rear: i--, first use I value, then perform i=i-1.
      For example:
      j=3; k=--j; k=2,j=2 after execution. The above statement is equivalent to: j=3; j=j-1; k=j;
      j=3; k=j--; k=3,j=2 after execution. The above statement is equivalent to: j=3; k=j; j=j-1;
    • Attention:
      1) The increment and decrement operators, which can only be used for variables, cannot be used for constants and expressions. such as 5++,--(A+B) are illegal.
      2) The union direction of the increment, decrement and minus operators is right-to-left.

1.4 Operator Precedence?

Operators are divided into 15 levels, 1-level priority, and 15-level precedence.
Operators of the same precedence, the order of operations is determined by the binding direction. (Binding: The Monocular operator, the trinocular operator, and the various assignment operators are right-to-left others are left-to-right)
Simple Note is:! > Arithmetic operators > Relational operators > && > | | > Assignment Operators
In a little more detail, it comes with a formula. (Online Search)
Bracket member first; The parentheses operator member operator. -
The whole monocular second; All monocular operators such as + +,--、 + (positive),-(negative), pointer operations *, &
Multiplication Yozo, plus minus four; This "remainder" refers to the residual operation that is%
Shift five, relationship six; Shift operators:<< >>, Relationships:> < >= <=, etc.
Equal to (and) unequal row seventh; that is = = and! =
Bits and Xor and bits or;     "Three points of the world" eighty or ninety; These are bitwise operations: Bits and (&) XOR (^) bits or (|)
logic or heel; Logical operators: | | and &&
12 and 11; Note Order: Priority (| |) Bottom-priority (&&)
Conditions above assignment,//three mesh operator precedence to 13-bit only than assignment operator and "," high
Lowest comma operation level! The comma operator has the lowest precedence

1.5 which expressions are in C language?

An expression:

    • 1 Arithmetic Expressions:
      Monocular: +,-, + +,--。
      Binocular: +,-,*,/,%.
    • 2 an Assignment expression:
      Simple assignment: =
      Compound assignment: +=,-=,*=,,/=%=,!=.
    • 3 Relationship expression: >,>=,<,<=,!=.
    • 4 Logical expression:! , &&,| |
    • 5 Conditional Expressions:
      Exp1? Exp2:exp3
      Cases:

      if(x>0)y=x+2;      =====    y=(x>0)?x+2:x*xelsey=x*x;
    • 6. Comma-expression:,
      Expression 1, expression 2, Expression 3 ..... Expression n
      The expression 1 is evaluated first, and then the expression 2 is evaluated .... And the value of the expression n as the value of the comma expression.
      Int A,b,c;
      (a=2), (b=3), (C=A+B);
      The comma operator has the lowest priority, the left combination
    • Note: (1) You can see that an expression can also have no operator, such as "4", which is the simplest form of expression, that is, the simplest expression has only one constant or one variable name and no operator.
      (2) Function call is also an expression

    • Class School

      By y=012, Y is an octal, converted into a decimal =10
      By using the X-value first, the expression equals 10.

1.6 Other content?
    • 1. Digital character and number conversion
      number=ch-' 0 '
      ch=number+ ' 0 '
    • 2. Case conversion
      lowercase to uppercase: ch-' a ' + ' a '
      Uppercase to lowercase: ch+ ' a '-' a '
    • 3. Forcing type conversions
      Double n=3.14
      X=int (x) =3
    • 4. Automatic type conversion
      Non-assignment operations: horizontal, vertical, type small, large
      Assignment operation: variable = expression
      Automatically converts the type of the expression on the right side of the assignment operator to the type of the left variable of the assignment number
    • Note: Logical operators note P138
      In the solution of a logical expression, not all logical operators are executed, and the next operator executes the statement only if the next logical operator must be executed to find the value of the expression:
      eg
      T are all type int variables,
      Set X, y
      X=y=3;t=++xll ++y;
      The value of Y is 13
    • Bit computing applications
      2 The number of the whole number of features: Turn into 2 after the system?
      Only one tail 1 others are 0. That's 1000.
      Only 1 1 binary number-1, what is the form? 0111
      And then I do the & operation, the result? Can judge the number of 2 of the whole number of square
      int pow_2 (int n) {
      if ((N1) &n) ==0) return 1;
      else return 0;

2. This week's content, you are not what?
    • 1. The bitwise operation is also very unfamiliar, such as bit logic operation, shift operation, compound bit assignment operation are not very familiar with, nor its specific flexible use, in addition to understand the teacher class example to find 2 of the number of the whole number of
    • 2. To XOR or binary, octal, hexadecimal conversion is not proficient
    • 3. Not familiar with automatic type conversion
3. Circular Structure Examination Summary
    • 1. What is wrong with the question, and how to change it?
      The design of the last topic is too complex to cause partial correctness and run timeout


      But it's still not exactly right still in the debugging and modification phase .....
    • 2. is the exam result satisfactory, how to improve?
    • 1. Not satisfied
    • 2. The design idea is still not clear and clear, especially in the case of limited time, after debugging or will find that there are always some common small mistakes or have been committed
    • Improved:
      1) in the future code more to have the concept of time, that is, in peacetime to train themselves to write code efficiency rather than just the pursuit of completion
      2) Common mistakes should be read frequently to remind yourself after summarizing the blog.
      3) The old adage: Practice makes perfect~~ still continues to knock code! Knock the Code! Knock the Code!
      4) For the last question, this is more flexible and complex questions to see and think more and more practice
    • 3. Other summaries.
      1) I think I preview the efficiency is too low, do preview homework situation is not ideal
      2 job efficiency is also very low, although now has a half semester has also played a lot of code, but will always be a few pieces of code trapped for a long time to move, resulting in daily work chasing run
      3) This week's learning content is too much, a bit difficult to digest, octal hexadecimal and bitwise arithmetic is not enough to understand

C Language Blog Job--function

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.