#include <stdio.h>
int main()
{
int day[30],
month[11],
year[2] = { 2000, 2001 };
int combinations[743];
printf("Days:\n");
for (int i = 1; i <= 31; i++)
{
day[i - 1] = i;
printf("%d ", day[i-1]);
}
printf("\n\nMonth:\n");
for (int j = 1; j <= 12; j++)
{
month[j - 1] = j;
printf("%d ", day[j - 1]);
}
printf("\n\nYear:\n%d %d\n\n", year[0], year[1]);
for (int x = 0, y = 0, z = 0, k = 0;
x <= 30, y <= 11, z <= 1, k <= 743;
x++, y++, z++, k++)
{
if (x == 31)
{
x = 0;
}
if (y == 11)
{
y = 0;
}
combinations[k] = day[x],".",month[y],".",year[0];
}
for (int a = 0; a <= 20; a++)
{
printf("Combination: %d \n", combinations[a]);
}
getch();}
我想制作一个程序,打印出2000、2001个人生日的所有组合,但是输出中出现了一些奇怪的问题
它看起来像这样:
天: 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
月份: 1 2 3 4 5 6 7 8 9 10 11 12
年份: 2000 2001
组合:1 组合:2 组合:3 组合:4 组合:5 组合:6 组合:7 组合:8 组合:9 组合:10 组合:11 组合:12 组合:13 组合:14 组合:15 组合:16 组合:17 组合:18 组合:19 组合:20 组合:21
答案 0 :(得分:1)
首先,在这里出现快速错误:
这样做可以使读取和正确执行变得更加容易,因为在您的代码中,该循环将只执行两次,因为Z停在2,并且您每次迭代都增加它。
for(year = 0; year < 2 ; year ++)
for(month = 0; month < 12; month ++)
for(day = 0; day < 31; day++)
//combination code goes here
最后,如果要保存日期,则不能将其设置为整数,除非将日期加起来并用作参考,否则,请创建一个结构,例如“ 11-05-2000”为此或将其另存为字符串。其次,您假设每个月31个...
您可能有任何其他疑问:)
答案 1 :(得分:0)
#include <stdio.h>
int main()
{
//changed days to 31 and months to 12
int day[31],
month[12],
year[2] = { 2000, 2001 };
//made three arrays one for days,one for months,one for year storage, or you can use structure array, structure should have three fields
int combinations1[744];
int combinations2[744];
int combinations3[744];
printf("Days:\n");
for (int i = 1; i <= 31; i++)
{
day[i - 1] = i;
printf("%d ", day[i-1]);
}
printf("\n\nMonth:\n");
for (int j = 1; j <= 12; j++)
{
month[j - 1] = j;
printf("%d ", day[j - 1]);
}
printf("\n\nYear:\n%d %d\n\n", year[0], year[1]);
int x,y,z,k=0;
//this will be code for storing all combination of days, looping through all days of a month of a year, then to next month and finally to next year
for(x = 0; x < 2 ; x ++)
{
for(y = 0; y < 12; y ++)
{
for(z = 0; z < 31; z++)
{
combinations1[k]=z+1;
combinations2[k]=y+1;
combinations3[k]=year[x];
k++;
}
}
}
// printing all combinations
for (int a = 0; a < 744; a++)
{
printf("Combination: %d.%d.%d\n", combinations1[a],combinations2[a],combinations3[a]);
}
//getch();
}
我已经在注释中解释了代码。请阅读评论以了解