其他分享
首页 > 其他分享> > PAT乙级1019

PAT乙级1019

作者:互联网

在这里插入图片描述
特殊判断点2、1000等。
注意设置字符串0的补全就问题不大,这里利用了fill这个函数。

#include <iostream>
#include <stdlib.h>
#include <string>
#include <string.h>
using namespace std;

int jiangxp(string s);
int shengxp(string s);
string fill(string s);
int m, n,p,q;
int i = 0;
int j = 0;
char tmp;
int sum = 0;
bool panduan = false;
int main()
{
	//i是非递增,j是非递减
	string s;
	cin >> s;
	s = fill(s);
	for (i = 0; i < s.length(); i++)
	{
		for (j = i + 1;  j < s.length();j++)
		{
			if (s[i] != s[j])
				panduan = true;
		}
	}

	if (panduan == false)
	{
		p = atoi(s.c_str());
		printf("%04d - %04d = %04d",p,p,p-p);
	}
	else
	{
		while (true)
		{
			s = fill(s);
			i = jiangxp(s);
			j = shengxp(s);
			sum = i - j;
			printf("%04d - %04d = %04d\n",i,j,sum);
			s = to_string(sum);
			if (sum == 6174)
				break;
		}
	}
	return 0;
}


int jiangxp(string s)
{
	for (m = 0; m < s.length(); m++)
	{
		for (n = m + 1; n < s.length(); n++)
		{
			if (s[n] > s[m])
			{
				tmp = s[n];
				s[n] = s[m];
				s[m] = tmp;
			}
		}
	}
	i = atoi(s.c_str());
	return i;
}


int shengxp(string s)
{
	for (m = 0; m < s.length(); m++)
	{
		for (n = m + 1; n < s.length(); n++)
		{
			if (s[n] < s[m])
			{
				tmp = s[n];
				s[n] = s[m];
				s[m] = tmp;
			}
		}
	}
	j = atoi(s.c_str());
	return j;
}

string fill(string s)
{
	while (s.length() < 4)
	{
		s = s + '0';
	}
	return s;

}

标签:tmp,PAT,string,int,04d,乙级,++,length,1019
来源: https://blog.csdn.net/taitoushuxingx/article/details/116353034