当前位置:网站首页>C language: Exercise 2

C language: Exercise 2

2022-06-26 13:12:00 No code red

Hello everyone , I am a Not allowed to . Practice together c Basic questions of language , Consolidate basic knowledge !

One 、 fraction

Title Description :

Using the nesting of conditional operators to complete this problem : academic record >=90 It's for the students A Express ,60-89 Use between points B Express ,60 Divide the following uses C Express .

Topic analysis :

(a>b)?a:b This is a basic example of a conditional operator .

Source code ;

#include<stdio.h>

int main(){
    
    int score;
    char grade;
    printf(" Please enter the score : ");
    scanf("%d",&score);
    grade=(score>=90)?'A':((score>=60)?'B':'C');
    printf("%c\n",grade);
    return 0;
}

effect :
 Insert picture description here

Two 、 The common factor 、 Common multiple

Title Description :

Enter two positive integers m and n, Find the greatest common divisor and the least common multiple .

Topic analysis :

(1) Minimum common multiple = The product of the two numbers entered is divided by their greatest common divisor , The key is to find the greatest common divisor ;

(2) Finding the greatest common divisor by rolling Division ( Also known as Euclidean algorithm )

1) prove : set up c yes a and b Maximum common divisor of , Write it down as c=gcd(a,b),a>=b, Make r=a mod b
set up a=kc,b=jc, be k,j Relatively prime , otherwise c It's not the greatest common divisor According to the above ,r=a-mb=kc-mjc=(k-mj)c
You know r It's also c Multiple , And k-mj And j Relatively prime , Otherwise, it is the same as the above k,j Mutual contradiction , Thus we can see that ,b And r The greatest common divisor of c, namely gcd(a,b)=gcd(b,a
mod b), Obtain evidence .

2) Algorithm description :

