ZZUIL problem solution 1041-1050 (C language version)

If you have any questions, please comment below
1041: sequence summation 2

1042: sequence summation 3

1043: maximum

1044: failure rate

1045: numerical statistics

1046: product of odd numbers

1047: logarithm table

1048: logarithm table

1049: sum of squares and sum of cubes

1050: cumulative sum of factorials

1041: sequence summation 2

Enter an integer n and output the sum of the first n items of the sequence 1-1 / 3 + 1 / 5 -.

input
Enter only one integer n.
output
The result keeps 2 as decimal and occupies a separate line.
Sample input Copy
3
Sample output Copy
0.87
Problem solving ideas:
The denominator is defined as a single variable. Each cycle changes the value accordingly (floating point type is still required). The odd term is positive and the even term is negative;

AC Code:

#include<stdio.h>
int main(){
    int i,n;
    double total,n1=1.0;
    scanf("%d", &n);
    for(i=1; i<=n; i++){
        if(i%2==0)
        total-=1/n1;
        else
        total+=1/n1;
        n1+=2;
    }
    printf("%.2f\n", total);
    return 0;
} 

1042: sequence summation 3

Title Description 1-2 / 3 + 3 / 5-4 / 7 + 5 / 9-6 / 11 + The first n items of and, and the result retains 3 decimal places.

input
Enter a positive integer n (n > 0).
output
Output a real number, keep 3 decimal places, and occupy a separate line.
Sample input Copy
5
Sample output Copy
0.917
Problem solving ideas:
The numerator and denominator are calculated respectively according to their own laws. The even number item is positive and the odd number item is negative. In addition, it is emphasized that floating point type should be used again

AC Code:

#include<stdio.h>
int main(){
    int i,n;
    double total=0,n1=1.0,n2=1.0;
    scanf("%d", &n);
    for(i=1; i<=n; i++){
        if(i%2==0)
            total-=n1/n2;
        else
            total+=n1/n2;
        n1+=1;
        n2+=2;
    }
    printf("%.3f\n", total);
    return 0;
}

1043: maximum

Enter an integer n and N integers, and output the maximum value of these n integers.

input
The input has two lines: the first line is a positive integer n, and the second line is n integers.

output
The output contains an integer, that is, the maximum of n numbers, which occupies a single line.

Sample input Copy
4
3 7 5 6
Sample output Copy
7
Problem solving ideas:
Use a variable to record the maximum value, and judge each number;

AC Code:

#include<stdio.h>
int main(){
    int i,n,num,max;
    scanf("%d %d", &n, &num);
    max=num;
    for(i=1; i<n; i++){
        scanf("%d", &num);
        if(num>max)
            max=num;
    }
    printf("%d\n", max);
    return 0;
} 

1044: failure rate

Title Description input n and N student scores (real numbers) and output the failure rate.

input
The first line of input is an integer n, and the second line is n real numbers separated by spaces.

output
Output a real number to indicate the failure rate, and keep 2 decimal places for the result, accounting for a separate line.

Sample input Copy
8
98 45 86 79 56 75 90 70
Sample output Copy
0.25
Problem solving ideas:
Accumulate the number of failures. Note that floating point type should be used when calculating the number of failures (floating point type / integer type = floating point type, and integer type / integer type = integer type)

AC Code:

#include<stdio.h>
int main(){
    int i,n;
    double num,total=0,result;
    scanf("%d", &n);
    for(i=1; i<=n; i++){
        scanf("%lf", &num);
        if(num<60)
            total+=1;
    }
    printf("%.2f\n", total/n);
    return 0;
} 

1045: numerical statistics

The title describes the number of negative, zero and positive numbers in a given number of n.

input
The first number entered is the integer n (n < 100), indicating the number of values to be counted, and then n integers

output
Output a line a,b and c, representing the number of negative, zero and positive numbers in a given data respectively.

Sample input Copy
6 0 1 2 3 -1 0
Sample output Copy
1 2 3
Problem solving ideas:
The topic examines the use of if else, using three variables to record the results;

AC Code:

#include<stdio.h>
int main(){
    int i,n,total1=0,total2=0,total3=0;
    int num;
    scanf("%d", &n);
    for(i=1; i<=n; i++){
        scanf("%d", &num);
        if(num<0)
            total1+=1;
        else if(num==0)
            total2+=1;
        else
            total3+=1;
    }
    printf("%d %d %d\n", total1,total2,total3);
    return 0;
} 

