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 ...
随机推荐
- Sqlserver 数据库、表常用查询操作
查询所有表以及记录数: select a.name as 表名,max(b.rows) as 记录条数 from sysobjects a ,sysindexes b where a.id=b.id ...
- python 函数式编程:高阶函数,map/reduce
python 函数式编程:高阶函数,map/reduce #函数式编程 #函数式编程一个特点就是,允许把函数本身作为参数传入另一个函数,还允许返回一个函数 #(一)高阶函数 f=abs f print ...
- Android.mk(4) 依赖:目标编程的模式
https://www.jianshu.com/p/3777a585a8d0 另一种范式 我一直觉得,Makefile确实是C/C++程序员的良配,因为Makefile所使用的两种范式都是C/C++程 ...
- Shell语法整理,持续维护
Shell shell条件表达式: CONDITIONAL EXPRESSIONSConditional expressions are used by the [[ compound command ...
- 如何在mysql中查询每个分组的前几名
问题 在工作中常会遇到将数据分组排序的问题,如在考试成绩中,找出每个班级的前五名等. 在orcale等数据库中可以使用partition 语句来解决,但在MySQL中就比较麻烦了.这次翻译的文章就是 ...
- vue开发 - 将方法绑定到window对象,给app端调用
通过jsBridge方法,H5可以调用客户端(ios,android)的内部方法,同样,客户端也需要能调用H5页面里定义的js方法,但是在vue里,所有的方法都是在组件内部声明的,也只能在组件内部调用 ...
- STM32 ADC转换时间
STM32F103XX的ADC的采样时钟最快14MHz,最快采样率为1MHz. ADC时钟: 这个ADC时钟是从哪来的呢.我们看下面这个STM32的时钟结构图: 我们大多使用STM32的最快PCLK2 ...
- 手写代码UI,xib和StoryBoard间的的优劣比较
在UI制作方面,逐渐分化三种主要流派:使用代码手写UI:使用单个xib文件组织viewController或者view:使用StoryBoard来通过单个或很少的几个文件构建UI.三种方式各有优劣,也 ...
- CSS基础问题
1.css引入问题 本来以为css引入是很简单的问题,但是在写demo中,使用连接方式引入就出现了问题,找了半天,还是说一下问题吧. 在引入时没有写rel="stylesheet" ...
- Scala学习笔记(3)-表达式归纳
语法:使用表达式定义值和变量 val <identifier>[:<type>]=<expression> 字面值类型 var <identifier> ...