[PAT Class B Winter 2020] 7-1 Ancestral Good Luck (15 points)

We first define that 0 to 9 are good luck numbers , and then start from a certain good luck number and continue to add numbers to the right to form new numbers. We say that a number N greater than 9 has ancestral good luck , if it is obtained by adding a single digit to a certain good luck number, and it can be evenly divided by its own digits.

For example, 123 is an ancestral good luck number. First of all, because 1 is the ancestor of a good luck number, after adding 2, the resulting 12 can be divisible by its digit 2 (that is, 12 is a two-digit number), so 12 is an ancestral good luck number; add it after 12 After 3, the formed 123 can be divisible by its digit 3, so 123 is an ancestral good fortune number.

This question asks you to judge whether a given positive integer N has good luck from ancestors.

Input format:

Each input contains 1 test case. The first line of each test case gives a positive integer K (≤1000); the second line gives K positive integers not more than 10​9​​ to be evaluated. Note that these numbers are guaranteed to have no extra leading zeros.

Output format:

For each number to be evaluated, output in one line  Yes if it is an ancestral good luck number, if not, output it  No.

Input sample:

5
123 7 43 2333 56160

Sample output:

Yes
Yes
No
No
Yes

Code: 

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int k;
    cin>>k;
    string s; 
    for(int i=0;i<k;i++){
        int cnt=0;
        cin>>s;
        for(int j=0;j<s.length();j++){
            int t=stoi(s.substr(0,j+1));
            if(t%(j+1)==0){
                cnt++;
                continue;
            } else break;
        }
        if(cnt==s.length()) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}

13 minutes

Guess you like

Origin blog.csdn.net/WKX_5/article/details/114477354
Recommended