当前位置:网站首页>"Analysis of PAT B-level real questions" Basic Level 1091 N-self-defense number (problem analysis + complete steps + pseudo-code description + submitted pass code)

"Analysis of PAT B-level real questions" Basic Level 1091 N-self-defense number (problem analysis + complete steps + pseudo-code description + submitted pass code)

2022-11-24 21:32:05Mingzai's Sunny Afternoon

乙级的题目训练主要用来熟悉编程语言的语法和形成良好的编码习惯和编码规范.从小白开始逐步掌握用编程解决问题.

PAT (Basic Level) Practice 1091 N-自守数

问题分析

The title given a definition, 给定一组数, Request output can let each number satisfies the definition of positive integer problem set, When there is no such positive integer output"No".
需要满足的条件是, 正整数NMultiplied by a given number ofKThe product of the square to get toK为结尾.

How to check if a number with another number at the end

方案一: If stored in the form of numerical number two

To obtain a numerical all the Numbers on a digital,Need to use more than take on from after obtain.
So if the two Numbers are in numerical form storage, Need to two Numbers at the same time for10取余,If the remainder is different that does not meet the requirement.
If until a number is equal to0, There was no remainder different situations, 则满足要求.

方案二: If the string is stored two Numbers

Compute the number of digits of the shorter, Then use index access directly compare.

Complete description process

  1. 获取输入: To check the positive integer number, Given the positive integer
  2. For every positive integer:
    • 系数从1到9, 进行以下检查:
      • If the coefficient multiplied by the positive integer square to get the product, End with positive integer, 则:
        • Output the coefficient and get the product.
        • Directly into the next positive integer processing
    • If the coefficient of1到9都不符合要求, 则输出"No".

伪代码描述

  1. get input: case_amount;
  2. for each case:
    • get input: number
    • init flag:
      • found = False
    • calculate square = number * number;
    • for coefficient in range(1, 10):
      • calculate product = coefficient * square;
      • if is_postfix(number, product):
        • found = True;
        • print(“{number} {product}”);
        • break;
    • if not found:
      • print(“No”)

完整提交代码

/* # 问题分析 The title given a definition, 给定一组数, Request output can let each number satisfies the definition of positive integer problem set, When there is no such positive integer output"No". 需要满足的条件是, 正整数NMultiplied by a given number ofKThe product of the square to get toK为结尾. ## How to check if a number with another number at the end ### 方案一: If stored in the form of numerical number two To obtain a numerical all the Numbers on a digital,Need to use more than take on from after obtain. So if the two Numbers are in numerical form storage, Need to two Numbers at the same time for10取余,If the remainder is different that does not meet the requirement. If until a number is equal to0, There was no remainder different situations, 则满足要求. ### 方案二: If the string is stored two Numbers Compute the number of digits of the shorter, Then use index access directly compare. # Complete description process 1. 获取输入: To check the positive integer number, Given the positive integer 2. For every positive integer: - 系数从1到9, 进行以下检查: - If the coefficient multiplied by the positive integer square to get the product, End with positive integer, 则: - Output the coefficient and get the product. - Directly into the next positive integer processing - If the coefficient of1到9都不符合要求, 则输出"No". # 伪代码描述 1. get input: case_amount; 2. for each case: - get input: number - init flag: - found = False - calculate square = number * number; - for coefficient in range(1, 10): - calculate product = coefficient * square; - if is_postfix(number, product): - found = True; - print("{number} {product}"); - break; - if not found: - print("No") */

# include<stdio.h>

int meet_condition(int number, int result){
    
    while (number != 0){
    
        int remainder = number % 10;
        if (result % 10 != remainder){
    
            return 0;
        }
        
        number /= 10;
        result /= 10;
    }
    return 1;
}

int main(){
    
    int case_amount;
    scanf("%d", &case_amount);
    
    int number, square, result;
    int found;
    for (int i = 0; i < case_amount; i++){
    
        scanf("%d", &number);
        square = number * number;
        found = 0;
        for (int j = 1; j < 10; j++){
    
            result = square * j;
            if (meet_condition(number, result) == 1){
    
                printf("%d %d\n", j, result);
                found = 1;
                break;
            }
        }
        if (found == 0){
    
            printf("No\n");
        }
    }
    
    
    return 0;
}
原网站

版权声明
本文为[Mingzai's Sunny Afternoon]所创,转载请带上原文链接,感谢
https://chowdera.com/2022/11/202211242126213881.html

随机推荐