C-language jobs-functions

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

First, the PTA experimental work problem 1:400~499 4 occurrences of the number of 1. PTA Submission List

2. Design Ideas

First, main function

    • 1. function declaration int Fun (int x)
    • 2. Define the variable i,k,i represents the input value, K holds 4 occurrences
    • 3. Repeat the following steps until i>499
    • 4. Call the function int fun (int x) to calculate the K value in I
    • 5. Accumulate K value
    • 6. Output k value

Second, function int fun (int x)

    • 1.K Assigning initial value 0
    • 2. When x! =0, repeat the steps below
    • 3. Calculate single digit x%10, if x%10==4,k++
    • 4. Remove the digit, X=X/10
    • 5. Return k value
3. Problems encountered in debugging process and PTA Submission List situation description.

K value output is not correct

Debugging

It is clear to see that the value of M is the single digit of I, that is, every time a single digit to remove the single digit of the statement is not implemented, resulting in the actual output of k is a digit number 4 digits

Correction: The change does while statement is the while statement, debug under See if there is no implementation to remove single digit

You can see the value of the K record.

Episode: Ignoring the K+=fun (i) statement in the main function, see that the K value is not realized accumulation, using static local variables to achieve the accumulation of k, resulting in a value of thousands of output. Post-correction

Topic 2: Use the function to output daffodils number 1. PTA Submission List

2. Design Ideas

First, main function

    • 1. Defining Integer Variables M,n
    • 2. Enter M,n
    • 3. Call the function int narcissistic (number) to determine if it is a daffodil
    • 4. If narcissistic (m) ==1,printf ("%d is a narcissistic number\n", m)
    • 5. If narcissistic (n) ==1,printf ("%d is a narcissistic number\n", N)
    • 6. Call the function void Printn (int m, int n) to elect the number of daffodils (m,n) and output the number of daffodils

Second, function int narcissistic (int number)

    • 1. Defining Integer Variables N,i,j,,number1,number2
    • 2. Assigning an initial value to n 0
    • 3. Define integer variable sum, hold the number of each digit n times and assign the initial value
      1. Number1=number,number2=number
    • 5. If Number1 does not repeat for 0 the following steps
    • 6. Go to single digit: NUMBER1=NUMBER1/10
    • 7.n++, used to denote the number of bits
    • 8.i=1, when I is less than n the value, repeat the following steps
    • 9.j=number2%10,sum=sum+pow (J,n)
    • 10. If sum==number, return 1, otherwise, return 0

Third, function void Printn (int m, int n)

    • 1.m++
    • 2. When M<n, repeat the steps below
    • 3. If narcissistic (m) ==1, output this number
    • 4.m++;
3. Problems encountered in debugging process and PTA Submission List situation description.

Error code:

#include <stdio.h>int narcissistic (int number), void printn (int m, int n); int main () {int m, n;    scanf ("%d%d", &m, &n);    if (narcissistic (m)) printf ("%d is a narcissistic number\n", m);    PRINTN (M, n);    if (narcissistic (n)) printf ("%d is a narcissistic number\n", N); return 0;}        #include <math.h>int narcissistic (int number)//Call function {int n,i,j;        int sum=0; while (number!=0) {//If number does not perform a loop body of 0 j=number%10;//take single digit n++;//accumulate N to calculate narcissus digits Number=numbe            r/10;//remove single digit to get new number} for (i=1;i<=n;i++) {j=number%10;            NUMBER=NUMBER/10; Sum=sum+pow (j,n);///Calculate the number of bits of the n-th square and} if (Sum==number) {//if the individual digits of the number n times and equal to this number return 1;}    Returns 1 return 0;//otherwise returns 0}void printn (int m, int n) {int number; for (number==m;number<=n;number++) {//when number executes the loop body if (narcissistic (number) ==1) in the (m,n "range) {//If it is a narcissus pr intf ("%d\n", number);//output This number}}} 

Run:

The second function has no output, could it not have entered this function?

135 is not a narcissus number, but also output, so the first function call is not also a problem ....

Turns output m to n all numbers, no number of daffodils to be judged

Error code:

