卡布列克常数(string类运用)

卡布列克常数

时间限制: 1 Sec  内存限制: 128 MB

题目描述

最近,小Q在数学兴趣课中了解了“卡布列克常数”。卡布列克是一位数学家,他在研究数字时发现:任意一个不是用完全相同数字组成的四位数,如果对它们的每位数字重新排序,组成一个最大的数和一个最小的数,然后用最大数减去最小数,差不够四位数时补零,类推下去,最后将变成一个固定的数:6174,这就是卡布列克常数。
例如:4321-1234=3087
  8730-378=8352
  8532-2358=6174
  7641-1467=6174
               ……
小Q想,我能不能编程来验证呢?输入一个符合条件的四位数,然后验证运算过程。

 

 

输入

共1行,为任意一个不是用完全相同数字组成的四位数。

 

输出

变为卡布列克常数的运算过程,由若干行组成,每行是一个算式,不含空格。

 

样例输入

复制样例数据

4321

样例输出

4321-1234=3087
8730-378=8352
8532-2358=6174
/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <sstream>
#include <queue>

typedef long long LL;
using namespace std;

string s;

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	cin >> s;
	int maxx, minn;
	while(s != "6174"){
		stringstream s1, s2, s3;
		sort(s.begin(), s.end());
		s1 << s, s1 >> minn;
		reverse(s.begin(), s.end());
		//cout << s << endl;
		s2 << s, s2 >> maxx;
		int num = maxx - minn;
		printf("%d-%d=%d\n", maxx, minn, num);
		s3 << num, s3 >> s;
		int len = s.size();
		for(int i = len; i < 4; i++) s += '0';
	}

	return 0;
}
/**/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值