1023 Have Fun with Numbers

PTA(Advanced Level) 1023:Have Fun with Numbers(C语言实现) PTA(Advanced Level) 1023:Have Fun with Numbers(C语言实现) #include <stdio.h> #include <string.h> void Double(char s[]) { int i,c=0,k=strlen(s); for(i=k-1; i>=0; i--,k--) { s[k]=((s[i]... 阅读详情

1023 Have Fun with Numbers (20 point(s))

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 kdigits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

Input Specification:

Each input 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

题意是存在一些数(如123456789),将它们加倍后得到的是这个数的另一个排列。本题要求计算输入的2倍,并且判断它是否是输入的另一个排列。

由于至多有20位,不可以用int甚至long long存储。因此用string读入,并将它存储到数组中,然后按照大数加法/乘法的方式处理。对于另一个排列,必然对于每一个数字,有相同的个数。利用Hash思想,只需要用长度为10的int[]来存储数位个数即可。 

关键点:1. 大数加法/乘法;2. hashTable(判断是否是同一个排列)。

注意点:熟悉大数问题(string转数组、数值的高位在字符串数组的低位、进位处理、多余位处理等)

#include<iostream>
#include<string>
using namespace std;
const int N = 21;
int number[N];
int rec[10]={0};
int rec2[10]={0};
int main(void)
{
	string s;
	cin>>s;
	bool flag = true;
	//记录每个数字的个数,并且存入数组
	int len = s.length();
	int dlen;
	for(int i=len-1;i>=0;i--){
		int tmp = s[i]-'0';
		number[len-1-i] = tmp;
		rec[tmp]++;
	} 
	//大数加法
	int carry = 0;
	for(int i=0;i<len;i++){
		int tmp = carry+2*number[i];
		number[i] = tmp % 10;
		carry=tmp/10; 
	} 
	if(carry!=0){
		number[len]=carry;
		dlen = len+1;
		flag = false;
	}
	else dlen = len;
	for(int i=0;i<dlen;i++){
		rec2[number[i]]++;
	}
	for(int i=0;i<=9;i++){
		if(rec[i]!=rec2[i]){
			flag = false;
			break;
		}
	}
	if(!flag) cout<<"No"<<endl;
	else cout<<"Yes"<<endl;
	for(int i=dlen-1;i>=0;i--) cout<<number[i];
	return 0;
}

拓展:

1. <algorithm>定义的count函数和count_if函数,可以用于vector和数组。

链接:https://blog.youkuaiyun.com/lyj2014211626/article/details/69615514

2. PAT1023参考链接:https://blog.youkuaiyun.com/richenyunqi/article/details/79555465

#include<bits/stdc++.h>
using namespace std;
string input,result="";//输入的大整数、2倍后的大整数
int hashTable[10];//辅助数组
void mul(){//大整数乘法
    int num=0;
    for(int i=0;input[i]!='\0';++i){
        int k=2*(input[i]-'0')+num;
        result+=k%10+'0';
        num=k/10;
    }
    if(num!=0)
        result+=num+'0';//如果最终进位不为0,置于result数组末尾
}
int main(){
    cin>>input;
    reverse(input.begin(),input.end());//翻转读取到的数组
    mul();
    for(int i=0;input[i]!='\0';++i){//遍历新数原数两个数组,在相应的hashTable数组下标下进行递增递减
        ++hashTable[input[i]-'0'];
        --hashTable[result[i]-'0'];
    }
    bool yes=(count(hashTable,hashTable+10,0)==10);//遍历整个hashTable数组,看元素0的个数是否为10
    reverse(result.begin(),result.end());//将result数组翻转
    printf("%s\n%s",yes?"Yes":"No",result.c_str());
    return 0;
}

 

