Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一般般,给后人参考吧

Posted by 2014ccutliuzehao at 2015-03-11 09:57:20 on Problem 1350
#include<iostream>
#include <algorithm>
using namespace std;
int fun(int a)
{

	if (a < 1000){
		int t[3];
		int w = 0;
		for (int r = 0; r < 3; r++)
		{
			t[r] = a % 10;
			a = a / 10;
		}
		sort(t, t + 3);
		for (int u = 2; u >= 0; u--){
			w = w * 10 + t[u];
		}
		return w;
	}
	else
	{
		int t[4];
		int w = 0;
		for (int r = 0; r < 4; r++)
		{
			t[r] = a % 10;
			a = a / 10;
		}
		sort(t, t + 4);
		for (int u = 3; u >= 0; u--){
			w = w * 10 + t[u];
		}
		return w;
	}
}
int sun(int a)
{
	int t[4], w = 0;
	for (int r = 0; r < 4; r++)
	{
		t[r] = a % 10;
		a = a / 10;
	}
	sort(t, t + 4);
	for (int u = 0; u < 4; u++){
		w = w * 10 + t[u];
	}
	return w;
}
int main()
{
	int a;
	while (cin >> a&&a != -1)
	{
		int b, s = 0;
		cout << "N=" << a << ":" << endl;
		if (a == 1111 || a == 2222 || a == 3333 || a == 4444 || a == 5555 || a == 6666 || a == 7777 || a == 8888 || a == 9999 || a<1000 || a>9999){
			cout << "No!!" << endl;
			continue;
		}
		while (1)
		{
			s++;
			b = fun(a) - sun(a);
			cout << fun(a) << "-" << sun(a) << "=" << b << endl;
			a = b;
			if (b == 0 || b == 6174){
				cout << "Ok!!" << " " << s << " " << "times" << endl;
				break;
			}
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator