自测-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 ...
随机推荐
- ThreadLoacl的反思
在我的随笔 spring mvc:注解@ModelAttribue妙用 中使用ThreadLocal来简化spring mvc控制层controller中的ModelMap,Response.Jso ...
- Oracle之range,hash,list分区现实应用及优缺点汇总
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp51 [align=center;] Oracle之range,hash,l ...
- springboot 入门三- 读取配置信息二(读取属性文件方式)
在上篇文章中简单介绍自带读取方式.springboot提供多种方式来读取 一.@ConfigurationProperties(value="my") 支持更灵活的绑定及元数据的支 ...
- 第4阶段——制作根文件系统之分析init_post()如何启动第1个程序(1)
本章学习如何启动第一个应用程序 1.在前面的分析中我们了解到,在init进程中内核挂接到根文件系统之后,会开始启动第一个应用程序: kernel_init函数代码如下: static int __in ...
- HTTP请求响应机制与响应状态码
转载来源:http://blog.csdn.net/xyw591238/article/details/51907143 HTTP协议 Internate的基本协议是TCP/IP(传输控制协议和网际协 ...
- 转: 【Java并发编程】之十八:第五篇中volatile意外问题的正确分析解答(含代码)
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17382679 在<Java并发编程学习笔记之五:volatile变量修饰符-意料之外 ...
- 团队作业4——第一次项目冲刺(Alpha版本)2st day
一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 界面 1.四个用户登录界面已经完成. 2.界面内的功能完成了一小部分. 登陆部分 1.QQ授权已经申请,还未通过. 2.通过好 ...
- 团队作业4——第一次项目冲刺(Alpha版本) Day3
1.由于大家课程都比较多,时间紧迫,今天最后一节课下课完在教室召开了简短的站立式会议,会议照片如下: 2.Leangoo任务分解图: 3.每个人的工作: 队员 今天已完成的工作 明天计划完成的工作 林 ...
- 201521123096《Java程序设计》第六周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 2. 书面作业 (1)clone方法 1.1 Object ...
- 201521123004《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 接口 接口(interface)就是方法声明和常量值的集合 实现接口的类叫接口的 ...