gpt4 book ai didi

c++ - 排列组合 - 运行时失败

转载 作者:行者123 更新时间:2023-11-27 22:32:08 27 4
gpt4 key购买 nike

<分区>

这个程序从用户输入中获取 2 个数字,询问他们是否想找出排列或组合,然后输出结果。这是代码。

#include "std_lib_facilities.h"

int permutation(int first, int second)
{
int top_fac;
int bottom_fac;
for (int i = first-1; i >= 1; --i)
top_fac *=i;
for (int i2 = (first-second)-1; i2>=1; --i2)
bottom_fac *= i2;
return (top_fac/bottom_fac);
}

int combination(int first, int second)
{
int bottom_fac;
for (int i = second-1; i>=1; --i)
bottom_fac *= i;
return permutation(first, second)/(bottom_fac);
}

int main()
{
cout << "Enter two numbers.\n";
int first = 0;
int second = 0;
cin >> first >> second;
cout << "Now choose permutation(p) or combination(c).\n";
string choice;
cin >> choice;
if (choice == "p")
cout << "Number of permutations: " << permutation(first,second) << endl;
else if (choice == "c")
cout << "Number of combinations: " << combination(first,second) << endl;
else
cout << "p or c stupid.\n";
keep_window_open("q");
}

当我尝试运行该程序并选择 p 或 c 时,我收到一条消息“permutations_combinations.exe 已停止工作”。我试图捕捉到一个错误,但什么也没有发生。有什么想法吗?

提前致谢。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com