当我输入一个数字时,无论我使用什么数字,我都会得到 1。我该如何解决这个问题?

发布于 2024-10-18 17:46:49 字数 2393 浏览 1 评论 0原文

//"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;
    }
}
//"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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开半夏魅人心 2024-10-25 17:46:49

在您的方法 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,或约为 0.27 。假设小组人数为 3,并且第二个人没有击中第一个人,则第三个人的几率为 2 / 365,即大约 0.54% 的几率。假设随机数生成器是完全随机的,那么两个生日发生冲突的机会将为 (groupsize - 1) / 365。

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


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

In your method FindThePerctange you have an issue. Inside the while loop, you declare the variable j, set it to 0, increment it to 1, then return it immediately. This means you'll get 1 every time.

What I think you are trying to do is increment j each time in the while loop. If you are, put the declaration of j outside the while loop, like so:


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;
    }
}

This still doesn't make your code work, but it will at least not return 1. I'm not going to re-write your entire program for you (someone else probably will) but it does not do what you are expecting it to do.

Edit: I want to help you more, but your code is internally flawed. Your biggest problem is probably the fact that you don't assign any value to the DayOfBirth array. I would suggest going back and re-coding this. The problem you are asking is trivial. Consider the following:

Assume each person's birthday is a random number from 1 to 365. The first person's birthday then is assigned A. The second person has a 1 in 365 chance of hitting the same day. So for group size 2, the chance is 1 / 365, or about .27 . Assuming group size is 3, and the 2nd person didn't hit the first, the third person has a chance of 2 / 365, or about .54% chance. Assuming that the random number generator is completely random, your chance that two birthdates will collide is going to be (groupsize - 1) / 365.

So your code will only need to accept a GroupSize. It is shortened to:


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

月依秋水 2024-10-25 17:46:49

您既没有在任何地方调用 RandArray(),也没有 DayofBirth[] 获得任何值。

Neither are you calling RandArray() anywhere nor is DayofBirth[] getting any value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文