#include <stdio.h>int narcissistic( int number );void PrintN( int m, int n );int main(){    int m, n;    scanf("%d %d", &m, &n);    if ( narcissistic(m)) printf("%d is a narcissistic number\n", m);    PrintN(m, n);    if ( narcissistic(n))printf("%d is a narcissistic number\n", n);    return 0;}#include<math.h>int narcissistic( int number )//调用函数 {        int N,j,sum,i;        N=0;        sum=0;      do{//如果number不为0执行循环体             number=number/10;//除去个位数得到新的number             N++;//位数加一         }   while(number!=0);        for(i=1;i<=N;i++){            j=number%10;//取个位数             number=number/10;//去除个位数             sum=sum+pow(j,N);//计算各个位数的数字的N次方和         }        if(sum==number)//如果各个位数数字N次方和等于这个数              return 1;//返回1              return 0;//否则返回0 }void PrintN( int m, int n ){     while(m<n){    if( narcissistic(m)==1){//如果为水仙花数             printf("%d\n",m);}//输出这个数                   m++;     }}

Debugging:

Found M,n cannot be entered, and sum has been 0

Error: Ignoring the type defined by number is a global variable, so number=0 after the end of the loop where the digits are taken, and affects the number that appears later, depending on the global variable, they have a value of 0, resulting in an error

Correction: Use Number1,number2 to store the value of number, Number1,number2 for two loops, so it does not change the value of number

Submit:

New error: Run timeout, no output

Debugging:

debugging found that the loop is out of circulation, careful observation found that the cycle condition is number1! =0, and I wrote number!. =0, which causes the loop to not end.

After correction:

Not very clear about this error, but know the error is because of the input range problem
Read the topic again, the loop starting point should be m+1 instead of M, execute m++ before the loop starts, submit the correct

Topic 3: Finding the number of combinations 1. PTA Submission List

2. Design Ideas

First, main function

    • 1. Define two integer variables, m,n
    • 2. Define two floating-point variable result, representing the combined number results
    • 3. Call function double fact (int n), calculate result=fact (n)/(Fact (M) *fact (N-M))
    • 4. Output result, control output number is 0

Second, function double fact (int n)

    • 1. Define integer variable i, storage cycle times
    • 2. Assign the initial value to fact double fact=1.0
    • 3. When I is less than or equal to N, repeat the steps below
    • 4.fact=fact*i
    • 5.i++
    • 6. Return the value of fact
3. Problems encountered in debugging process and PTA Submission List situation description.

Error: No initial value for fact

Correction process: Try double i,double (fact*i) but run the same error, double type multiplied by int is double type, the error point is not here. Assign a fact=1 to the fact, run or show the error, and change to 1.0 or not. The error above explains that you want to output a double type. So try changing it to double fact==1.0.

Second, peer code pairs of peer review 1. Peer reviews Photos. (Me, Shen Mengting)

2. My Code, mutual evaluation Classmate Code topic 6-9 Verify Goldbach conjecture

My Code:

  #include <stdio.h> #include <math.h>int prime (int p); void Goldbach (int n); int main () {int m,    n, I, CNT;    scanf ("%d%d", &m, &n);    if (Prime (m)! = 0) printf ("%d is a prime number\n", m);    if (M < 6) m = 6;    if (m%2) m++;    CNT = 0;        for (i=m; i<=n; i+=2) {Goldbach (i);        cnt++;        if (cnt%5) printf (",");    else printf ("\ n"); } return 0;}    int prime (int p) {int k;    for (k=1;k<=p/2;k++) {//If p%k==0 is satisfied in the loop, the number is not a prime, ending the loop if (p%k==0) break;          } if (k>p/2&&p!=1)//If the loop ends normally, the condition is met, which is the prime number return 1; else return 0;}    void Goldbach (int n) {int x1,x2; for (x1=2;x1<n;x1++) for (x2=2;x2<n;x2++) if (Prime (x1) ==1&&prime (x2) ==1&&x1<=x2) {                             Call the prime function to determine if it is a prime if (x1+x2==n)//To determine whether the Goldbach conjecture is satisfied with printf ("%d=%d+%d", n,x1,x2);            Break }}

Shen Mengting's Code:

#include <stdio.h>#include <math.h>int prime( int p );void Goldbach( int n );int main(){    int m, n, i, cnt;    scanf("%d %d", &m, &n);    if ( prime(m) != 0 ) printf("%d is a prime number\n", m);    if ( m < 6 ) m = 6;    if ( m%2 ) m++;    cnt = 0;    for( i=m; i<=n; i+=2 ) {        Goldbach(i);        cnt++;        if ( cnt%5 ) printf(", ");        else printf("\n");    }    return 0;}int prime( int p ){    int i,n;    if(p==1)//判断p为1时不是素数         return 0;    n=sqrt(p);    for(i=2;i<=n;i++){        if(p%i==0){//判断p是否为素数             return 0;        }    }        return 1; }  void Goldbach( int n ) {    int p,q;     for(p=2;p<=n;p++){        if(prime(p)!=0){//p是素数             q=n-p;//p与q的关系             if(prime(q)!=0){//q是素数                 printf("%d=%d+%d",n,p,q);                break;             }         }     } }
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.

