PAT(Advanced Level)A1132. Cut Integer

PAT(Advanced Level)A1132. Cut Integer

题意

将一个长度为K的数字Z对半分,前半部分和后半部分各自构成了A和B两个数字,问A*B是否为Z的因子

思路

  • 模拟题,说得很清楚了,我们把数字当作字符串来处理,结合stoi()函数会很方便
  • ⚠️注意取余操作是不能对0取余的

代码

#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <cmath>
using namespace std;
int main() {
    int N;
    cin >> N;
    string s;
    for(int i = 0; i < N; i++) {
        cin >> s;
        int A = stoi(s.substr(0, s.size() / 2));
        int B = stoi(s.substr(s.size() / 2));
        int Z = stoi(s);
        if(A != 0 && B != 0 && Z % (A * B) == 0)
            cout << "Yes
";
        else
            cout << "No
";
    }
    return 0;
}