TA的每日心情 | 擦汗 前天 09:07 |
---|
签到天数: 527 天 连续签到: 4 天 [LV.9]测试副司令
|
1测试积点
我想用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[4], number2[4];
- int n = 0;
- for (int i = 0; i < 4; i++)
- {
- cin>>number1[i];
- }
- if (number1[0] == number1[1]&& number1[1]== number1[2]&& number1[2]== number1[3])//4位数字相同时的输出
- {
- for (int i = 0; i < 4; i++)
- {
- cout << number1[i];
- }
- printf(" - ");
- for (int i = 0; i < 4; i++)
- {
- cout << number1[i];
- }
- printf(" = 0000");
- }
- else//其他情况
- {
- while (n != 6174)
- {
- for (int i = 0; i < 4; i++)
- {
- number2[i] = number1[i];
- }
- sort(number1, number1 + 4, cmp);//较大的数
- sort(number2, number2 + 4);//较小的数
- for (int i = 0; i < 4; i++)//输出相减的两个数
- {
- cout << number1[i];
- }
- printf(" - ");
- for (int i = 0; i < 4; i++)
- {
- cout << number2[i];
- }
- int n1 = 0, n2 = 0,t=1;
- for (int i = 3; i >=0; i--)//输出相减结果
- {
- n1 += ((int)number1[i]-48) * t;
- t *= 10;
- }
- t = 1;
- for (int i = 3; i >= 0; i--)
- {
- n2 += ((int)number2[i]-48) * t;
- t *= 10;
- }
- n = n1 - n2;
- printf(" = %04d\n", n);
- int temp = n;
- int i = 0;
- while(i<4)//记录相减的结果
- {
- number1[i++] = (char)(temp % 10+48);
- temp /= 10;
- }
- }
- }
- return 0;
- }
复制代码
|
|