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 ...
随机推荐
- 《转》python学习(12)-列表解析
转自 http://www.cnblogs.com/BeginMan/p/3164937.html 一.列表解析 列表解析来自函数式编程语言(haskell),语法如下: [expr for iter ...
- java高级---->Thread之单例模式的使用
这里我们介绍一下在多线程中如何安全正确的编写单例模式的代码.不知为何,恰如其分的话总是姗姗来迟,错过最恰当的时机. 多线程中的单例模式 这里面通过代码来体会一下在多线程中如何正确的编写单例模式的代码. ...
- [转]C++中模板的特化与偏特化
转载自:http://hi.baidu.com/klcdyx2008/blog/item/5adbf77b79f316f90bd1873c.html 1.引言C++中的模板分为类模板和函数模板,虽然它 ...
- Android自定义组件——四个方向滑动的菜单实现
今天无意中实现了一个四个方向滑动的菜单,感觉挺好玩,滑动起来很顺手,既然已经做出来了就贴出来让大家也玩弄一下. 一.效果演示 (说明:目前没有安装Android模拟器,制作的动态图片太卡了,就贴一下静 ...
- sencha touch 侧边栏扩展(只隐藏不销毁)
基于Ext.ux.MenuButton改造而来,和它不同的是,不会每次都去销毁侧边栏,只是单纯的隐藏,属性配置方面没啥区别,每次点击按钮显示时,会触发showMenu事件/方法 代码如下: /** * ...
- SecureCRT无法使用root账户远程连接ubuntu
========1.问题============ SecureCRT无法使用root账户远程连接ubuntu 用其他账户连接,正常 用root账户连接,不能连接 =========2.原因====== ...
- myisam innodb 次级 索引的区别
MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的是数据记录的地址.下图是MyISAM索引的原理图: 这里设表一共有三列,假设我们以Col1为主键,则上图是一个MyISAM表的主索 ...
- linux下的一些操作命令
1.切换到root账号下: su root 输入密码: 2.修改root账号密码: sudo passwd root 输入密码: 3.cat用法: 查看文件内容 cat 文件名 创建文件 ...
- JS如何遍历Object中的所有属性?
JS如何遍历Object中的所有属性? var params = ""; for(var i in baseParams){ params += "&" ...
- yii---生产链接的方法
yii生成链接的方法: Yii::$app->urlManager->createUrl('xxx/xxx') <?= Yii::$app->urlManager->cr ...