C语言算法实例(四)

C语言算法实例(四)

输入任意日期,判断此年为平年还是闰年?判断此天为此年的第几天?

题目分析:
  1. 闰年解释:闰年分为普通闰年和世纪闰年;普通闰年的年份能被4整除而不能被100整除,世纪闰年的年份能被400整除。

实现代码


#include "stdio.h"
/*如果函数定义在main函数之后,需要在使用之前进行函数声明*/
unsigned int leap_normal_year_judg( unsigned int year);

int main()
{
    unsigned int year,month,day,sum_day,day_leap;
    printf("\n请输入日期,格式为:year-month-day  例:2021-05-07\n");
    scanf("%d-%d-%d",&year,&month,&day);
    if(month > 12 || day > 31)
    {
        printf("输入的year或day错误,请重新输入日期,格式为:year-month-day \n");      
        scanf("%d-%d-%d",&year,&month,&day);     
    }
    switch(month)
    {
        case 1: sum_day = 0; break;
        case 2: sum_day = 31; break;
        case 3: sum_day = 59; break;
        case 4: sum_day = 90; break;
        case 5: sum_day = 120; break;
        case 6: sum_day = 151; break;
        case 7: sum_day = 181; break;
        case 8: sum_day = 212; break;
        case 9: sum_day = 243; break;
        case 10: sum_day = 273; break;
        case 11: sum_day = 304; break;
        case 12: sum_day = 334; break;
        default: printf("输入月份有误\n");break;
    }
    if(leap_normal_year_judg(year) == 1)
    {
        printf("%d年为闰年\n",year);
        if(month > 2)
        {
            day_leap = 1;
        }
        else 
        {
            day_leap = 0;
        }
    }
    else 
    {
        day_leap = 0;
        printf("%d年为平年\n",year);
    }
    sum_day = sum_day + day + day_leap;
    printf("%d-%d-%d是%d年的第%d天\r\n",year,month,day,year,sum_day);
    return 0;
}

/************************************************************************************************** 
*  @brief  Leap year judgment function. 
* 	if the input parameter is leap year, it returns 1,otherwise it returns 0
**************************************************************************************************/
unsigned int leap_normal_year_judg( unsigned int year)
{
    if(year % 4 == 0)
    {
        if(year % 100 == 0)
        {
            if(year % 400 == 0)
            {
                return 1; //能被400整除---闰年 
            }
            else
            {
                return 0;
            }   
        }
        else 
        {
            return 1; //能被4整除,但不能被100整除--闰年
        }
    }
    else
    {
        /*不能被4整除 */
        return 0;
    }
}

运行结果
正确输入要求格式的日期并运行在这里插入图片描述
当月份或日期输入错误,需要重新输入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的遗传算法的C语言程序实例: ```c #include <stdio.h> #include <stdlib.h> #include <math.h> #define POP_SIZE 50 // 种群大小 #define NUM_VARS 1 // 变量个数 #define NUM_GENERATIONS 100 // 迭代次数 #define CROSSOVER_PROB 0.8 // 交叉概率 #define MUTATION_PROB 0.1 // 变异概率 // 目标函数 double func(double x) { return 2 * sin(x) + cos(3 * x); } // 个体结构体 typedef struct { double vars[NUM_VARS]; // 变量数组 double fitness; // 适应度 } Individual; // 初始化种群 void initializePopulation(Individual population[]) { for (int i = 0; i < POP_SIZE; i++) { for (int j = 0; j < NUM_VARS; j++) { population[i].vars[j] = (double)rand() / RAND_MAX * 10 - 5; } } } // 计算适应度 void calculateFitness(Individual population[]) { for (int i = 0; i < POP_SIZE; i++) { population[i].fitness = func(population[i].vars[0]); } } // 选择 void selection(Individual population[]) { // 排序种群 for (int i = 0; i < POP_SIZE - 1; i++) { for (int j = 0; j < POP_SIZE - i - 1; j++) { if (population[j].fitness > population[j+1].fitness) { Individual temp = population[j]; population[j] = population[j+1]; population[j+1] = temp; } } } // 选择前一半个体作为父代 for (int i = 0; i < POP_SIZE / 2; i++) { population[i + POP_SIZE / 2] = population[i]; } } // 交叉 void crossover(Individual parent1, Individual parent2, Individual* offspring1, Individual* offspring2) { int point = rand() % NUM_VARS; for (int i = 0; i < NUM_VARS; i++) { if (i <= point) { offspring1->vars[i] = parent1.vars[i]; offspring2->vars[i] = parent2.vars[i]; } else { offspring1->vars[i] = parent2.vars[i]; offspring2->vars[i] = parent1.vars[i]; } } } // 变异 void mutation(Individual* individual) { int point = rand() % NUM_VARS; individual->vars[point] += ((double)rand() / RAND_MAX) * 0.1; } // 更新种群 void updatePopulation(Individual population[]) { for (int i = POP_SIZE / 2; i < POP_SIZE; i++) { if ((double)rand() / RAND_MAX < CROSSOVER_PROB) { int parent1Index = rand() % (POP_SIZE / 2); int parent2Index = rand() % (POP_SIZE / 2); crossover(population[parent1Index], population[parent2Index], &population[i], &population[i+1]); i++; } else { int parentIndex = rand() % (POP_SIZE / 2); population[i] = population[parentIndex]; mutation(&population[i]); } } } // 打印最优解 void printBestSolution(Individual population[]) { double bestFitness = population[0].fitness; double bestSolution = population[0].vars[0]; for (int i = 1; i < POP_SIZE; i++) { if (population[i].fitness < bestFitness) { bestFitness = population[i].fitness; bestSolution = population[i].vars[0]; } } printf("最小值为 %f,对应的解为 %f\n", bestFitness, bestSolution); } int main() { Individual population[POP_SIZE]; // 初始化种群 initializePopulation(population); // 遗传算法迭代 for (int i = 0; i < NUM_GENERATIONS; i++) { // 计算适应度 calculateFitness(population); // 选择 selection(population); // 更新种群 updatePopulation(population); } // 输出结果 printBestSolution(population); return 0; } ``` 请注意,这只是一个简单的示例程序,具体的遗传算法实现可能会根据实际情况有所调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值