PAT 1023 Have Fun with Numbers python解法 1023 Have Fun with Numbers (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 happen... 阅读详情

相关推荐

内网开发必备:5分钟搞定WSL2离线安装(含Ubuntu迁移避坑指南)

本文详细介绍了在内网环境下快速完成WSL2离线安装的完整流程,包括系统兼容性检查、Linux内核更新包安装以及Ubuntu镜像的离线部署。特别针对外网Ubuntu环境迁移至内网时的用户权限、系统路径等常见问题提供了实用解决方案,帮助开发者高效搭建Linux开发环境。

weixin_29255731的博客 404

PAT 甲级 1023 Have Fun with Numbers (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

mxy1060957459的博客 799

利用setEcoMode优化innovus中的ecoAddRepeater批量操作效率

本文详细介绍了在Cadence Innovus工具中,如何利用setEcoMode命令的batchMode参数,显著提升ecoAddRepeater命令在批量插入buffer或inverter时的操作效率。通过合并重复的初始化和数据库更新开销,该模式可将大规模时序修复操作的执行速度提升数倍,并提供了完整的脚本示例与最佳实践,帮助后端设计工程师优化ECO流程。

mango的博客 514

【PAT刷题甲级】1023.Have Fun with Numbers

1023 Have Fun with Numbers (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 nu

C_greenbird的博客 471

Android13 CarPowerManagementService onApPowerStateChange流程分析

如下为CarPower上电的流程图:下面我们从代码角度分析:在CPMS的init方法中会调用onApPowerStateChange(CpmsState.WAIT_FOR_VHAL, CarPowerManager.STATE_WAIT_FOR_VHAL); 设置ApPowerState为STATE_WAIT_FOR_VHAL: 在onApPowerStateChange中调用PowerHandler 的handlePowerStateChange方法: 在handlePowerStateChang

liuning1985622的博客 926

一生一芯——用RTL实现最简单的处理器NPC(南大数逻计组RV32ICPU) 代码及思考参考

本文介绍自己前期用Verilog编写的基于RV32I指令集的CPU,个人认为是最简单最易懂的版本(即没做什么优化,即代码很水,没有考虑一些奇怪的情况,只适用简单指令序列,即题目所示最简单的处理器),适合新手练手Verilog以及了解简单CPU的运行过程。本文基于一生一芯,利用Verilator进行仿真,并用gtkwave查看波形,不了解verilator和gtkwave的可以看前几篇文章。

long0_o的博客 3310

「1023」Have Fun with Numbers

➳ENTRY 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-...

weixin_52435933的博客 185

5G时代必知:LDPC与Polar码实战解析(附MATLAB仿真代码)

本文深入解析5G时代核心信道编码技术LDPC码与Polar码的原理差异及性能表现,通过MATLAB仿真代码展示其在实际通信系统中的应用。LDPC码适用于大数据块高速传输,而Polar码在小数据块低时延场景表现优异,帮助开发者优化5G系统设计。

sugar的博客 643

1023 Have Fun with Numbers(20分)

给定一个不超过20位的正整数,将其2倍后得到的正整数进行判断,如果组成两者的元素相同(仅仅排列方式不同),则输出“Yes",否则输出”No"。由于不超过20位,而unsigned long long最多支持19位,因此可以分情况讨论。

a131529的博客 350

setoolkit克隆网站获取用户信息

一、环境 kali、DVWA 二、步骤 首先打开kali,输入setoolkit 依次选择选项1-选项2-选项3-选项2 输入钓鱼网站的网址(默认为本地网址) 输入要克隆的网址URL 这里我的网址如图,将网址复制 然后模拟攻击场景,攻击者向受害者发送了一条钓鱼链接,此链接与DVWA的登陆页面完全一样,受害者点击之后并进行登陆信息 点击login之后,用户信息就被setool...

BeatRex的博客 2998

Apache Kylin 命令注入漏洞复现(CVE-2020-1956)

Apache Kylin 中的静态 API 存在安全漏洞,可以将os命令与用户输入字符串连接起来,攻击者可以在Kylin没有任何保护或验证的情况下执行任何os命令。

0xSec 1558

[PAT-Advanced] A1023 Have Fun with Numbers (20)

文章目录A1023 Have Fun with Numbers (20point(s))Input Specification:Output Specification:Sample Input:Sample Output:CodeAnalysis Author: CHEN, Yue Organization: 浙江大学 Time Limit: 400 ms Memory Limit: 64 MB...

Duke 286

K8s-K8s部署Zookeeper+Kafka+ELFK+持久化数据

K8s环境下容器化部署`Zookeeper+Kafka+ELFK`日志收集系统

✅ 专注企业级 Linux/DevOps 运维的实战派 ✅ 已输出上百篇可落地方案 ✅ Shell 脚本开箱即用 ✅ K8s/YAML 环境验证 ✅ 故障排查链路完整复现 1916

PAT 甲级 1023 Have Fun with Numbers

1023 Have Fun with Numbers (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 nu

qq_42323299的博客 272

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

PAT甲级 1023 Have Fun with Numbers

PAT甲级 1023 Have Fun with Numbers 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...

旺崽的博客 2922

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

PAT (Advanced Level) 1023 Have Fun with Numbers (20 分) 【Python/C++】【大数乘法】

PAT (Advanced Level) 1023 Have Fun with Numbers (20 分) 按位做大数乘法即可 Python: import sys from math import * str_in = input() length = len(str_in) b = [int(n) for n in str_in.split()] n = b[0] a = [0]*100 ...

Irish_Moonshine的博客 323

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

PAT甲级真题-1023 Have Fun with Numbers详解优化

1023 Have Fun with Numbers 题目链接 https://pintia.cn/problem-sets/994805342720868352/problems/994805478658260992 解题思路 1.因为ungined long long 也无法记录20位数,因此只能用字符串来处理; 2.就是简单的对乘法的模拟,用一个变量记录每个数位乘以2产生的进位就可以了。嘿嘿 总结 想象平时算数学题的思路,模拟就好~~~。 代码展示 #include<bits/stdc++.h

高高冷的博客 336

1023 Have Fun with Numbers

1023Have Fun with Numbers(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 happe...

yu121380的博客 394

[PAT A1023]Have Fun with Numbers

[PAT A1023]Have Fun with Numbers 题目描述 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 ...

欢迎来到我的编程小屋 947

1023. Have Fun with Numbers (20)

题目链接:http://www.patest.cn/contests/pat-a-practise/1023 题目: 1023. Have Fun with Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard

Apie_CZX的专栏 4073
上一篇: 【PAT】Problem List and Solutions (Advanced Level)
下一篇: 1027 Colors in Mars
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值