【C语言经典100题】(1月24日-day41)

cr:经典

题目41

按位运算符:&、|、^

思路:

代码:

对方:

反思&收获:

& 和 | 都是之前就学习过的,只有 ^ 是今天刚了解到的:相同为0,不同为1。巧记为“异为一”。


cr:小林

例52

输入一行字符,C语言编程分别统计出其中英文字母、空格、数字和其他字符的个数。

思路:

统计英文字母、空格、数字以及其他字符的个数。英文字母只需在26个字母的范围内即可统计,字符则是在ASCLL码表里范围内即可,数字也是在范围内即可;最难的是空格的计数。可以在统计完其余三项之后最后得出空格的个数。

代码:

#include<stdio.h>
main()
{
	char a;
	int i, sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
	printf("输入一行长度为10的字符:");
	while ((a = getchar()) != '\n') {
		if (a >= 'A' && a <= 'Z' || a >= 'a' && a <= 'z') {
			sum1++;
		}
		else if (a >= '0' && a <= '9') {
			sum2++;
		}
		else if (a == ' ') {
			sum3++;
		}
		else {
			sum4++;
		}
	}
	printf("\n字母:%d个\n数字:%d个\n空格:%d个\n符号:%d个\n", sum1, sum2, sum3, sum4);
}

对方:

反思&收获:

getchar和gets函数没有区分清楚。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是用 C 语言实现将一个毫秒数转换成年月分秒的程序: ```c #include <stdio.h> void msToDateTime(long long milliseconds) { // 每个月的天数(非闰年) int daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 获取当前时间戳的秒数 long long seconds = milliseconds / 1000; // 计算年份 int year = 1970; while (1) { int daysInYear = 365; if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { daysInYear = 366; // 当年是闰年 } if (seconds < daysInYear * 24 * 60 * 60) { break; } seconds -= daysInYear * 24 * 60 * 60; year++; } // 计算月份和期 int month = 1; while (1) { int daysInMonth = daysInMonth[month - 1]; if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) { daysInMonth = 29; // 当月是闰年的二月 } if (seconds < daysInMonth * 24 * 60 * 60) { break; } seconds -= daysInMonth * 24 * 60 * 60; month++; } int day = seconds / (24 * 60 * 60) + 1; seconds = seconds % (24 * 60 * 60); // 计算小时、分钟和秒钟 int hour = seconds / (60 * 60); seconds = seconds % (60 * 60); int minute = seconds / 60; int second = seconds % 60; // 输出结果 printf("时间为:%d年%d月%d %02d时%02d分%02d秒\n", year, month, day, hour, minute, second); } int main() { long long milliseconds = 1689230512000; // 输入要转换的毫秒数 msToDateTime(milliseconds); return 0; } ``` 运行以上程序,将毫秒数1689230512000转换成年月时分秒的时间为:2023年10月26 00时01分52秒。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜東

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值