C Language Fifth Blog assignment

Source: Internet
Author: User

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

2. Design Ideas
    • (1) First define the narcissistic function.
    • (2) Define four shaping variables n,a,d,cnt,sum,cnt is used to calculate the sum of several numbers, sums used to hold each number of CNT-sub-square.
    • (3) Make a loop, let the N=number (prevent the subsequent need to use number when its value changes), loop N=N/10, until
      The n<=0 ends the loop, where the CNT value of each cycle is added to one, which is used to calculate several numbers.
    • (4) Perform A For loop again (let A=number; until a does not >0;a/=10)
      D=a%10;sum=sum+pow (d,cnt) in the loop, which is to add the CNT of each number of bits.
    • (5) Here is a judgment, if nunber=sum that the number is the number of daffodils, then return 1;
      Otherwise, return 0;
    • (6) The definition of void printn (int m, int n) function.
    • (7) Define a variable i is used to cycle
    • (8) The for (i=m+1;i<n;i++) loop lets it output the number of daffodils in this range.
      Because the number of daffodils in this interval cannot take M and N, the initial value defined for I is m+1,
      The qualifying condition is i<n.
    • (9) if (narcissistic (i)) call the previous function to determine whether I is a daffodil number
      printf ("%d", i);p rintf ("\ n") if the number is output and wraps.

      3. Problems encountered in commissioning process and PTA submission List status note
    • (1) At the time of compiling the problem, it was initially intended to use CNT to calculate the value of number.
      But in the title of the problem has been given the value of 153, the 4th step when the CNT was lost to 3
      The results have been partially correct.

With more than three digits, the number of daffodils cannot be lost.

    • (2) Another problem is that you don't know how to return this value after you've judged the number of daffodils.
      After the attempt to use the next return 1 on it, good luck.

      Topic 2:6-8 Use a function to output the number of Fibonacci in a specified range of 1. PTA Submission List

