【Coding日记】菜鸟编程C语言100例——第四题

今日收获——取地址符号➕数组的妙用

📚题目

question: input certain year,month,day,output which day of this year it is

🧠思路分析

anylsis:

  1. input year,month,day
  2. output number of days
  3. process
    a. accelarated days of previous months -> use array to store days of each year
    b. comsider special case of leap year -> how to judge it ?

🚀精选答案

#include <stdio.h>

int main(void)
{
    int year, month, day;
    int total_days = 0;
    int days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    // Input year, month, day
    printf("Enter year, month, day: ");
    scanf("%d %d %d",&year, &month, &day);
    //Check for Leap Year
    if( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    {
        days_in_month[2] = 29; // February has 29 days in a leap year
    }
    // calculate total days
    for(int i =1 ; i< month;i++) //previous months days
    {
        total_days += days_in_month[i];
    }
    total_days = total_days + day; // current month days
    //-> equal to total_days += day;

    // Output result
    printf("%d-%d-%d is the %dth day of %dyear.\n", year, month, day, total_days, year);
}

🤕我的试错之旅

一开始拿到题目,三下五除二地分析了一波,😅结果写代码的时候依旧卡壳,

😵‍💫到底怎么样才能在写代码的时候行云流水,
而且我也发现一个弊端——我现在用VScode 写代码,过分依赖编程助手了——诚然,他在我开发项目的时候是很大的功臣,帮助我提升效率,

😳看来当初就不该删除DEV c这个C语言初学者软件,起码他不会给提示,这样我做的效果更好,这个AI助手,让我有一个抄答案的感觉

💬总结碎碎念

进行编程,分三步走

  • 问题分析
    – 输入有什么
    – 要输出什么
    – 要处理什么
  • 代码编写
    – 初始化各种变量
    — 输入变量,输出变量,中间变量/数组
    –用数学逻辑解决编程问题
  • 最后完善
    – 例如,人机交互,和函数打包,以及算法优化

⚠️知识点

起因:输入scanf 需要取地址符号&,而输出printf 不需要在调用变量时候加取地址操作

原因: 输出的时候,变量所对应的地址已经存在“内容”——相当于已经开好房间了——只需要根据变量名字,去房间里,拿东西就行

而输入的时候,计算机需要知道,把它读取到的值,存取到那个内存地址,所以需要取地址符号&告诉它,存到哪里——类比,还没开好房间,需要🔑钥匙& 进入对应的房间,才能放东西

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值