First step :a ÷ b, Make r Is the remainder of the result (0≤r The second step : swap : Set up a←b,b←r, And go back to the first step .

Source code :

#include<stdio.h>
int main()
{
    
    int a,b,t,r,n;
    printf(" Please enter two numbers :\n");
    scanf("%d %d",&a,&b);
    if(a<b)
    {
    t=b;b=a;a=t;}
    r=a%b;
    n=a*b;
    while(r!=0)
    {
    
        a=b;
        b=r;
        r=a%b;
    }
    printf(" The greatest common divisor of these two numbers is %d, The minimum common multiple is %d\n",b,n/b);
    
    return 0;
}

effect :
 Insert picture description here

3、 ... and 、 Character statistics

Title Description :

Enter a line of characters , Count out the English letters 、 Space 、 The number of numbers and other characters .

Topic analysis :

utilize while sentence , The condition is that the character entered is not ’\n’.

Source code :

#include<stdio.h>
int main()
{
    
    char c;
    int letters=0,spaces=0,digits=0,others=0;
    printf(" Please enter some letters :\n");
    while((c=getchar())!='\n')
    {
    
        if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
            letters++;
        else if(c>='0'&&c<='9')
            digits++;
        else if(c==' ')
            spaces++;
        else
            others++;
    }
    printf(" Letter =%d, Numbers =%d, Space =%d, other =%d\n",letters,digits,spaces,others);
    return 0;
}

effect :
 Insert picture description here

Four 、 Sum up

Title Description :

seek s=a+aa+aaa+aaaa+aa…a Value , among a It's a number . for example 2+22+222+2222+22222( At this time, there is 5 Add up the numbers ), A keyboard controls the addition of several numbers .

Topic analysis :

The key is to calculate the value of each item .

Source code ;

#include<stdio.h>
int main()
{
    
    int s=0,a,n,t;
    printf(" Please enter  a  and  n:\n");
    scanf("%d%d",&a,&n);
    t=a;
    while(n>0)
    {
    
        s+=t;
        a=a*10;
        t+=a;
        n--;
    }
    printf("a+aa+...=%d\n",s);
    return 0;
}

effect :
 Insert picture description here

5、 ... and 、1000 Within completion

Title Description :

If a number is exactly equal to the sum of its factors , This number is called " Complete ". for example 6=1+2+3. Programming to find out 1000 All the completions within .

Topic analysis :

Find all the factors of a number , Add it up , Is it equal to itself .

Source code ;

#include<stdio.h>
#define N 1000
int main()
{
    
    int i,j,k,n,sum;
    int a[256];
    for(i=2;i<=N;i++)
    {
    
        sum=a[0]=1;
        k=0;
        for(j=2;j<=(i/2);j++)
        {
    
            if(i%j==0)
            {
    
                sum+=j;
                a[++k]=j;
            }
            
        }
        if(i==sum)
        {
    
            printf("%d=%d",i,a[0]);
            for(n=1;n<=k;n++)
                printf("+%d",a[n]);
            printf("\n");
        }
        
    }
    return 0;
}

effect :
 Insert picture description here

6、 ... and 、 The ball fell freely

Title Description ;

A ball from 100 Free fall at meter height , Jump back to half of the original height after landing ; And then fall , Ask it in the 10 The next landing , How many meters in total ? The first 10 How high is the rebound ?

Source code :

#include<stdio.h>
int main()
{
    
    float h,s;
    h=s=100;
    h=h/2; // First bounce height 
    for(int i=2;i<=10;i++)
    {
    
        s=s+2*h;
        h=h/2;
    }
    printf(" The first 10 The next landing , Common course %f rice , The first 10 Secondary rebound high %f rice \n",s,h);
    return 0;
}

effect :
 Insert picture description here

7、 ... and 、 Monkeys eat peaches

Title Description :

The problem of monkeys eating peaches : The monkey picked some peaches on the first day , Half eaten immediately , It's not addictive , Another one
The next morning I ate half of the rest of the peaches , Another one . Every morning I eat the rest of the day before
One and a half . To the first 10 When I want to eat again in the morning , See there's only one peach left . Ask how much you picked on the first day .

Topic analysis ;

Adopt the method of converse thinking , Infer from back to front .

  1. set up x1 Count the peaches of the previous day , set up x2 Count the peaches for the next day , be :

x2=x1/2-1, x1=(x2+1)*2

x3=x2/2-1, x2=(x3+1)*2

And so on : x front =(x after +1)*2

  1. From 10 The day can be analogized to the 1 God , It's a circular process .

Source code :

#include <stdio.h>
#include <stdlib.h>
int main(){
    
    int day, x1 = 0, x2;
    day=9;
    x2=1;
    while(day>0) {
    
        x1=(x2+1)*2;  //  The number of peaches on the first day was 2 The number of peaches in the sky 1 After 2 times 
        x2=x1;
        day--;
    }
    printf(" The total number is  %d\n",x1);
    
    return 0;
}

effect ;
 Insert picture description here

8、 ... and 、 match

Title Description :

Two ping-pong teams play , Three people each . Team a is for a,b,c Three people , Team B is x,y,z Three people . The match list has been drawn . Someone asked the players for the list of the game .a Say he's not at peace x Than ,c Say he's not at peace x,z Than , Please program to find out the players of the three teams .

Source code :

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    
    char i,j,k;
    for(i='x';i<='z';i++) {
    
        for(j='x';j<='z';j++) {
    
            if(i!=j) {
    
                for(k='x';k<='z';k++) {
    
                    if(i!=k&&j!=k) {
    
                        if(i!='x'&&k!='x'&&k!='z') {
    
                            printf(" In sequence :a--%c\tb--%c\tc--%c\n",i,j,k);
                        }
                    }
                }
            }
        }
    }
}

effect :
 Insert picture description here

Nine 、 Sum fractions

Title Description :

There's a sequence of scores :2/1,3/2,5/3,8/5,13/8,21/13… Find the front of this sequence 20 Sum of items .

Topic analysis :

Please grasp the law of change of the numerator and denominator .

Source code :

#include <stdio.h>
 
int main()
{
    
    int i,t;
    float sum=0;
    float a=2,b=1;
    for(i=1;i<=20;i++)
    {
    
        sum=sum+a/b;
        t=a;
        a=a+b;
        b=t;
    }
    printf("%9.6f\n",sum);  
}

effect :
 Insert picture description here

Ten 、 Multiplicative multiplication

Title Description :

seek 1+2!+3!+…+20! And .

Topic analysis :

This program just turns accumulation into multiplication .

Source code :

#include <stdio.h>
 
int main()
{
    
    int i;
    double sum,mix;
    sum=0,mix=1;
    for(i=1;i<=20;i++)
    {
    
        mix=mix*i;
        sum=sum+mix;
    }  
    printf("%lf\n",sum);  
}

effect :
 Insert picture description here

原网站

版权声明
本文为[No code red]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261221177476.html