PAT 1023 Have Fun with Numbers[大数乘法][一般]
1023 Have Fun with Numbers (20)(20 分)
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.
Input Specification:
Each input file contains one test case. Each case contains one positive integer with no more than 20 digits.
Output Specification:
For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.
Sample Input:
1234567899
Sample Output:
Yes
2469135798
题目大意: 对于输入一个不超过20位的数,判断*2之后的结果b的各位是否恰巧是原来的数组成的(不包含新的数)。
#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std; int hasN[],hasM[];
int main(){
//用long long好吗?,使用long long在牛客网上通过率为56.25%。
//需要模拟大数乘法。
//直接用string好了,可以reverse.
char ch[];
scanf("%s",&ch);
int len=strlen(ch);
for(int i=;ch[i]!='\0';i++){
hasN[ch[i]-'']+=;//计算所含每个数的个数。
}
//将字符数组反转。
int cha;
for(int i=;i<len/;i++){//
cha=ch[i];
ch[i]=ch[len-i-];
ch[len-i-]=cha;
}
int jin=,tp=,i;
for(i=;ch[i]!='\0';i++){
tp=(ch[i]-'')*+jin;
if(tp<) {
ch[i]=tp+'';
jin=;
}
else {
ch[i]=(tp%+'');
jin=;
}
}
if(jin==){
ch[i]='';
ch[i+]='\0';
}
bool flag=true;
for(int i=;ch[i]!='\0';i++){
hasM[ch[i]-'']+=;
} for(int i=;i<;i++){
if(hasN[i]!=hasM[i]){
flag=false;break;
}
}
if(flag)
cout<<"Yes\n";
else
cout<<"No\n";
//printf("%s",ch);
//计算结果应该倒序输出
len=strlen(ch);
for(int i=len-;i>=;i--){
printf("%c",ch[i]);
}
return ;
}
//这是我的AC代码,在牛客网上提交了3次。
1.直接用long long通不过,因为有很大的数。
2.使用字符数组,发现最终结果是错误的,因为没有将其倒序输出。
3.提交一个也没通过,因为发现,模拟数乘,需要将初始的倒序。
总之就是:将初始倒序,相乘,结果倒序输出。
学习到了:
1.判断字符数组的长度,使用strlen,头文件是<string.h>,不包括'\0'。
2.使用scanf读入字符输出,结束处会自己带一个'\0'。
PAT 1023 Have Fun with Numbers[大数乘法][一般]的更多相关文章
- PAT 1023 Have Fun with Numbers
1023 Have Fun with Numbers (20 分) Notice that the number 123456789 is a 9-digit number consisting ...
- pat 1023 Have Fun with Numbers(20 分)
1023 Have Fun with Numbers(20 分) Notice that the number 123456789 is a 9-digit number consisting exa ...
- PAT 甲级 1023 Have Fun with Numbers (20 分)(permutation是全排列题目没读懂)
1023 Have Fun with Numbers (20 分) Notice that the number 123456789 is a 9-digit number consisting ...
- PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642
PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...
- PAT 甲级 1023 Have Fun with Numbers(20)(思路分析)
1023 Have Fun with Numbers(20 分) Notice that the number 123456789 is a 9-digit number consisting exa ...
- hdu1313 Round and Round We Go (大数乘法)
Problem Description A cyclic number is an integer n digits in length which, when multiplied by any i ...
- 51nod 1027大数乘法
题目链接:51nod 1027大数乘法 直接模板了. #include<cstdio> #include<cstring> using namespace std; ; ; ; ...
- [POJ] #1001# Exponentiation : 大数乘法
一. 题目 Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 156373 Accepted: ...
- 用分治法实现大数乘法,加法,减法(java实现)
大数乘法即多项式乘法问题,求A(x)与B(x)的乘积C(x),朴素解法的复杂度O(n^2),基本思想是把多项式A(x)与B(x)写成 A(x)=a*x^m+b B(x)=c*x^m+d 其中a,b,c ...
随机推荐
- 【数据处理】SQL Server高效大数据量存储方案SqlBulkCopy
要求将Excel数据,大批量的导入到数据库中,尽量少的访问数据库,高性能的对数据库进行存储. 一个比较好的解决方案,就是采用SqlBulkCopy来处理存储数据. SqlBulkCopy存储大批量的数 ...
- SQL SERVER 2008 R2安全配置与防暴力破解
https://blog.csdn.net/enweitech/article/details/49864215 0x00 sql server 2008 权限介绍 在访问sql server 200 ...
- 【WEB前端开发最佳实践系列】CSS篇
一.有效组织CSS代码 规划组织CSS代码:组织CSS代码文件,所有的CSS都可以分为2类,通用类和业务类.代码的组织应该把通用类和业务类的代码放在不同的目录中. 模块内部的另一样式规则:样式声明的顺 ...
- SharpGL学习笔记(七) OpenGL的变换总结
笔者接触OpenGL最大的困难是: 经常调试一份代码时, 屏幕漆黑一片, 也不知道结果对不对,不知道如何是好! 这其实就是关于OpenGL"变换"的基础概念没有掌握好, 以至于对& ...
- LeetCode 42 Trapping Rain Water(积水体积)
题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description Problem: 根据所给数组的值,按照上图的示意 ...
- Linux 开启VNCSERVER
尽管我们可以使用 SSH连接远程通过字符界面来操作Linux,但是对于更多熟悉图形人来说是很不方便的,因此开启Linux的远程桌面还是很有必要的.目前有两种比较流 行的方式:XDM(X display ...
- 2527: [Poi2011]Meteors[整体二分]
2527: [Poi2011]Meteors Time Limit: 60 Sec Memory Limit: 128 MB Submit: 1528 Solved: 556 [Submit][S ...
- python中的null值
在一个没有接口文档的自动化测试中,只能通过抓包及查日志查看发送的信息,其中有一个接口发送的信息如下: enable_snapshot": true, "new_size" ...
- IntelliJ IDEA导出Java 可执行Jar包
extends:http://blog.sina.com.cn/s/blog_3fe961ae0102uy42.html 保证自己的Java代码是没有问题的,在IDEA里面是可以正常运行的,然后,按下 ...
- ftok函数
ftok函数 系统建立IPC通讯(消息队列.信号量和共享内存)时必须指定一个ID值.通常情况下,该id值通过ftok函数得到. ftok原型 头文件: #include <sys/types.h ...