[PTA-C Language] Experiment 4-Loop Structure II

7-1 Draw Squares with Obama (Score 15)

U.S. President Obama not only called on everyone to learn programming, but even set an example by writing code, becoming the first president in U.S. history to write computer code. At the end of 2014, to celebrate the official launch of Computer Science Education Week, Obama wrote a very simple computer code: draw a square on the screen. Now you can paint with him too!

Input format:
Enter the length of the side of the square N (3≤N≤21) and a certain character that makes up the side of the square in one lineC, separated by a space.

Output format:
Output the square drawn by the given charactersC. But notice that the row spacing is larger than the column spacing, so in order to make the result look more square, the number of rows we output is actually 50% of the number of columns (rounded up).

Input example:

10 a

Output sample:

aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa

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

Reference Code

#include<stdio.h>
int fun(int n)
{
    
    
    double t = n*1.0/2;
    if(t+0.5>=(int)t+1.0)
        return (int)t+1;
    else
        return (int)t;
}
int main()
{
    
    
    int n, i, j;
    char c;
    scanf("%d %c", &n, &c);
    int t = fun(n);
    for(i=0; i<t; i++) {
    
    
        for(j=0; j<n; j++)
            printf("%c", c);
        printf("\n");
    }
    return 0;
}

7-2 Print the nine-nine formula table (score 10)

The following is a complete lower triangle nine-nine formula table:

1*1=1   
1*2=2   2*2=4   
1*3=3   2*3=6   3*3=9   
1*4=4   2*4=8   3*4=12  4*4=16  
1*5=5   2*5=10  3*5=15  4*5=20  5*5=25  
1*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  
1*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  
1*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  
1*9=9   2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81  

This question requires that for any given positive integerN, output part of the formula from 1*1 to N*N surface.

Input format:
The input gives a positive integer in one lineN (1≤N≤9) .

Output format:
Output lower triangleN*N part of the formula table, in which the number on the right side of the equal sign occupies 4 digits and is left-justified.

Input example:

4

Output sample:

1*1=1   
1*2=2   2*2=4   
1*3=3   2*3=6   3*3=9   
1*4=4   2*4=8   3*4=12  4*4=16  

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

Reference Code

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

7-3 Find the set of integers that meet the given conditions (fraction 15)

Given a positive integer A not exceeding 6, consider 4 consecutive numbers starting from A. Please output all 3-digit numbers without repetitions composed of them.

Input format:
The input gives A in one line.

Output format:
Output the 3-digit number that meets the conditions, from small to large, with 6 integers per line. Integers should be separated by spaces, but there should be no extra spaces at the end of the line.

Input example:

2

Output sample:

234 235 243 245 253 254
324 325 342 345 352 354
423 425 432 435 452 453
523 524 532 534 542 543

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

#include<stdio.h>
int main(){
    
    
    int n, i, j, k;
    int flag=0;
    scanf("%d",&n);
    for(i=n; i<=n+3; i++)
        for(j=n; j<=n+3; j++)
            for(k=n; j!=i && k<=n+3; k++)
                if(k!=i && k!=j) {
    
    
                    if(flag==0) {
    
    
                        printf("%d", i*100+j*10+k);
                        flag++;
                    } else if(flag==5) {
    
    
                        printf(" %d\n", i*100+j*10+k);
                        flag=0;
                    } else {
    
    
                        printf(" %d", i*100+j*10+k);
                        flag++;
                    }
                }
    return 0;
}

7-4 Finding positive integer solutions to special equations (Fraction 15)

This question requires that for any given positive integer N, find the equation X2 + Y2 = all positive integer solutions of N.

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

Output format:
Output equation X2 + Y 2 = All positive integer solutions of N, where X ≤ Y . Each group of solutions occupies one line. The two numbers are separated by 1 space and are output in increasing order of X. If there is no solution, outputNo Solution.

Input example 1:

884

Output sample 1:

10 28
20 22

Input example 2:

11

Output sample 2:

No Solution

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

Reference Code

#include<stdio.h>
int main()
{
    
    
    int x, y, n, flag = 0;
    scanf("%d", &n);
    for (x = 1; x <= 100; ++ x)
        for (y = x; y <= 100; ++ y)
            if (x*x + y*y == n) {
    
    
                printf("%d %d\n", x, y);
                flag = 1;
            }
    if (!flag)
        printf("No Solution\n");
    return 0;
}

7-5 unchanged initial number (score 15)