1046: product of odd numbers

The problem description gives you n integers and finds the product of all odd numbers in them.

input
The first number is n, which means that there are n data in this group, followed by N integers. You can assume that there must be at least one odd number in each group of data.
output
Output the product of all odd numbers in n numbers, accounting for one line.
Sample input Copy
5 2 5 4 6 7
Sample output Copy
35
Problem solving ideas:
Use the loop to judge the value encountered, and multiply it if it is an odd number;

AC Code:

#include<stdio.h>
int main(){
    int i,n,multiply=1;
    int num;
    scanf("%d", &n);
    for(i=1; i<=n; i++){
        scanf("%d", &num);
        if(num%2!=0)
            multiply*=num;
    }
    printf("%d\n", multiply);
    return 0;
} 

1047: logarithm table

Enter two positive integers m and N, and output the natural logarithm of each integer between M and n. The input consists of two integers m and n (m < = n), separated by a space.

output
Each row outputs an integer and its logarithm. The integer accounts for 4 columns and the logarithm accounts for 8 columns. It is right aligned and the logarithm retains 4 decimal places.

Sample input Copy
2 4
Sample output Copy
2 0.6931
3 1.0986
4 1.3863
Problem solving ideas:
Use mathematical functions to realize the output of logarithmic table, and pay attention to the requirements of the topic;

AC Code:

#include<stdio.h>
#include<math.h>
int main(){
    int i,n1,n2; 
    scanf("%d %d", &n1,&n2);
    for(i=n1; i<=n2; i++){
        printf("%4d%8.4f\n", i,log(i));
    }
    return 0;
}

1048: factorial table

Enter a positive integer n (n < = 20) and output a factorial table between 1 and n. The input has only one positive integer n. Output the factorial table between 1 and N. see the output example for the format. There are two data in each row. The first data accounts for 4 columns and the second data accounts for 20 columns. It is aligned to the left.

Sample input Copy
5
Sample output Copy
1 1
2 2
3 6
4 24
5 120
Problem solving ideas:
Use a single loop to print factorial results once. Pay attention to the topic requirements to prevent overflow;

AC Code:

#include<stdio.h>
int main(){
    int i;
    long long n,fact=1; 
    scanf("%d", &n);
    for(i=1; i<=n; i++){
        fact*=i;
        printf("%-4d%-20lld\n", i,fact);
    }
    return 0;
} 

1049: sum of squares and sum of cubes

Given two integers m and N, find the square sum of all even numbers and the cubic sum of all odd numbers in the continuous integer m~n.

input
It consists of two integers m and N. you can assume that m < = n
output
It shall include two integers x and y, representing the sum of squares of all even numbers and the sum of cubes of all odd numbers in the continuous integer. 32-bit integers are sufficient to hold the results.
Sample input Copy
2 5
Sample output Copy
20 152
Problem solving ideas:
Use a loop to judge each value. If it is even, calculate the sum of squares, and if it is odd, calculate the sum of cubes

AC Code:

#include<stdio.h>
int main(){
    int i,num1,num2;
    long long total1=0,total2=0; 
    scanf("%d %d", &num1,&num2);
    for(i=num1; i<=num2; i++){
        if(i%2==0)
            total1+=i*i;
        else
            total2+=i*i*i;  
         
    }
    printf("%lld %lld\n", total1,total2);
    return 0;
} 

1050: cumulative sum of factorials

Title Description for 1+ 2! + …… n!

input
Enter an integer n, and you can assume that n is not greater than 10.
output
Output an integer, that is, the result of factorial accumulation, which occupies a separate line.
Sample input Copy
4
Sample output Copy
33
Problem solving ideas:
Set a variable a to record the result of each factorial, and use a cycle to complete the accumulation of factorials

AC Code:

#include<stdio.h>
int main(){
    int i,n;
    long long total=0,a=1; 
    scanf("%d", &n);
    for(i=1; i<=n; i++){
            a=a*i; 
            total+=a;
    }
    printf("%lld\n", total);
    return 0;
} 

Keywords: C

Added by bloodgoat on Mon, 03 Jan 2022 22:38:11 +0200