gpt4 book ai didi

c++ - 当我输入一个数字时,无论我使用什么数字,我都会得到 1 。我怎样才能解决这个问题?

转载 作者:行者123 更新时间:2023-11-28 01:08:07 25 4
gpt4 key购买 nike

//"This program will ask for the number of people in a group and then output percentage likelyhood that two birthdays occur on the same day." << endl << endl;


#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

int FindThePerctange(int [], int);
void RandArray(int, int []);
void Counter(int []);

int main (){
int GroupNumber;
int DayOfBirth [365] = {};
char Percant;
Percant = '%';
cout << "This program will ask for the number of people in a group and then output percentage likelyhood that two birthdays occur on the same day." << endl << endl;
cout << "How many in group (0 quits)? ";
cin >> GroupNumber;
if (GroupNumber == 0){
cout << "\nThanks for using this program.";
srand(time(0));
}
else{
cout << "In a group of " << GroupNumber << " the chances for two birthdays the same is " << FindThePerctange(DayOfBirth, GroupNumber) << Percant << "." << endl << endl;
cout << "How many in group (0 quits)? ";
cin >> GroupNumber;
cout << "In a group of " << GroupNumber << " the chances for two birthdays the same is " << FindThePerctange(DayOfBirth, GroupNumber) << Percant << "." << endl << endl;
cout << "How many in group (0 quits)? " << endl;
cin >> GroupNumber;
cout << "Thanks for using this program.";
}
cin.get();
cin.get();
return 0;
}


int FindThePerctange(int DayOfBirth [], int GroupNumber){
double Overlap = 0, Percentage;
for (int d =0; d<=10000; d++){
(GroupNumber, DayOfBirth);
while (d <= 365){
int j;
j = 0;
j++;
if(DayOfBirth[j] >= 2){
Overlap = Overlap + 1;
j=365;
}
return j;
}
Percentage = (Overlap/10000)*100;
return Percentage;
}
}

void RandArray(int GroupNumber, int DayOfBirth[]){
int Day, d;
while (d < GroupNumber){
Day = rand()%365;
DayOfBirth[Day] +=1;
d++;
}
}

void Counter(int DayOfBirth[]){
for (int d = 0; d<=365; d++){
DayOfBirth[d] = 0;
}
}

最佳答案

在您的 FindThePerctange 方法中,您遇到了问题。在 while 循环中,声明变量 j,将其设置为 0,将其递增为 1,然后立即返回。这意味着你每次都会得到 1。

我认为您要做的是在 while 循环中每次递增 j。如果是,请将 j 的声明放在 while 循环之外,如下所示:


int FindThePerctange(int DayOfBirth [], int GroupNumber){
double Overlap = 0, Percentage;
int j;
for (int d =0; d<=10000; d++){
//(GroupNumber, DayOfBirth);
j = 0;
while (d <= 365){
j++;
if(DayOfBirth[j] >= 2){
Overlap++;
j=365;
}
}
Percentage = (Overlap/10000)*100;
return Percentage;
}
}

这仍然不能使你的代码工作,但它至少不会返回 1。我不会为你重写你的整个程序(其他人可能会)但它不会做你想要的期待它做。

编辑:我想为您提供更多帮助,但您的代码存在内部缺陷。您最大的问题可能是您没有为 DayOfBirth 数组分配任何值。我建议返回并重新编码。你问的问题很简单。请考虑以下事项:

假设每个人的生日是 1 到 365 之间的随机数。那么第一个人的生日被指定为 A。第二个人有 365 分之一的机会碰上同一天。因此对于组大小 2,机会是 1/365,或大约 .27。假设小组人数为 3,并且第二个人没有击中第一个人,则第三个人有 2/365 的几率,或者大约 0.54% 的几率。假设随机数生成器是完全随机的,那么两个生日发生冲突的几率将是 (groupsize - 1)/365。

因此您的代码只需要接受 GroupSize。它被缩短为:


float FindThePerctange(int GroupNumber){
return (GroupNumber - 1) / 365 * 100;
}

关于c++ - 当我输入一个数字时,无论我使用什么数字,我都会得到 1 。我怎样才能解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5055223/

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