7-49 Have Fun with Numbers(20 分)

PAT C-语言入门题目-7-49 Have Fun with Numbers (20 分)(char数组进行数字运算) 7-49 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 happe... 阅读详情

7-49 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 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 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

#include <stdio.h>
#define MAX_BITS 21

int main(void) {
    int bits = 0, i, num[MAX_BITS], doubled_num[MAX_BITS], carry = 0, double_bits;
    int times[10] = { 0,0,0,0,0,0,0,0,0,0 };  //一个数中每个数字出现的次数(下标表示数字,下标对应的数据为该数字出现的次数)
    int after_times[10] = { 0,0,0,0,0,0,0,0,0,0 };  //该数乘2后每个数字出现的次数
    char temp;

    temp = getchar();
    while (temp >= '0'&&temp <= '9') {  //输入原始数
        num[bits] = temp - '0';
        bits++;
        temp = getchar();
    }
    double_bits = bits;
    for (i = bits - 1; i >= 0; i--) {  //模拟乘法
        doubled_num[i] = num[i] * 2 % 10;
        if (carry == 1) {  //需要进位
            doubled_num[i]++;
            carry = 0;
        }
        if (num[i] * 2 >= 10) {
            carry = 1;
        }
        if (i == 0 && num[i] * 2 >= 10) {  //最高位需要进位
            for (int j = bits; j >= 1; j--) {  //所有数字往后挪一位(腾出最高位)
                doubled_num[j] = doubled_num[j - 1];
            }
            doubled_num[0] = 1;  //最高位为1
            double_bits++;  //乘2后的数位数加1
        }
    }
    for (i = 0; i < bits; i++) {  //统计原始数每个数字出现的次数
        times[num[i]]++;
    }
    for (i = 0; i < double_bits; i++) {  //统计乘2后每个数字出现的次数
        after_times[doubled_num[i]]++;
    }
    for (i = 0; i < 10; i++) {  //判断每个数字出现的次数是否一样
        if (times[i] != after_times[i]) {
            break;
        }
    }
    if (i < 10 || bits != double_bits) {  //每个数字出现次数不完全一样 或 乘2后位数与原始数不一样
        printf("No\n");
        int maxbits;

        if (bits != double_bits) {  //判断乘2后的位数
            maxbits = double_bits;
        }
        else {
            maxbits = bits;
        }
        for (i = 0; i < maxbits; i++) {
            printf("%d", doubled_num[i]);
        }
    }
    else {
        printf("Yes\n");
        for (i = 0; i < bits; i++) {
            printf("%d", doubled_num[i]);
        }
    }
    printf("\n");

    return 0;
}
PAT 1023. Have Fun with Numbers (20) 【题目开始没理解好,后来折腾半天才搞懂】 1023. Have Fun with Numbers (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Notice that the number 123456789 is 阅读详情

相关推荐

脑影像分析入门:如何用一般线性模型(GLM)处理fMRI数据?

本文为神经影像研究入门者详细解析了如何运用一般线性模型(GLM)处理fMRI数据。文章从GLM的核心原理出发,逐步拆解了从实验设计、构建稳健设计矩阵、模型估计与统计推断,到高阶分析及多重比较校正的全流程,并指出了常见陷阱与结果解读要点,是掌握fMRI数据分析基础的关键指南。

weixin_29091445的博客 399

PAT 1023. Have Fun with Numbers (20)

题目说明:输入一个数,如果这个数翻倍之后和原数每一位的数字一样,只是排序不同,则输出Yes,否则输出No。 思路:原数最多20位,不能直接乘以2,需要按位去计算。用两个数组分别存放原数和2倍数每个数字的出现次数,最后对比得到结果。 代码: #include #include #include using namespace std; char ori[25]; char res[25];

彼岸大鱼的专栏 445

二、基于C语言的JPEG解码代码详解

// JPEG解码 // 输入JPEG后输出Bitmap #include <stdio.h> #include <stdlib.h> #pragma warning(disable : 4996) unsigned int BuffIndex; // JPEG数据的位置 unsigned int BuffSize; // JPEG数据的大小 unsigned int BuffX; // 图像的横向尺寸 unsigned int BuffY; //

m0_37633745的博客 4452

7-49 Have Fun with Numbers (20分)

7-49 Have Fun with Numbers (20分) 翻译玩数字游戏 请注意,数字123456789是9位数字,完全由1到9组成,没有重复。将其加倍,我们将获得246913578,它恰好是另一个9位数字,恰好由1到9组成,只是排列不同。如果再次将其加倍,请检查结果! 现在,您应该检查此属性是否还有更多的数字。也就是说,将给定数字与k位数字加倍,就可以知道结果数是否仅由原始数字中的数字排列组成。 输入规范: 每个输入文件包含一个测试用例。每个案例包含一个不超过20位的正整数。 输出规格: .

LEO的博客 402

别再手动拆解字节了!用博图这个功能指令,5分钟搞定ARRAY转BOOL数组

本文详细介绍了西门子博图中`ARRAY_TO_BOOL`功能指令的高效应用,帮助工程师快速实现ARRAY到BOOL数组的转换。通过实战案例和参数配置指南,展示了如何5分钟内完成传统方法需半小时的任务,大幅提升PLC编程效率,特别适用于通信报文解析和设备状态监控。

weixin_42653691的博客 260

基于Lora的一主多从监测与控制系统

题目:基于Lora的远程农业监测与控制系统 目的:通过Lora子节点采集好各种传感器的信息,发送给汇聚节点,汇聚节点将数据通过ESP8266上传至云端,使用了微信小程序进行监测与控制

热爱生活、奋斗前行 7841

7-8 Have Fun with Numbers (20 分)

7-8 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...

丁世杰的博客 1232

初识colmap

COLMAP是一款开源的计算机视觉软件,主要用于从一系列二维图像中进行三维重建。它全称为“COLLISION-MAPping”,是一个高度集成的Structure-from-Motion (SfM) 和 Multi-View Stereo (MVS) 管道工具,由斯坦福大学开发和维护。COLMAP的主要功能包括特征检测与匹配、增量式SfM、多视图立体匹配以及优化。它能够从无序或有序的二维照片集合中恢复出三维场景的几何结构(点云)以及每张图像对应的相机姿态。

Hhsjehejj的博客 5549

7-49 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 a

sayinginging的博客 202

【AIGC】COT思维链:让AI学会拆解问题,像人一样思考

COT思维链的出现,为AI的发展开辟了一条新的道路。它让AI学会了像人类一样思考,将复杂问题分解成简单的步骤,并逐步推理出最终答案。希望我的引导会对你产生启发。

智兔唯新的博客 9002

Spring AI 实战:5 分钟搭建你的第一个 AI 应用(含完整代码)

能力关键 API适用场景基础对话简单问答、文本生成流式输出实时打字机效果@Bean模型需要调用外部工具结构化输出需要返回 Java 对象多轮对话聊天机器人场景Spring AI 让 Java 开发者可以用最熟悉的方式(Spring Boot + Maven)进入 AI 应用开发领域,不用学新语言,不用换框架,这是它最大的优势。但目前为止,我们的 AI 应用只能回答"它脑子里已有的知识"。如果让它读懂你自己的技术文档、公司内部知识库呢?这就是下一篇文章的主题——RAG(检索增强生成)实战。

qq_17204557的博客 642

7-49 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 con...

weixin_43526304的博客 638

计算机组成原理(第三版)唐朔飞-第六章计算机的运算方法-课后习题(1-16)

计算机组成原理(第三版)唐朔飞-第六章计算机的运算方法-课后习题(1-16)

weixin_45735391的博客 8万+

7-49 Have Fun with Numbers (20分) C语言 带注释

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 con...

十年 551

假人有限元模型

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

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

中M2018春C入门和进阶练习集-编程题49 7-49 Have Fun with Numbers(20 分)

7-49 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...

sinoshen的博客 408

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

1023 Have Fun with Numbers (20分)

文章目录1 题目2 解析2.1 题意2.2 思路3 参考代码 1 题目 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...

韩江雪de 小屋 517

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

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 to be another 9-digit number con

sheng_xc的专栏 765

4 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 to be another 9-digit number cons

yz764127031的博客 488

PAT Have fun with numbers (Python)

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 con

乱乱写 581

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

392

PAT_自测4_Have Fun with Numbers

题目: https://pta.patest.cn/pta/test/17/exam/4/question/263 自测-4 Have Fun with Numbers   (20分) Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wi

2322
上一篇: 7-48 求组合数(15 分)
下一篇: 7-50 近似求PI(15 分)
centralunit
博客等级 码龄10年 219粉丝 115原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值