我环顾四周,但我仍然坚持一些逻辑。
我有一个包含生日列表的文件。我想找到最常见的生日(月/日)。
到目前为止我的逻辑:
int maxValue = 0;
int maxBday;
while (Bday >> month >> b >> day >> b >> year) // take in values a line at a time from the file
{
int a = totalDays(month, day); //function finds number of days into the year
//(i.e. Feb. 2 = 33, not considering leap years in this problem).
for (int i = 0; i < 365; i++)
{
if (i = a) //going through all 365 days until the number of days matches
{
bdays[i]++; //add one to the array at position i
if (bdays[i] > maxValue) //finding what the maximum value in the array is
{
maxBday = i; //max number of days
maxValue = bdays[i]; //Setting new max for next line
}
}
}
}
cout << "The most common birthday: " << maxBday;
}
稍后我将创建一个函数,将总天数转换为年份作为稍后的实际日期。
我的文件在 1 月 1 日有一个重复的日期,因此输出应该是 1 表示一年中的 1 天,但我没有得到任何输出。我已经输入了 cout 语句,函数的每个部分都被访问了,但循环永远不会结束。我真的不知道我的逻辑错误可能在哪里。
最佳答案
尝试
if(i == a)
否则程序会将 i 设置为 a 的值。它可能不是完整的解决方案。
关于c++ - 查找文件中的重复数字 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42917557/