自测-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 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 kk 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
说明:最开始使用的思路是大数计算的方式,存入字符串数组,然后运算后与原数组进行对比,但存在wa点,现在没找到。先放置于此:
#include <stdio.h>
#include<string.h>
int main()
{
// N为长度,M为进位
int N = ,M = , i = , num = ,loop = , j = ;
char str[];
char tmp[];
scanf("%s",str);
N = strlen(str); for(i = N - ; i >= ; i--){
num = (str[i] - '')* + M;
M = ;
if(num > ){
num = num % ;
M++;
}
tmp[i] = num + '';
}
for(i = ; i < N; i++){
for(j = ; j < N; j++){
if(tmp[i] == str[j]){
str[j] = 'a';
break;
}
}
}
for(i = ; i < N; i++){
if(str[i] != 'a'){
loop = ;
break;
}
}
if(M == ){
printf("No\n1");
for(i = ; i < N; i++){
printf("%d",tmp[i] - '');
}
return ;
}
if(loop == ){
for(i = ; i < N; i++){
printf("%d",tmp[i] - '');
}
}else{
printf("Yes\n");
for(i = ; i < N; i++){
printf("%d",tmp[i] - '');
}
}
return ;
}
后改变思路,只记录0~9十个数组出现的次数,仅需要两个长度为10的一维数组即可。使用别人写的C++代码,引用链接在最后
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
int cnt1[] = { }, cnt2[] = {}; int main(){
string s;
cin >> s;
string s2 = s;
for (int i = ; i < s.size(); i++){
cnt1[s[i] - '']++;
}
int carry = ;
for (int i = s.size() - ; i >= ; i--){
s2[i] = ((s[i] - '') * + carry )% + '';
carry = ((s[i] - '') * + carry) / ;
}
if (carry){
char c = carry + '';
s2 = c + s2;
cout << "No\n" << s2 << endl;
return ;
}
for (int i = ; i < s2.size(); i++){
cnt2[s2[i] - '']++;
}
bool flag = true;
for (int i = ; i < ; i++){
if (cnt1[i] != cnt2[i]){
flag = false;
break;
}
}
if (flag) cout << "Yes" << "\n" << s2 << endl;
else cout << "No\n" << s2;
return ;
}
http://blog.csdn.net/xtzmm1215/article/details/38837203
自测-4 Have Fun with Numbers的更多相关文章
- pat00-自测4. Have Fun with Numbers (20)
00-自测4. Have Fun with Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yu ...
- 数据结构练习 00-自测4. Have Fun with Numbers (20)
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- 00-自测4. Have Fun with Numbers
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- PTA 自测-4 Have Fun with Numbers
#include<iostream> #include<string> #include<cstring> #include<vector> using ...
- PAT自测_打印沙漏、素数对猜想、数组元素循环右移、数字加倍重排、机器洗牌
-自测1. 打印沙漏() 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号 ...
- poj 3641 Pseudoprime numbers Miller_Rabin测素裸题
题目链接 题意:题目定义了Carmichael Numbers 即 a^p % p = a.并且p不是素数.之后输入p,a问p是否为Carmichael Numbers? 坑点:先是各种RE,因为po ...
- jmeter压测数据库,抓包工具,python基础
jmeter压力测试 前提场景的设置:单场景(单个接口进行压力测试一个请求)或混合场景(有业务流程的场景进行压力测试多个请求),压测时间一般在5--1515分组具体看需求. 数据准备:数据量少和数据量 ...
- 【MySQL】sysbench压测服务器及结果解读
主要压测范围包括CPU测试.磁盘IO测试.线程测试.OLTP测试等,那么sysbench就可以满足我们的压测需求.下面我们简单来看下sysbench的安装使用以及压测结果的解读. 一.sysbench ...
- pat00-自测5. Shuffling Machine (20)
00-自测5. Shuffling Machine (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Sh ...
随机推荐
- 有哪些关于 Python 的技术博客?
Python是一种动态解释型的编程语言,它可以在Windows.UNIX.MAC等多种操作系统以及Java..NET开发平台上使用.不过包含的内容很多,加上各种标准库.拓展库,乱花渐欲迷人眼.因此如何 ...
- 帆软的报表系统与泛微OA结合起来,这两个软件麦枫提供了经典的服务。
一.集成配景泛微OA对企业的代价 泛微协同办公计划能向你供给一个协同的.集成的办公情况,使所有的办公职员都在统一且个性化的信息流派中一路事情, 解脱光阴和地区的限定,实现协同事情与知识治理. Eoco ...
- element ui datePicker 设置当前日期之前的日期不可选
pickerOptions0: { disabledDate(time) { return time.getTime() < Date.now() - 8.64e7 } },
- 如何在sublime+chrome中调试php代码?
1.搭建php本地运行环境具体点击如何使用phpstudy本地搭建多站点(每个站点对应不同的端口) 2.下载php_xdebug.dll, [5.3版以上的php下载地址]http://pecl.ph ...
- 【集美大学1411_助教博客】alpha阶段后 成绩
团队作业7 成绩(助教对团队排名) 团队名称 设想和目标 计划 资源 变更管理 设计/实现 测试/发布 团队的角色,管理,合作 总结 全组讨论的照片 团队成员在Alpha阶段的角色和具体贡献 合计 排 ...
- JAVA基础第四组(5道题)
16.[程序16] 题目:输出9*9口诀. 1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列. package com. ...
- 201521145048《Java程序设计》第5周学习总结
1. 本章学习总结 2. 书面作业 Q1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过?哪句会出现错误?试改正该错误.并分析输出结果. 错误 ...
- 201521123002《Java程序设计》第10周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...
- Java:Object类的equals()方法 如何编写一个完美的equals()方法
一 代码实例: package freewill.objectequals; /** * @author freewill * @see Core Java page161 * @desc get ...
- Hyperledger Fabric 1.0 从零开始(二)——环境构建(公网)
1:环境构建 在本文中用到的宿主机环境是Centos ,版本为Centos.x86_647.2,通过Docker 容器来运行Fabric的节点,版本为v1.0.因此,启动Fabric网络中的节点需要先 ...