[PTA-C Language] Programming Exercise 3 - Loop Structure I

7-1 Counting the number of digits in whole numbers (fraction 15)

Read an integer from the keyboard and count the number of digits in the number. For example, input 12534 and output 5; input -99 and output 2; input 0 and output 1.

Export entry formula:
Import single integer N(−105 ≤N≤10). 5

Output format:
Output the number of digits in one line in the following format.

It contains 位数 digits.

Input example:

12534

Output sample:

It contains 5 digits.

Code length limit 16 KB
Time limit 400 ms
Memory limit 64 MB

Reference Code

#include <stdio.h>
int main()
{
    
    
    int n, d=0;
    scanf("%d", &n);
    if (n==0) d=1;
    if (n<0) n=-n;
    while (n!=0) {
    
    
        n/=10;
        d++;
    }
    printf("It contains %d digits.", d);
    return 0;
}

7-2 Output leap year (score 15)

Output all leap years since a certain year in the 21st century. Note: The criterion for identifying a leap year is that the year is divisible by 4 but not 100, or divisible by 400.

Input format:
The input gives a cut-off year in the 21st century in one line.

Output format:
Output all leap years that meet the conditions line by line, that is, each year occupies one line. If the input is not a year in the 21st century, "Invalid year!" will be output. If there are no leap years, output "None".

Input example 1:

2048

Output sample 1:

2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

Input example 2:

2000

Output sample 2:

Invalid year!

Code length limit 16 KB
Time limit 400 ms
Memory limit 64 MB

Reference Code

#include <stdio.h>
#include <math.h>
int main() {
    
    
    int n, i, t = 0;
    scanf("%d", &n);
    if (n <= 2000 || n > 2100) {
    
    
        printf("Invalid year!\n");
        return 0;
    }
    for (i = 2001; i <= n; i++) {
    
    
        if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
    
    
            printf("%d\n", i);
            t = 1;
        }
    }
    if (t == 0) printf("None");
    return 0;
}

7-3 Find the sum of the first N terms of the fraction sequence (fraction 15)

This question requires writing a program to calculate the sum of the first N items of the sequence 2/1+3/2+5/3+8/5+…. Note that starting from item 2 in this sequence, the numerator of each item is the sum of the numerator and denominator of the previous item, and the denominator is the numerator of the previous item.

Input format:
The input gives a positive integer N in one line.

Output format:
Output the value of the partial sum in one line, accurate to two decimal places. The question ensures that the calculation result does not exceed the double precision range.

Input example:

20

Output sample:

32.66

Code length limit 16 KB
Time limit 400 ms
Memory limit 64 MB

Reference Code

#include <stdio.h>
int main()
{
    
    
    int n, i;
    double sum=0, a=1.0, b=2.0, c;
    scanf("%d", &n);
    for(i=1; i<=n; i++) {
    
    
        sum += b/a;
        c=b;
        b=a+b;
        a=c;
    }
    printf("%.2f", sum);
    return 0;
}

7-4 Find the partial sum of simple interleaved sequences to a given precision (Fraction 15)

This question requires writing a program to calculate the sum of the sequence parts 1 - 1/4 + 1/7 - 1/10 + ... until the absolute value of the last item is not greater than the given precision eps.

Input format:
The input gives a positive real number eps in one line.

Output format:
Output the partial sum value S in one line in the format of "sum = S", accurate to six decimal places. The question ensures that the calculation result does not exceed the double precision range.

Input example 1:

4E-2

Output sample 1:

sum = 0.854457

Input example 2:

0.02

Output sample 2:

sum = 0.826310

Code length limit 16 KB
Time limit 400 ms
Memory limit 64 MB

Reference Code

#include <stdio.h>
int main()
{
    
    
    int i, flag=1;
    double sum=0, n=1.0, eps;
    scanf("%lf", &eps);
    if (eps>=1)
        printf("sum = %.6f", n);
    else {
    
    
        for(i=1; n>eps; i++) {
    
    
            n = 1.0/(3*i-2);
            sum += n*flag;
            flag =- flag;
        }
        printf("sum = %.6f", sum);
    }
    return 0;
}

7-5 Best Couple Height Difference (Score 10)

Experts found through multiple groups of couple research data that the optimal height difference between couples follows a formula: (the woman’s height) × 1.09 = (the man’s height). If they match, the height difference between you two will be the most harmonious difference whether you are holding hands, hugging, or kissing.

Now please write a program to calculate the optimal height of his/her couple for any user.

