PTA Level B Practice Questions

PAT (Basic Level) Practice (Chinese) question


1001 The (3n+1) conjecture that killed people without paying for their lives (15 points)

Callatz conjecture:

For any positive integer n, if it is even, cut it in half; if it is odd, cut (3n+1) in half. This has been repeated repeatedly, and finally n=1 must be obtained at a certain step. Karaz announced this conjecture at the World Congress of Mathematicians in 1950. According to legend, teachers and students of Yale University were mobilized to prove this seemingly silly and innocent proposition. As a result, the students were unwilling to study. +1), so that some people say that this is a conspiracy, Karaz is deliberately delaying the progress of teaching and scientific research in American mathematics...

Our topic today is not to prove the Karaz conjecture, but to simply count any given positive integer n that does not exceed 1000. How many steps (cut several times) does it take to get n=1?

Input format:

Each test input contains 1 test case, which gives the value of a positive integer n.

Output format:

Output the number of steps required to calculate from n to 1.

Input sample:

3

Sample output:

5

java code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


//import java.util.Scanner;

public class BasicOne {
    
    

	public static void main(String[] args) throws NumberFormatException, IOException {
    
    
//		Scanner scanner = new Scanner(System.in);
//		int n = scanner.nextInt();
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(reader.readLine());
		
		int step = 0;
		
		while(n!=1) {
    
    
			
			if(n%2==0) {
    
    
				n = n/2;
				step++;
			}else {
    
    
				n = 3*n+1;
			}
		}
		
		System.out.print(step);
	}
}

python code

n = int(input())
step = 0


while True:

    if n == 1:
        break

    if n%2 == 0:

        step+=1
        n = n/2

    else:

        n = 3*n+1    

print(step)

C++ code

# include<iostream>
using namespace std;

int main(){
    
    
    int n;
    int step=0;

    cin >> n;

    while(n!=1){
    
    

        if(n%2==0){
    
    
            step++;
            n=n/2;
        }else{
    
    
            n = 3*n+1;
        }
    }
    cout << step;
}

1002 Write this number (20 points)

Read in a positive integer n , calculate the sum of its digits, and write each digit of the sum in Chinese Pinyin.

Input format:

Each test input contains 1 test case, which gives the value of the natural number n . Here, it is guaranteed that n is less than 10100.

Output format:

Output each digit of the sum of the digits of n in a line. There is a space between the pinyin numbers, but there is no space after the last pinyin number in a line.

Input sample:

1234567890987654321123456789

Sample output:

yi san wu

java code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {
    
    

	public static void main(String[] args) throws IOException {
    
    
		
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		
		String ss = bf.readLine();
				
				
		StringBuffer so = new StringBuffer();
		
		int sum = 0;
		for(int i=0;i<ss.length();i++) {
    
    
			sum += (ss.charAt(i)-48);
		}
		ss = sum+"";
		
		for(int i=0;i<ss.length();i++) {
    
    
			
			switch (ss.charAt(i)-48) {
    
    
			
				case 0:
					so.append("ling ");
					break;
					
				case 1:
					so.append("yi ");
					break;
					
				case 2:
					so.append("er ");
					break;
					
				case 3:
					so.append("san ");
					break;
				
				case 4:
					so.append("si ");
					break;
					
				case 5:
					so.append("wu ");
					break;
					
				case 6:
					so.append("liu ");
					break;
				
				case 7:
					so.append("qi ");
					break;
					
				case 8:
					so.append("ba ");
					break;
					
				case 9:
					so.append("jiu ");
					break;
	
				default:
					break;
			}
		}
		so.deleteCharAt(so.length()-1);
		bf.close();
		System.out.print(so);
	}
}

python code

s = input()

num_sum = str(sum(map(int,list(s))))

ss=""

L = {
    
    "1":"yi","2":"er","3":"san","4":"si","5":"wu","6":"liu","7":"qi","8":"ba","9":"jiu"}

for i in num_sum:

    ss+=(L[i]+" ")


print(ss[:-1])

c++ code

# include<iostream>
# include<string>

using namespace std;

int main(){
    
    

    string so,ss,s1;
    int sum = 0,x;

    cin >> s1;



    for(int i=0;i<s1.length();i++) {
    
    
        sum += (s1[i]-48);
    }

    while(sum>0){
    
    

        x = sum%10;
        sum = sum/10;

        ss = char(x+48)+ss;
    }

//    cout << ss;

    for(int i=0;i<ss.size();i++) {
    
    

        switch (ss[i]-48) {
    
    
            case 0:
                so.append("ling ");
                break;
            case 1:
                so.append("yi ");
                break;
            case 2:
                so.append("er ");
                break;
            case 3:
                so.append("san ");
                break;

            case 4:
                so.append("si ");
                break;

            case 5:
                so.append("wu ");
                break;

            case 6:
                so.append("liu ");
                break;

            case 7:
                so.append("qi ");
                break;

            case 8:
                so.append("ba ");
                break;

            case 9:
                so.append("jiu ");
                break;

            default:
                break;
        }
    }
    so.erase(so.end()-1);
    cout << so;
}

Guess you like

Origin blog.csdn.net/qq_46456049/article/details/109511101