写出下列程序运行结果?

#include <iostream>using namespace std;long CalculateFactor(int n){ long f; if (n<0) cout <<”n<0”<<endl; else if (n==0) f=1; else f= CalculateFactor(n-1)*n; return f;}int main(){ int n; long y; cout <<"Input a positive number: "; cin >>n; y= CalculateFactor(n); cout <<n<<"!="<<y<<endl;}

这个程序的源程序和运行结果如图,但是这个程序有问题,

在n小于0时,返回的f没有赋值,是一个随机值,所以结果有错.

改正的程序见第2个源代码图和运行结果图.

改正后的程序和运行结果

温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-12-09
若出现除0的情况会报“浮点错误”
最大公约数
更相减损法
辗转相除法
最大公约数 * 最小公倍数 = 两数乘积
目录

A1081 Rational Sum
B1034 / A1088 有理数四则运算
A1081 Rational Sum
Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum.

Input Specification:
Each input file contains one test case. Each case starts with a positive integer N (≤100), followed in the next line N rational numbers a1/b1 a2/b2 ... where all the numerators and denominators are in the range of long int. If there is a negative number, then the sign must appear in front of the numerator.

Output Specification:
For each test case, output the sum in the simplest form integer numerator/denominator where integer is the integer part of the sum, numerator < denominator, and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.

Sample Input 1:
5
2/5 4/15 1/30 -2/60 8/3
1
2
1
2
Sample Output 1:
3 1/3
1
1
Sample Input 2:
2
4/3 2/3
1
2
1
2
Sample Output 2:
2
1
1
Sample Input 3:
3
1/3 -1/6 1/8
1
2
1
2
Sample Output 3:
7/24
1
1
我的

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
int n = 0;
scanf("%d", &n);

string in = "";
long long a = 0, b = 1;
for (int i = 0;i < n;++i) {
cin >> in;
int pos = in.find("/");
long long a1 = atoi(in.substr(0, pos).c_str());
long long b1 = atoi(in.substr(pos + 1, in.size() - pos - 1).c_str());

a = a * b1 + a1 * b;
b *= b1;
}

if (!a) {
printf("0");
return 0;
}

long long absa = abs(a), absb = b;
if (a != 1) {
while (absa != absb) {
if (absa > absb) absa -= absb;
else absb -= absa;
}
a /= absa;
b /= absb;
}

long long integer = a / b, numerator = abs(a % b);
if (integer) printf("%lld", a / b);
if (numerator) {
if (integer) printf(" ");
printf("%lld/%lld", numerator, b);
}

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
测试点5:若出现除0的情况会报“浮点错误”

用int会溢出,须使用long long

《算法笔记》P211

“必须在每一步加法后都进行约分,如果等全部加完后才约分,则会溢出。”

我是全部加完后才约分的,没有出现溢出……

B1034 / A1088 有理数四则运算
本题要求编写程序,计算 2 个有理数的和、差、积、商。

输入格式:
输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为 0。

输出格式:
分别在 4 行中按照 有理数1 运算符 有理数2 = 结果 的格式顺序输出 2 个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式 k a/b,其中 k 是整数部分,a/b 是最简分数部分;若为负数,则须加括号;若除法分母为 0,则输出 Inf。题目保证正确的输出中没有超过整型范围的整数。

输入样例 1:
2/3 -4/2
1
1
输出样例 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
1
2
3
4
1
2
3
4
输入样例 2:
5/3 0/6
1
1
输出样例 2:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
1
2
3
4
1
2
3
4
我的

#include <iostream>
#include <string>

using namespace std;

void gcd(long long&a, long long&b)
{
long long absa = abs(a), absb = abs(b);
if (a && a != 1) {
while (absa && absb) {
if (absa > absb) absa %= absb;
else absb %= absa;
}
absa ? absa : absa = absb;
a /= absa;
b /= absa;
}
}

void Output(long long&a, long long&b)
{
if (!a) printf("0");
else {
gcd(a, b);
long long integer = a / b, numerator = abs(a % b);
if (integer) {
if (integer < 0) printf("(");
printf("%lld", integer);
}
if (numerator) {
if (integer) printf(" ");
else {
if (a < 0) numerator = -numerator;
if (numerator < 0) printf("(");
}
printf("%lld/%lld", numerator, b);
}
if (integer < 0) printf(")");
else if (numerator < 0) printf(")");
}
printf("\n");
}

void Input(string&in1, long long&a1, long long&b1)
{
cin >> in1;
int pos = in1.find("/");
a1 = atoi(in1.substr(0, pos).c_str());
b1 = atoi(in1.substr(pos + 1, in1.size() - pos - 1).c_str());

gcd(a1, b1);
in1 = "";
long long integer = a1 / b1, numerator = abs(a1 % b1);
if (integer) in1 = to_string(integer);
if (numerator) {
if (integer) in1 += " ";
else if (a1 < 0) numerator = -numerator;
in1 += to_string(numerator) + "/" + to_string(b1);
}

if (!a1 || b1 == 1) in1 = to_string(a1);
if (a1 < 0) in1 = "(" + in1 + ")";
}

int main(void)
{
long long a1 = 0, b1 = 0, a2 = 0, b2 = 0;
string in1 = "", in2 = "";
Input(in1, a1, b1);
Input(in2, a2, b2);

// 加
cout << in1 << " + " << in2 << " = ";
long long a = 0, b = 1;
a = a1 * b2 + a2 * b1;
b = b1 * b2;
Output(a, b);

// 减
cout << in1 << " - " << in2 << " = ";
a = a1 * b2 - a2 * b1;
b = b1 * b2;
Output(a, b);

// 乘
cout << in1 << " * " << in2 << " = ";
a = a1 * a2;
b = b1 * b2;
Output(a, b);

// 除
cout << in1 << " / " << in2 << " = ";
if (!a2) printf("Inf");
else {
a = a1 * b2;
b = b1 * a2;
if (b < 0) {
a = -a;
b = -b;
}
Output(a, b);
}

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
注意除法中b = b1 * a2可能导致b为负数,而不是像前三个一样b = b1 * b2总为正
如果有测试点过不去,可以用牛客网找bug:https://www.nowcoder.com/pat/6/problem/4060
若用更相减损法求gcd可能导致测试点3超时,可以改用辗转相除法或其他方式
《算法笔记》P213
第2个回答  2021-12-09
是 (a+b)/2+c/d 计算出结果了 再转换成FLOAT的 一般计算过程所有的数据类型 先转换成双精度DOUBLE 计算出结果了 再装换成相应的输出格式
因为A B都是INT,所以(a+b)/2的结果是2 后面的是2.0 所以运行结果是4.0000000
第3个回答  2021-12-09
输出结果为: A的print功能。 B的print功能。 p=&a; fun(p); // p指向a,执行A::print() p=&b; fun(p); // p指向b,执行B::print(),因为A::print()是虚函数,被B::print()覆盖
第4个回答  2021-12-09
暗恋的程序运行结构的话,是直接点击回车键就可以弹出一个动画的,它是一个动画的程序。