每日一分享C语言代码(The first day)

  分离一个四位整数的千,百,十和个位数

 这是常用的分离每一位数的方法

#include <stdio.h>	   // 标准输入输出库函数
int main()             // 定义main函数
{
   // 请在此添加代码
   /********** Begin *********/
   int a,b,c,d,e,f,g;  //此处都定义为整型,不可以定义为浮点数型
   scanf("%d",&a);
   b=a/1000;           //此处为千位
   c=a/100,d=c%10;     //此处为百位
   e=a/10,f=e%10;      //此处为十位
   g=a%10;             //此处为个位
   printf("%d,%d,%d,%d",b,d,f,g);//当其为整型时,小数点会被忽略取为个位上的数
    
    
   /********** End **********/
    return 0;
}

这个也叫做基础代码,就是可以运用在很多种情况或者是套用在一个比这个程序大的程序中。

大多数在大学大一里考试比较常考!多多复习,百炼成钢! 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个较为复杂的万年历的 C 语言编程代码,它支持任意年份的万年历: ```c #include <stdio.h> int is_leap_year(int year); int get_day_of_week(int year, int month, int day); int get_days_in_month(int year, int month); void print_month(int year, int month); int main() { int year, month; printf("请输入年份: "); scanf("%d", &year); printf("请输入月份: "); scanf("%d", &month); printf("\n"); // 输出当年的日历 for (int i = 1; i <= 12; i++) { print_month(year, i); } printf("\n"); // 输出当月的日历 print_month(year, month); return 0; } // 判断是否是闰年 int is_leap_year(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } // 计算某一天是星期几 int get_day_of_week(int year, int month, int day) { int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; if (month < 3) { year--; } return (year + year / 4 - year / 100 + year / 400 + t[month - 1] + day) % 7; } // 获取某个月份的天数 int get_days_in_month(int year, int month) { int days_in_month[] = {31, 28 + is_leap_year(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; return days_in_month[month - 1]; } // 输出某个月份的日历 void print_month(int year, int month) { int days_in_month = get_days_in_month(year, month); int first_day_of_month = get_day_of_week(year, month, 1); printf("%d 年 %d 月\n", year, month); printf("日\t一\t二\t三\t四\t五\t六\n"); for (int i = 0; i < first_day_of_month; i++) { printf("\t"); } for (int i = 1; i <= days_in_month; i++) { printf("%d\t", i); if ((first_day_of_month + i) % 7 == 0) { printf("\n"); } } if ((first_day_of_month + days_in_month) % 7 != 0) { printf("\n"); } printf("\n"); } ``` 程序中使用了一些数学公式和算法来计算某一天是星期几,某个月份的天数等信息。运行程序后,用户可以输入年份和月份,然后程序会输出当年和当月的日历。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值