I expect to be through two loops, find the output that satisfies Goldbach conjecture, in order to find the topic request of a group to end, also added break, but still output all the situation, changed several times also not, think this method is more suitable for the poor lift . And the Shen Mengting is by finding the smallest prime number, using the two numbers to add equal to the condition of N, the other value is expressed, and then determine whether the number is a prime, if so, then the Goldbach conjecture is verified. Shen Mengting's code is clear and simplifies a lot. Comparing my code with nested loops determines how many times a loop is cycled than the number of Shen Mengting code loops. All I like more about her code.

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?

Integer, character, real

1.2 Character type data need to be aware of the place?

(1) Not only can be written in the form of character constants, can also be expressed in the corresponding ASSCII code, can be expressed in integers. Integer and char variables can be interchanged
(2) characters have numeric characteristics that can participate in operations like integers
(3) The escape character represents only one character
(4) All characters in the ASCII character set can be represented by escape characters

1.3 Self-increment decrement operator?

(1) Increase the value of the variable by 1 or minus 1
Set n is an integer variable and has been assigned a value
++n and n++ are equal to n=n+1.
--n and n--are equal to n=n-1.

(2) Take the value of a variable as the value of an expression
The order of operations for ++n is to execute the n=n+1 first, and then the value of n as the value of the expression ++n
n++ The Order of operations is: first the value of n as the value of the expression n++, and then execute n=n++

1.4 Operator Precedence?

(1) If operator precedence is the same on both sides of the operand, the calculation order is determined by the combination direction
(2) operator precedence from highest to lowest: logical operators, arithmetic operators, relational operators, logical operators, conditional expressions, assignment operators, comma operators
(3) Different priorities are calculated from highest to lowest priority levels

1.5 which expressions are in C language?

arithmetic expressions, assignment expressions, relationship expressions, logical expressions, conditional expressions, and comma expressions

What do you do wrong in class, please analyze the reason here?

(1) int x=1,y=012;
printf ("%d", y*x++);

Correct answer: 10
My answer: 20

Analysis: + + priority higher than *,x++ value is x, Y is octal, y=10, so the result is 10

(2) If A is of type int and its value is 3, the value of a after the expression a+=a-=a*a is executed

Correct answer:-12
My Answer:-3

Analysis: A*a priority, so a-=9. That is, the value of A=A-9.A becomes-6. So a+=-6, that's a=a-6, so a=-12

1.6 Other content?

(1) Specifies the output width of the integer data:%MD, if the actual number of bits is less than m, then the left fill space, if greater than M, the actual number of digits output

(2) Type of double specifies output width:%m.nf, reserved n decimal, output width m

(3) ch-' a ' + ' a ' converts lowercase characters to numbers
ch-' 0 ' convert numeric characters to numbers
val+ ' 0 ' converts numbers to numeric characters

(4) Type conversion
1. Automatic type conversion
2. Forcing type conversions

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

Binary conversions and operator prioritization are not memory-based. and forgetting that global variables are used throughout the program in the PTA is a significant feature that changes the values of global variables in the program's run. Mainly just learn the knowledge point is not familiar, a single handle out will, put in the topic forget, need a deeper impression of the process bar.

3. Circular structure Examination Summary 1. What is wrong and how to change it?

The fourth question is not done, the fifth is wrong.
The fourth question in the binary system, the textbook did not lead to get the topic self-suggestion I have not learned, certainly will not write. The fifth question has not yet developed the algorithm, do not know how to write code.
The fourth question can first understand the next binary, and then do it again. The fifth question wants to think seriously again ...

2. is the exam result satisfactory, how to improve?

Not satisfied. More to strengthen the textbook on some such as the prime judgment of such functions, the topic is also quite common, too dependent on textbooks will feel that all understand these simple functions, but their own writing when a variety of small details of the problem exposed, wasting time to find the wrong

3. Other summaries.

1. Do not waste time on simple topics, resulting in excessive time, so some common functions ah, statement ah, to remember
2. The knowledge points mentioned in the book but not introduced in detail should be made available as soon as possible by finding out the information and not delaying it, so there is no time to read it. Binary is a bloody example to me.
3. The ability to find faults is important

C-language jobs-functions

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.