首页 > 其他 > 详细

NYOJ_51 6174问题

时间:2015-05-29 15:53:16      阅读:238      评论:0      收藏:0      [点我收藏+]

题目地址

分析:

以后排序直接用库函数提供的sort很方便。
核心问题在于需要把一个数拆开排序。
其实在判断的时候直接判断6174即可,不需要判断是否和前一个数字相等。

心得:

水题需要锻炼自己的速度。

代码实现:

#include<iostream>
#include<algorithm>
using namespace std;

void ReverseMinus(int &a0)
{
	int temp1,temp2;
	int *t = new int [4];
	t[0] = a0/1000;
	t[1] = a0/100%10;
	t[2] = a0/10%10;
	t[3] = a0%10;
	//BubbleSort(t,4);
	sort(t,t+4);
	temp1 = t[0]+t[1]*10+t[2]*100+t[3]*1000;
	temp2 = t[3]+t[2]*10+t[1]*100+t[0]*1000;
	a0 = temp1 -temp2;
}
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		int a;
		cin>>a;
		int i=0;
		while(++i)
		{
			int a0= a;
			ReverseMinus(a);
			if(a0 == a)    //和前一个数不相等 
			{
				cout<<i<<endl;
				break;
			}
		}
	}
	return 0;
}

/*
void BubbleSort(int *a, int n) //冒泡排序 
{
	int i,j,last;
	i = n-1; //n-1趟
	while(i>0)
	{
		last = 0;
		for( j=0;j<i;j++)
			if(a[j+1]<a[j])
			{
				int temp;
				temp = a[j+1];
				a[j+1] = a[j];
				a[j] = temp;
				last =j;
			}
			i = last;	
	} 
}*/


NYOJ_51 6174问题

原文:http://blog.csdn.net/think_ycx/article/details/46234245

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!