2. Design Ideas
    • (1) int fib (int n) first defines the FIB function
    • (2) Int a=1,b=1,i. Define three shaping variables (because of the content given by the topic, A, B's initial value is 1,i is used to do the loop
    • (3) if (N==1 | | n==2) return 1, if n=1,n=2 Fibonacci number is 1
    • (4) for (i=3;i<=n;) another loop, so that i=3 until i>n the end of the loop.
    • (5) a=a+b;i=i+1;b=b+a;i++; This allows the value of the preceding two numbers to be added to the third number.
    • (6) If n is an odd number, the value of return a
    • (7) Output even values for even numbers
    • (8) void printfn (int m, int n) and the definition of void printfn function
    • (9) Int CNT=0,I,J defines three shaping variable i,j for loop, CNT is used to determine whether there are Fibonacci in this interval
    • (x) for (I=1;FIB (i) <=n;i++) if (fib (i) >=m) printf ("%d", fib (i)) to compare each Fibonacci number to M
    • (11) If FIB (i) is >=m, it indicates that the Fibonacci number is within the interval, then the output fib (i)
    • (a) if (FIB (i+1) <=n) printf (""); enter "--" at the end of the last Fibonacci number in the interval to satisfy the line.
      And in this if statement let Cnt=1
    • (13) Make a judgment statement outside the loop if cnt=0 outputs no Fibonacci number.

      3. Problems encountered in commissioning process and PTA submission List status note

You cannot keep a space at the end of the line at first without noticing it, so it is correct only when it is empty

So I made a decision before the output space, if FIB (i+1) <n that the number is the second-to-last number in the interval, then you don't want to output a space after the next number.
However, the error is that both ends are F numbers, and the fib that tests over intervals does not satisfy the condition

So I read the topic again if M,n is Fibonacci number then he also needs to output. So a = is added to the IF (FIB (i+1) <n). Conditions are met.

Topic 1:6-7 Use a function to output a number of 1 in the specified range. PTA Submission List

2. Design Ideas

A simple function that computes integer factor and is required to implement

    • (1) int factorsum (int number) defines this function to determine whether a count is a function
    • (2) int i,sum=0; Defines the value that sum is used to hold the added factor
    • (3) if (number==1) return 1; Because 1 is certain to meet there is a little special, first take it out to consider
    • (4) for (i=1;i<number;i++) if (number%i==0) sum=sum+i; make a loop in the range of <n and not =n,
      So 1 is a bit special, and the next judgment statement is to add all the factors of I.
    • (5) if (Sum==number) indicates that the number is the end number, then return sum. otherwise return 0;
    • (6) void printpn (int m, int n) in the definition of this function
    • (7) Int i,j,a=1. I,j is used to cycle, a is used to determine whether there is a complete number of intervals within
      -(8) for (i=m;i<=n;i++) if (factorsum (i) = = i) indicates that this number is the end number, then printf ("%d = 1", i) because any count will be equal to the number of 1+.
      -(9) for (j=2;j<i;j++) in the case of a loop if (i%j==0) This is used to determine the factor of I, if yes then printf ("+%d", j)
    • (10) The line is then wrapped in the J-loop outside of printf ("\ n"). and make A=0
    • (11) Make a judgment if (a==1) on printf ("No Perfect Number")

      3. Problems encountered in commissioning process and PTA submission List status note

The difficulty of this problem lies in the completion number = Factor 1 + Factor 2 + ... + factor k How does the output of this format allow it to correctly output the factor while also outputting the correct output + and space.
After observing that each number must have a factor of 1, "+%d", j in such a format output, I started with each number after adding a space, so that the end of the line appears space,
Later after a classmate's point, put the space in front of the + so that there will be no space at the end of the line.

Space at the end of the line

Second, peer code peer evaluation

2. My code, peer-to-peer code of my Code!
int fib( int n ){    int a=1,b=1,i;    if(n==1 || n==2){//如果n=1,2时F数都为1        return 1;    }    for(i=3;i<=n;){        a=a+b;//让前两个数相加得到一个新的数        i=i+1;        b=b+a;        i++;    }    if(n%2==0){//判断奇偶数        return b;    }    else{        return a;    }}void PrintFN( int m, int n ){    int cnt=0,i,j;    for(i=1;fib(i)<=n;i++){        if(fib(i)>=m){            printf("%d",fib(i));            if(fib(i+1)<=n){              printf(" ");            }            cnt=1;        }     }    if(cnt==0){        printf("No Fibonacci number");    }}
Huang's Code
int fib( int n ){    int result=0,i,a=1,b=1;    if(n==1)return 1;    for(i=2;i<=n;i++)    {        result=b;        b=b+a;        a=result;    }    return result;}void PrintFN( int m, int n ){   int f,i,flag=0,c=0;//f放完数,i控制循环,利用flag判断是否有完数,c,判断是否为第一个数    for(i=1;i<=100;i++)   {    f=fib(i);    if(f>=m&&f<=n)//完数在所给范围内     {        c++;        flag=1;        if(f==1&&m==1&&i==1)//起始点为1比较特殊,直接输出1        {        printf("1");        continue;       }       if(c==1)//第一个数前面没有空格,所以单独输出        {        printf("%d",f);        continue;       }           printf(" %d",f);    flag=1;    }       }   if(flag==0)//没有完数,则flag还是为0,输出。         printf("No Fibonacci number");}
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: I Herbing classmate in defining the FIB function is not the same, I was let N from the beginning of the loop a=a+b,i++,b=b+a,i++ when n is odd when its F number on a,
Even when the F number is then B.
And Bing Wei classmate is in the definition of a variable to hold the number of fib, result=b; B=b+a; A=result its cycle is starting from 2 first let result=b, each time let b=b+a, again will A=result
It's equally ingenious to put the first two numbers on the result.
Advantage: 1: My Code calculates multiple F-numbers in one-step loop, cuts the cycle time, and saves space by not having to define more than one variable.
And, Bing Wei classmate's code is more refined, do not need to make judgment in the back, direct return to result, and mine has two exits.
2: On the second function, I think my advantage will be greater, in the first loop I end the condition is fib (i) <=n and Bing Wei classmate i<=100, so it will cycle more times, longer.
Then in order to avoid the end of the line output space if (FIB (i+1) <=n) printf (""); I'm looking for the second-to-last number in the NM range, and he's letting the first number be output separately, and designing a number to judge the first number in the interval.
In short, its code uses more space and time than I do.
So, I would like my own code more.

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

C Language Fifth Blog assignment

Related Article

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.