📜  用几行代码打印圣诞节的12天| xmas.c代码(1)

📅  最后修改于: 2023-12-03 15:40:55.304000             🧑  作者: Mango

用几行代码打印圣诞节的12天

如果你正在寻找一些简单的代码来庆祝圣诞节,那么你来对地方了!下面是一个使用C语言编写的程序,可以打印出经典的圣诞歌曲“12天”。

代码
#include <stdio.h>

int main()
{
    // 初始化歌曲中用到的数组
    char* days[] = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"};
    char* gifts[] = {"a partridge in a pear tree", "two turtle doves", "three French hens", "four calling birds", "five golden rings", "six geese a-laying", "seven swans a-swimming", "eight maids a-milking", "nine ladies dancing", "ten lords a-leaping", "eleven pipers piping", "twelve drummers drumming"};

    // 遍历数组并打印输出
    for(int i=0; i<12; i++)
    {
        printf("On the %s day of Christmas\n", days[i]);
        printf("My true love sent to me:\n");

        for(int j=i; j>=0; j--)
        {
            if(i>0 && j==0)
            {
                printf("And %s\n", gifts[j]);
            }
            else
            {
                printf("%s\n", gifts[j]);
            }
        }

        printf("\n");
    }

    return 0;
}
代码解释

这个程序的核心思路很简单:使用两个字符串数组来存储每个“天数”和对应的“礼物”,然后嵌套两层循环来输出这些信息。

外层循环遍历12个“天数”,内层循环从当前“天数”往前遍历所有的“礼物”(包括前面每一天数送的“礼物”和当前天数送的“礼物”)。在内层循环中,根据当前“天数”和“礼物”的位置,选择正确的词语拼接到输出字符串中。

运行结果

程序运行后,会输出以下内容:

On the first day of Christmas
My true love sent to me:
a partridge in a pear tree

On the second day of Christmas
My true love sent to me:
two turtle doves
And a partridge in a pear tree

On the third day of Christmas
My true love sent to me:
three French hens
two turtle doves
And a partridge in a pear tree

On the fourth day of Christmas
My true love sent to me:
four calling birds
three French hens
two turtle doves
And a partridge in a pear tree

On the fifth day of Christmas
My true love sent to me:
five golden rings
four calling birds
three French hens
two turtle doves
And a partridge in a pear tree

On the sixth day of Christmas
My true love sent to me:
six geese a-laying
five golden rings
four calling birds
three French hens
two turtle doves
And a partridge in a pear tree

On the seventh day of Christmas
My true love sent to me:
seven swans a-swimming
six geese a-laying
five golden rings
four calling birds
three French hens
two turtle doves
And a partridge in a pear tree

On the eighth day of Christmas
My true love sent to me:
eight maids a-milking
seven swans a-swimming
six geese a-laying
five golden rings
four calling birds
three French hens
two turtle doves
And a partridge in a pear tree

On the ninth day of Christmas
My true love sent to me:
nine ladies dancing
eight maids a-milking
seven swans a-swimming
six geese a-laying
five golden rings
four calling birds
three French hens
two turtle doves
And a partridge in a pear tree

On the tenth day of Christmas
My true love sent to me:
ten lords a-leaping
nine ladies dancing
eight maids a-milking
seven swans a-swimming
six geese a-laying
five golden rings
four calling birds
three French hens
two turtle doves
And a partridge in a pear tree

On the eleventh day of Christmas
My true love sent to me:
eleven pipers piping
ten lords a-leaping
nine ladies dancing
eight maids a-milking
seven swans a-swimming
six geese a-laying
five golden rings
four calling birds
three French hens
two turtle doves
And a partridge in a pear tree

On the twelfth day of Christmas
My true love sent to me:
twelve drummers drumming
eleven pipers piping
ten lords a-leaping
nine ladies dancing
eight maids a-milking
seven swans a-swimming
six geese a-laying
five golden rings
four calling birds
three French hens
two turtle doves
And a partridge in a pear tree