2,3,4测试点无法通过
我想用char数组解决输出时三位数之前的“0”,于是用了很多的循环,我自己改了几次,但2,3,4测试点就是无法通过,很苦恼。请问一下代码还有什么地方欠考虑。#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool cmp(char a, char b)
{
return a > b;
}
int main()
{
char number1, number2;
int n = 0;
for (int i = 0; i < 4; i++)
{
cin>>number1;
}
if (number1 == number1&& number1== number1&& number1== number1)//4位数字相同时的输出
{
for (int i = 0; i < 4; i++)
{
cout << number1;
}
printf(" - ");
for (int i = 0; i < 4; i++)
{
cout << number1;
}
printf(" = 0000");
}
else//其他情况
{
while (n != 6174)
{
for (int i = 0; i < 4; i++)
{
number2 = number1;
}
sort(number1, number1 + 4, cmp);//较大的数
sort(number2, number2 + 4);//较小的数
for (int i = 0; i < 4; i++)//输出相减的两个数
{
cout << number1;
}
printf(" - ");
for (int i = 0; i < 4; i++)
{
cout << number2;
}
int n1 = 0, n2 = 0,t=1;
for (int i = 3; i >=0; i--)//输出相减结果
{
n1 += ((int)number1-48) * t;
t *= 10;
}
t = 1;
for (int i = 3; i >= 0; i--)
{
n2 += ((int)number2-48) * t;
t *= 10;
}
n = n1 - n2;
printf(" = %04d\n", n);
int temp = n;
int i = 0;
while(i<4)//记录相减的结果
{
number1 = (char)(temp % 10+48);
temp /= 10;
}
}
}
return 0;
}
https://ask.csdn.net/questions/1055005参考下这个 参考1楼的链接~~ 参考下这个链接https://blog.csdn.net/qq_40930559/article/details/104094592 测试点在哪里?? ;P,没头啊
页:
[1]