The constant initial number refers to a special number that when multiplied by 2, 3, 4, 5, 6, 7, 8, and 9 respectively, the sum of the digits of the resulting product remains unchanged. For example, 18 is a number like this: 2 times 18 is 36, 3+6=9; 3 times 18 is 54, 5+4=9;... 9 times 18 is 162, 1+6+2=9. For 18, 9 is its original intention. This question requires you to determine whether any given number has an unchanged original intention.

Input format:
The input gives a positive integer N (≤ 100) in the first line. N lines follow, each giving a positive integer not exceeding 105
.

Output format:
For each given number, if it has an unchanged initial intention, output its initial intention in one line; otherwise, outputNO.

Input example:

4
18
256
99792
88672

Output sample:

9
NO
36
NO

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

Reference Code

#include <stdio.h>
int main(){
    
    
    int n, sum=0, i, t, j, a, f, c=0;
    scanf("%d", &n);
    for(i=0; i<n; i++) {
    
    
        scanf("%d", &a);
        t = a; sum = 0;
        while(t){
    
    
            sum += t%10;
            t /= 10;
        }
        f = sum; c = 0;
        for(j=2; j<=9; j++) {
    
    
            t = a*j;
            sum = 0;
            while(t){
    
    
              sum += t%10;
              t /= 10;
            }
            if(sum==f)
                c++;
        }
        if(c==8)
            printf("%d\n", f);
        else
            printf("NO\n");
    }
    return 0;
}

7-6 Wifi Password (Score 15)

The following is a photo circulated on Weibo: "Dear students, in view of the fact that you sometimes need to use wifi and are afraid of delaying your studies, the wifi password is now set to the answers to the following math questions: A-1; B- 2; C-3; D-4; Students are asked to answer by themselves, changing every two days. Thank you for your cooperation!!~" - Teachers also work hard to promote student learning... This question requires you to write a program to combine a series of The answers to the questions are translated into wifi passwords according to the correspondence given on the paper. It is simply assumed here that each multiple-choice question has 4 options and only 1 correct answer.

Insert image description here

Input format:
The first line of input gives a positive integer N (≤ 100), followed by N lines, each line follows the format of 编号-答案 Given four options for a question, T represents the correct option, and F represents the incorrect option. Separate options with spaces.

Output format:
Output the wifi password in one line.

Input example:

8
A-T B-F C-F D-F
C-T B-F A-F D-F
A-F D-F C-F B-T
B-T A-F C-F D-F
B-F D-T A-F C-F
A-T C-F B-F D-F
D-T B-F C-F A-F
C-T A-F B-F D-F

Output sample:

13224143

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

Reference Code

#include<stdio.h>
int main()
{
    
    
    int i, j, n;
    char a[n][15], b[n];
    scanf("%d", &n);
    getchar();
    for(i=0; i<n; i++)
        gets(a[i]);
    for(i=0; i<n; i++)
        for(j=2; j<15; j+=4)
            if(a[i][j] == 'T')
                b[i] = a[i][j-2];
    for(i=0; i<n; i++)
        if(b[i] == 'A')
            printf("1");
        else if(b[i] == 'B')
            printf("2");
        else if(b[i] == 'C')
            printf("3");
        else
            printf("4");
    return 0;
}

7-7 Mersenne number (fraction 15)

A prime number of the form 2n − 1 is called a Mersenne Number. For example, 22 −1=3, 23−1=7 They are all Mersenne numbers. In 1722, the blind Swiss mathematician Euler proved that 231−1=2147483647 was a prime number, which was called the "known number" in the world at that time. A record of "the largest prime number".

This question requires writing a program to output all Mersennes not exceeding 2n − 1 for any positive integer n (n<20) number.

Input format:
The input gives a positive integer n (n<20) in one line.

Output format:
Output all values ​​not exceeding 2n − 1 in ascending order Mersenne numbers, one per line. If there are none, output "None".

Input example:

6

Output sample:

3
7
31

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

Reference Code

#include<stdio.h>
#include<math.h>
int main()
{
    
    
    int i, j, n, m=0, k=0, c=0;
    scanf("%d", &n);
    for(i=2; i<=n; i++, c=0) {
    
    
        m = pow(2, i)-1;
        for(j=2; j<=sqrt(m); j++)
            if(m%j==0) {
    
    
                c++;
                break;
            }
        if(c==0) {
    
    
            printf("%d\n", m);
            k++;
        }
    }
    if(!k) printf("None");
    return 0;
} 

Guess you like

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