POJ 1350 Cabric Number Problem (模拟)
Description
If we input a number formed by 4 digits and these digits are not all of one same value, then it obeys the following law. Let us operate the number in the following way:
(1) Arrange the digits in the way from bigger to smaller, such that it forms the biggest number that could be made from these 4 digits;
(2) Arrange the digits in the way from smaller to bigger, such that it forms the smallest number that could be made from these 4 digits (If there is 0 among these 4 digits, the number obtained may be less than four digits);
(3) Find the difference of these two numbers that is a new four digital number.
Repeat the above process, we can finally always get the result 6174 or 0.
Please write the program to realize the above algorithm.
Input
Each case is a line of an integer.-1 denotes the end of input.
Output
If the integer is formed exactly by 4 digits and these digits are not all of one same value, then output from the program should show the procedure for finding this number and the number of repetition times. Otherwise output "No!!".
Sample Input
5364
2221
4444
-1
Sample Output
N=5364:
6543-3456=3087
8730-378=8352
8532-2358=6174
Ok!! 3 times
N=2221:
2221-1222=999
999-999=0
Ok!! 2 times
N=4444:
No!!
分析:
题目会给定一个四位数字,将每个数位上的数字重新排列组合后,能够组成一个最大的四位数和最小的四位数。将求差之后的结果接着进行计算,直到答案为6174或则为0.
注意:最开始输进去的一个是四位数,切四个位数不能够全部相等。
中间计算后的结果可能不是四位数字,所以如果按照四位数字计算的话,得去掉所有的前导0.
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int cant(int s)//判断四个位置上的数字是否完全相同
{
int a=s/1000;
int b=s/100%10;
int c=s/10%10;
int d=s%10;
if(a==b&&b==c&&c==d) return 1;
return 0;
}
int cal(int s)//s传进来的是每次计算后得到的结果
{
int a[4];
a[0]=s/1000;
a[1]=s/100%10;
a[2]=s/10%10;
a[3]=s%10;
int fl=0;//表示的是这个计算结果中前导0的个数
if(a[0]==0)
{
fl=1;
if(a[1]==0)
{
fl=2;
if(a[2]=0)fl=3;
}
}
sort(a,a+4);//从小到大排序
int max_num,min_num;
min_num=a[0]*1000+a[1]*100+a[2]*10+a[3];
// 处理高位为0的情况
if(fl==0)
{
max_num=a[3]*1000+a[2]*100+a[1]*10+a[0];
}
else if(fl==1)
{
max_num=a[3]*100+a[2]*10+a[1];
}
else if(fl==2)
{
max_num=a[3]*10+a[2];
}
else
{
max_num=a[3];
}
printf("%d-%d=%d\n",max_num,min_num,max_num-min_num);
return max_num-min_num;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n!=-1)
{
printf("N=%d:\n",n);
if(n>=9999||n<=1000||cant(n)) //注意,输入一定要是4位数,并且四个位置上的数字不能全部相同
{
printf("No!!\n");
continue;
}
int ans=0;
while(n!=0&&n!=6174)
{
ans++;
n=cal(n);
}
printf("Ok!! %d times\n",ans);
}
return 0;
}
POJ 1350 Cabric Number Problem (模拟)的更多相关文章
- poj 2104 K-th Number(主席树,详细有用)
poj 2104 K-th Number(主席树) 主席树就是持久化的线段树,添加的时候,每更新了一个节点的线段树都被保存下来了. 查询区间[L,R]操作的时候,只需要用第R棵树减去第L-1棵树就是区 ...
- POJ 1152 An Easy Problem! (取模运算性质)
题目链接:POJ 1152 An Easy Problem! 题意:求一个N进制的数R.保证R能被(N-1)整除时最小的N. 第一反应是暴力.N的大小0到62.发现当中将N进制话成10进制时,数据会溢 ...
- Buge's Fibonacci Number Problem
Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)
POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- poj 2104 K-th Number 主席树+超级详细解释
poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...
- POJ 2826 An Easy Problem? 判断线段相交
POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <io ...
随机推荐
- c++11 左值引用、右值引用
c++11 左值引用.右值引用 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #i ...
- linux 批量更改文件名 rename 命令
rename 的典型应用: # rename $1 $2 $3# $1: 要被取代的關鍵字# $2: 新的關鍵字# $3: 檔名符合這個規則的才取代 # 把 IMG001.jpg, IMG002.jp ...
- BZOJ 2878 【NOI2012】 迷失游乐园
题目链接:迷失游乐园 这道题也没有传说中的那么难写吗→_→ 似乎有篇博客讲得特详细……附上链接:戳这里 如果这道题不是基环树,而就是一棵树的话,我们来考虑改怎么做.因为树上的路径只有向上.向下两种走法 ...
- ThinkPHP 5.x远程命令执行漏洞分析与复现
0x00 前言 ThinkPHP官方2018年12月9日发布重要的安全更新,修复了一个严重的远程代码执行漏洞.该更新主要涉及一个安全更新,由于框架对控制器名没有进行足够的检测会导致在没有开启强制路由的 ...
- FastDFS分布式存储
分布式存储分类 通用分布式存储:mogilefs, fastdfs, ...(无文件系统接口, 通过API访问) 专用分布式存储:即分布式文件系统, moosefs, ...(有文件系统接口) GFS ...
- webservice 菜鸟探索之旅
项目背景 接到的任务是通过调用其他平台厂商提供的webservice接口来获取他们的图片数据把图片下载下来录入我司平台.之前没有接触过webservice,所以开始了这次webservice的菜鸟之旅 ...
- android studio 怎么做屏幕适配?
一.关于布局适配建议1.不要使用绝对布局2.尽量使用match_parent 而不是fill_parent .3.能够使用权重的地方尽量使用权重(android:layout_weight)4.如果是 ...
- 解题:HEOI 2015 最短不公共子串
题面 制杖四合一,HEOI以前居然出这种**题,看来HE还是联考比较好= = 首先对第二个串建SAM 第一个简单,以每个位置为起点在SAM上走,失配时更新答案 第二个先在第二个串上预处理$firs[i ...
- Java中JDK,JRE和JVM之间的关系
初学JAVA很容易被其中的很多概念弄的傻傻分不清楚,首先从概念上理解一下吧,JDK(Java Development Kit)简单理解就是Java开发工具包,JRE(Java Runtime Envi ...
- 【trie树】【P4551】 最长异或路径
Description 给定 \(n\) 个点的带边权树,求一条异或和最大的简单路径 Input 第一行是点数 \(n\) 下面 \(n - 1\) 行每行三个整数描述这棵树 Output 输出一个数 ...