PAT-1132 Cut Integer (整数分割)
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
题目大意:将一个k位(k为偶数)的整数拆成两半(即前k/2位 和 后k/2位),要求判断原数是否能整除于这两个数的积。
主要思路:很简单的题目,只需要写一个计算整数位数的函数,将整数不断除以10,直到该数变为0即可累积得到该数的位数k。将该数除以10^(k/2)可得到前半部分,而求余则可以得到后半部分,最后用原数对两部分的积求模即可完成判断。需要注意的是,如果两部分的乘积为0则会使除数为0,出现浮点错误,也就是说在取模之前需要判断后半部分数是否为0,如果为0则直接输出No。
#include <iostream>
#include <cmath>
using namespace std;
//计算整数的位数
int count_bit(int x) {
int count = 0;
while (x > 0) {
x /= 10;
count++;
}
return count;
}
int main(void) {
int n, z, i;
cin >> n;
for (i = 0; i < n; i++) {
cin >> z;
int mid = count_bit(z) / 2;
int t = pow(10, mid);
// z % t == 0 会产生浮点错误,比如当 z = 1000 时
if (z % t > 0 && z % ((z % t) * (z / t)) == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
PAT-1132 Cut Integer (整数分割)的更多相关文章
- PAT 1132 Cut Integer[简单]
1132 Cut Integer(20 分) Cutting an integer means to cut a K digits lone integer Z into two integers o ...
- PAT 1132 Cut Integer
1132 Cut Integer (20 分) Cutting an integer means to cut a K digits lone integer Z into two integer ...
- pat 1132 Cut Integer(20 分)
1132 Cut Integer(20 分) Cutting an integer means to cut a K digits lone integer Z into two integers o ...
- PAT 甲级 1132 Cut Integer
https://pintia.cn/problem-sets/994805342720868352/problems/994805347145859072 Cutting an integer mea ...
- PAT Advanced 1132 Cut Integer (20) [数学问题-简单数学]
题目 Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long ...
- PAT A1132 Cut Integer (20 分)——数学题
Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long int ...
- 1132. Cut Integer (20)
Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long int ...
- 1132 Cut Integer
题意:略. 思路:注意除数可能为0的情况,不然会导致浮点错误. 代码: #include <iostream> #include <string> using namespac ...
- PAT_A1132#Cut Integer
Source: PAT A1132 Cut Integer (20 分) Description: Cutting an integer means to cut a K digits lone in ...
- PAT1132: Cut Integer
1132. Cut Integer (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Cutting a ...
随机推荐
- 微软宣布一批新获得Microsoft Teams认证的会议硬件
COVID-19 疾病流行期间,微软见到了 Microsoft Teams 视频会议解决方案取得的巨大增长.然而在许多情况下,生产力与音视频硬件的质量息息相关.好消息是,该公司刚刚完成了一批第三方硬件 ...
- 【Linux常见命令】head命令
head - output the first part of files 读取文件的前n行,默认前10行 语法: head [OPTION]... [FILE]... 参数: -n 数字 显示前n行 ...
- 【Linux常见问题】CentOS 7 root用户密码忘记,找回密码方法
1.开机按esc 2.选择CentOS Linux (3.10.0-693.......) 按 e 键: 3.光标移动到 linux 16 开头的行,找到 ro 改为 rw init=sysr ...
- 5) ModelSerializer(重点) 基表 测试脚本 多表关系建外键 正反查 级联 插拔式连表 序列化反序列化整合 增删查 封装response
一.前戏要做好 配置:settings.py #注册drf INSTALLED_APPS = [ # ... 'api.apps.ApiConfig', 'rest_framework', ] # ...
- django源码解读——runserver分析
开始之前建议先参考一下这篇文章:https://blog.csdn.net/qq_33339479/article/details/78862156class Command(BaseCommand) ...
- python call java jar
python脚本如何调用Java的jar文件呢? HelloWorld.java public class HelloWorld { public static void main(String[] ...
- win7 64位系统使用vs2010编译OSG3.2.1
首先我想说的是,osg是有二进制安装包的:http://openscenegraph.alphapixel.com/osg/downloads/free-openscenegraph-binary-d ...
- Linux权限管理、系统进程管理
权限管理 linux系统中分为四种角色 u=user 当前用户 g=group 同组用户 o=other 其他用户 a=all 代表所有用户 三种权限 r=read 可读 w=write ...
- dbcp数据源连接池
一.数据源连接池 我们之前利用jdbc连接数据库,每次都要创建连接对象,销毁连接对象,如果并发访问量比较大,这样肯定比较辣 浪费数据库的效率,我们可以像之前mybatis中缓存查询到的数据一样,可以把 ...
- 自动化运维工具Ansible之Roles测验详解
Ansible Roles 详解与实战案例 主机规划 添加用户账号 说明: 1. 运维人员使用的登录账号: 2. 所有的业务都放在 /app/ 下「yun用户的家目录」,避免业务数据乱放: 3. 该用 ...