码迷,mamicode.com
首页 > 其他好文 > 详细

PAT1132: Cut Integer

时间:2017-10-07 17:44:50      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:输入   info   name   pos   pre   file   sed   first   sample   

1132. Cut Integer (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z = 167334, we have A = 167 and B = 334. It is interesting to see that Z can be devided by the product of A and B, as 167334 / (167 x 334) = 3. Given an integer Z, you are supposed to test if it is such an integer.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<= 20). Then N lines follow, each gives an integer Z (10<=Z<=231). It is guaranteed that the number of digits of Z is an even number.

Output Specification:

For each case, print a single line "Yes" if it is such a number, or "No" if not.

Sample Input:
3
167334
2333
12345678
Sample Output:
Yes
No
No

思路
水题
1.首先如果输入的数Z的位数为奇数肯定不满足条件,输出No。
2.如果A乘B为0,肯定不满足条件,输出No
3.剩下情况检查Z % (A*B) 是否为0即可。
代码
#include<iostream>
#include<string>
using namespace std;
int main()
{
  int N;
  while(cin >> N)
  {
      while(N--)
      {
          string num;
          cin >> num;
          int len = num.size();
          if(len % 2 == 1)
          {
              cout << "No" << endl;
              continue;
          }

          string a = num.substr(0,len/2);
          string b = num.substr(len/2,len/2);
          int A = stoi(a),B = stoi(b),Z = stoi(num);
          if(A * B != 0 && Z % (A * B) == 0)
          {
              cout << "Yes" << endl;
          }
          else
              cout << "No" << endl;

      }
  }
}

 

PAT1132: Cut Integer

标签:输入   info   name   pos   pre   file   sed   first   sample   

原文地址:http://www.cnblogs.com/0kk470/p/7635057.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!