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 ...
随机推荐
- 【laravel5.6】 Laravel 数据迁移给表和字段添加注释
1 引用DB use Illuminate\Support\Facades\DB; 2 up方法 public function up() { Schema::create('code_table', ...
- springboot---->springboot中的格式化(一)
这里面我们简单的学习一下springboot中关于数据格式化的使用.我以为你不是个好人,没想到你连个坏人都不是. springboot中的格式化 我们的测试环境是springboot,一个将字符串格式 ...
- java(2) 面向对象
1.类的封装 *在定义一个类时,将类中的属性私有化,即使用prviate关键字来修饰,私有属性只能在它所在的类中被访问.为了能让外界访问私有属性,需要提供一些使用public修饰的公有方法,其中包括用 ...
- rpm包安装失败的解决办法
author: headsen chen date : 2018-10-15 14:24:36 1,故障现象: # rpm -ivh zabbix-agent--.el6.x86_64.rpm ...
- 关于hp proliant sl210t服务器远程iLO接口的管理配置
提到hp proliant sl210t服务器,首先得提到HP ProLiant SL2500,HP ProLiant SL2500 可扩展系统提供了 一个非常密集的解决方案,可在标准 2U 机箱最多 ...
- 【CF802C】Heidi and Library (hard) 费用流
[CF802C]Heidi and Library (hard) 题意:有n个人依次来借书,第i人来的时候要求书店里必须有种类为ai的书,种类为i的书要花费ci块钱购入.而书店的容量只有k,多余的书只 ...
- 【JAVA】猜数字
import java.util.*; public class GN { public static void main(String arg[]) { ;// 数字标记 ;// 位置标记 ;// ...
- url分发、isinstance、request.GET请求之QueryDict和urlencode、post和get请求、limit_choices_to(Model字段)
这个的路径是怎么来的,是有一个个的url路由分发过来的 这两个是相等的,若url后面加括号了,那么前面就不用这个装饰器了:反之,若装饰器使用了,那么这个url后面就不要加括号了 eg:其他的views ...
- 利用javascript判断文件是否存在
1 判断本地文件是否存在 var fso,s=filespec; // filespec="C:/path/myfile.txt" fso=new ActiveXObject(&q ...
- poj1269 intersecting lines【计算几何】
We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a p ...