Input format:
The first line of input gives a positive integer N (≤10), which is the number of users who come to query. Then there are N lines, each line gives the gender and height of the user who came to query in the format of "Gender Height", where "Gender" is "F" for female and "M" for male; "Height" is the interval [1.0, 3.0 ] between real numbers.

Output format:
For each query, calculate the optimal height of the couple for the user in one row, retaining 2 decimal places.

Input example:

2
M 1.75
F 1.8

Output sample:

1.61
1.96

Code length limit 16 KB
Time limit 400 ms
Memory limit 64 MB

Reference Code

#include<stdio.h>
int main()
{
    
    
    int n, i;
    char ch, x;
    double h;
    scanf("%d", &n);
    scanf("%c", &x);
    for(i=1; i<=n; i++) {
    
    
        scanf("%c %lf%c", &ch, &h, &x);
        if (ch=='M') h /= 1.09;
        else h *= 1.09;
        printf("%.2lf\n", h);
    }
    return 0;
}

7-6 Convert abbreviation to full name (score 15)

Enter several characters as abbreviations in one line, with no spaces between characters, and generate output according to the following rules:

  • Each abbreviation letter corresponds to a line of output
  • If the character is C or c, then this line outputs "BEIJING OLYMPIC GAMES"
  • If the character is J or j, then the line outputs "JAPAN WORLD CUP"
  • If the character is K or k, then the line outputs "KOREA WORLD CUP"
  • If it is other characters, the line will output the characters as they are.

Input format:
Enter several characters in one line, with no spaces between characters, and end with a carriage return.

Output format:
Generate several lines of output according to the requirements of the question.

Input example:

kijckj

Output sample:

KOREA WORLD CUP
i
JAPAN WORLD CUP
BEIJING OLYMPIC GAMES
KOREA WORLD CUP
JAPAN WORLD CUP

Code length limit 16 KB
Time limit 400 ms
Memory limit 64 MB

Reference Code

#include <stdio.h>
int main()
{
    
    
    int ch;
    while((ch=getchar()) != '\n')
        if (ch=='c' || ch=='C')
            printf("BEIJING OLYMPIC GAMES\n");
        else if (ch=='J' || ch=='j')
            printf("JAPAN WORLD CUP\n");
        else if (ch=='K' || ch=='k')
            printf("KOREA WORLD CUP\n");
        else
            printf("%c\n", ch);
    return 0;
}

7-7 Reducing the simplest fraction (fraction 15)

The fraction can be expressed in the form分子/分母. Write a program that asks the user to enter a fraction and then reduces it to its simplest fraction. The simplest fraction means that the numerator and denominator have no components that can be reduced. For example, 6/12 can be roughly divided into 1/2. When the numerator is greater than the denominator, there is no need to express it in the form of an integer or a fraction, that is, 11/8 or 11/8; and when the numerator and denominator are equal, it is still expressed in the form of a fraction of 1/1.

Input format:
Enter a fraction in one line, with the numerator and denominator separated by slashes/, such as: 12/34 means 12/34. The numerator and denominator are both positive integers (excluding 0, if you don't know the definition of a positive integer).

hint:

  • For C language, add to the format string of scanf and let handle the slash . /scanf
  • For Python language, use code like a,b=map(int, input().split('/')) to handle this slash.

Output format:
Output the simplest fraction corresponding to this fraction in one line. The format is the same as the input, that is, it is expressed in the form of 分子/分母 Fraction. For example,
5/6 means 5/6.

Input example:

66/120

Output sample:

11/20

Code length limit 16 KB
Time limit 400 ms
Memory limit 64 MB

Reference Code

#include<stdio.h>
int main()
{
    
    
    int i, a=0, b=0;
    scanf("%d/%d", &a, &b);
    for(i=b; i>1; i--)
        if(a%i==0 && b%i==0){
    
    
            a /= i;
            b /= i;
        }
    printf("%d/%d", a, b);
    return 0;
}

Code length limit 16 KB
Time limit 400 ms
Memory limit 64 MB

7-8 Monkey eating peach problem (score 15)

A monkey picked a few peaches on the first day and ate half of them immediately. When he was not satisfied, he ate one more. The next morning, he ate half of the remaining peaches and ate one more. From then on, I ate half of what was left from the previous day and one more every morning. On the Nth morning, when I wanted to eat again, I saw that there was only one peach left. Question: How many peaches were picked on the first day?

Input format:
The input gives a positive integer N (1<N≤10) in one line.

Output format:
Output in one line how many peaches were picked on the first day.

Input example:

3

Output sample:

10

Code length limit 16 KB
Time limit 400 ms
Memory limit 64 MB

Reference Code

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

Guess you like

Origin blog.csdn.net/qq_45801887/article/details/134772898