import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份: ");
int year = scanner.nextInt();
System.out.print("请输入月份(输入0打印整年日历): ");
int month = scanner.nextInt();
if (month == 0) {
for (int i = 1; i <= 12; i++) {
printMonth(year, i);
}
} else {
printMonth(year, month);
}
}
private static void printMonth(int year, int month) {
System.out.println("\n" + year + "年" + month + "月");
System.out.println("日 一 二 三 四 五 六");
int firstDay = getFirstDayOfMonth(year, month);
int daysInMonth = getDaysInMonth(year, month);
for (int i = 0; i < firstDay; i++) {
System.out.print(" ");
}
for (int i =1; i <= daysInMonth; i++) {
System.out.printf("%2d ", i);
if ((i + firstDay) % 7 == 0) {
System.out.println();
}
}
System.out.println();
}
private static int getFirstDayOfMonth(int year, int month) {
int days = 0;
for (int i = 1900; i < year; i++) {
days += isLeapYear(i) ? 366 : 365;
}
for (int i = 1; i < month; i++) {
days += getDaysInMonth(year, i);
}
return (days + 1) % 7;
}
private static int getDaysInMonth(int year, int month) {
int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && isLeapYear(year)) {
return 29;
}
return daysInMonth[month];
}
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
}
就是一个日历问题
基于new bing的编写:
【主要功能:】
import java.util.Scanner; // 导入 Scanner 类
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // 创建 Scanner 对象
System.out.print("请输入年份: ");
int year = scanner.nextInt(); // 获取输入的年份
System.out.print("请输入月份(输入0打印整年日历): ");
int month = scanner.nextInt(); // 获取输入的月份
if (month == 0) { // 输入月份为 0,打印整年日历
for (int i = 1; i <= 12; i++) { // 遍历 1~12 个月份
printMonth(year, i); // 打印对应月份的日历
}
} else { // 输入月份非 0,打印该月份日历
printMonth(year, month); // 打印特定月份的日历
}
}
private static void printMonth(int year, int month) {
System.out.println("\n" + year + "年" + month + "月"); // 输出该年月信息
System.out.println("日 一 二 三 四 五 六"); // 输出日历表头
int firstDay = getFirstDayOfMonth(year, month); // 得到该月第一天是星期几
int daysInMonth = getDaysInMonth(year, month); // 得到该月的天数
for (int i = 0; i < firstDay; i++) { // 输出第一天之前的空格
System.out.print(" ");
}
for (int i =1; i <= daysInMonth; i++) { // 输出日历具体内容
System.out.printf("%2d ", i);
if ((i + firstDay) % 7 == 0) { // 每输出 7 天,换行
System.out.println();
}
}
System.out.println(); // 打印完一个月的日历后,再输出空行
}
private static int getFirstDayOfMonth(int year, int month) {
int days = 0;
for (int i = 1900; i < year; i++) { // 从 1900 年开始累加天数
days += isLeapYear(i) ? 366 : 365; // 如果是闰年,则累加 366 天,否则累加 365 天
}
for (int i = 1; i < month; i++) { // 累加该年该月之前的天数
days += getDaysInMonth(year, i);
}
return (days + 1) % 7; // 得到该月第一天是星期几
}
private static int getDaysInMonth(int year, int month) {
int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 定义每个月的天数
if (month == 2 && isLeapYear(year)) { // 如果是闰年的二月,返回 29 天
return 29;
}
return daysInMonth[month]; // 返回其他月份的天数
}
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; // 判断是否为闰年
}
}
整体编程思路:
1900年1月1日正好是星期一,所以,这个程序计算了要输出的日期(年、月)距离1900年1月1日有多少天,
然后用天数就可以7的余数就是所需求显示月份的第1天是星期几,然后根据该月的总天数打印日历。
在这个代码里,各函数具体功能及运行逻辑如下:
(1)main函数是程序的入口函数,在main函数中,输入了要显示的年份和月份,如果月份为0,则显示1-12月的所有日历。
代码的逻辑如下:
1)输入年、月
2)如果月份等于0,则通过for循环输出1-12月份的日历;
如果月份非0,则只输出该月份的日历,日历输出通过调用printMonth函数实现。
(2)printMonth函数用来显示某个年、月的日历。输出的时候从周日开始、周六结束,每行7天。
代码逻辑如下:
1)根据年、月获取该日期距离1900年1月1日的天数;
2)根据天数计算出该年、月的1日是星期几,这里通过调用getFirstDayOfMonth函数实现;
3)根据星期几在第一行输出n组空格(比如星期2,则在前面输出2组空格),然后将1日在对应的星期几下输出
4)每行输出7组(7天一行)
(3)getFirstDayOfMonth函数用来计算要显示的年、月的1日是星期几。
代码逻辑如下:
1)根据年、月获取该日期距离1900年1月1日的天数;
2)用天数除以7取余数,就是星期几
(4)getDaysInMonth函数用来获取要显示年、月一共有多少天(比如,输入1999、2,返回28)。
(5)isLeapYear函数用来判断输入的年份是否是闰年,闰年的2月有29天,全年366天,辅助计算总天数用的。
这段代码是一个 Java 程序,它实现了一个日历打印的功能。主要思路如下:
首先通过 Scanner 获取用户输入的年份和月份,如果输入的月份为 0,则打印整年的日历。
调用 printMonth 方法打印指定月份的日历。
printMonth 方法会分别调用 getFirstDayOfMonth 和 getDaysInMonth 方法来获取该月份的第一天是星期几以及这个月有多少天。
然后在控制台上按照规格输出日历。
getFirstDayOfMonth 方法首先计算从 1900 年到指定年份过去了多少天,然后加上月份之前的所有天数,并计算出该月份的第一天是星期几。
getDaysInMonth 方法使用一个数组来存储每个月的天数,根据是否是闰年来判断二月份的天数。
isLeapYear 方法判断一个年份是否是闰年,通过判断能否被 4 整除但不能被 100 整除,或者能够被 400 整除来实现。
总体来说,这段代码通过对日期和时间的计算来实现了一个基本的日历打印功能。
//用近似值判断!控制精度!
#include <stdio.h>
int main() {
float i = 1.456;
if (i-1.456>-0.000001&&i-1.456<0.000001) {
printf("相等\n");
}
else {
printf("不相等\n");
}
printf("%f\n", i);
return 0;
}
我们知道float类型的有效数字位6~7位,指的是十进制的有效位,而不是上图中存储过程中的23位!!!
再来看一下,float类型中23位的小数部分,转换成十进制 2232^{23}223=8388608,是7位!但最高位是8!并不是9999999(7个9,最多6个9)所以float的有效位是6~7位!它指的是十进制的!!!
2232^{23}223=8388608,是7位!但最高位是8!并不是9999999(7个9,最多6个9)所以float的有效位是6~7位!它指的是十进制的!!!
我可以为您提供一篇编程思路分析的文章,针对不同的问题给出解决方案。思路分析的关键在于拆解问题,将问题逐步分解为更小的子问题,然后再对每个子问题给出解决方案。下面以段落0中的代码为例,进行思路分析和代码讲解:
首先我们要分析这段代码的功能,它的作用是根据给定的字符串创建一个广义表。在创建广义表时,需要根据字符串的内容进行判断,如果字符串是空字符串,则广义表为空;如果字符串只包含一个字符,则广义表只包含一个原子;如果字符串包含多个字符,则需要将字符串进行分割,然后通过递归不断创建子表,最终生成整个广义表。
那么具体来说,该代码的实现分为以下步骤:
设置字符串的长度len,用于后面的判断和分割操作;
对于字符串为空的情况,直接返回NULL;
对于只包含一个字符的情况,创建一个结点表示该原子,然后返回该结点;
对于包含多个字符的情况,先将字符串分割为表头和表尾,然后再递归创建表头和表尾的子表,生成一个广义表结点并返回。
代码实现如下:
//创建广义表
GListNode *createGList(char str[])
{
GLNode *head;
int len = strlen(str);
if (strcmp(str, "()") == 0) //是空广义表
head = NULL;
else if (len == 1) //广义表只有一个原子
{
head = (GLNode*)malloc(sizeof(GLNode));
head->tag = 0;
head->val.atom = str[0];
}
else //广义表不止一个原子
{
head = (GLNode*)malloc(sizeof(GLNode));
head->tag = 1;
char hstr[200];
decomposeStr(str, hstr); //将广义表分解为表头和表尾
head->val.sublist.head = createGList(hstr); //创建表头广义表
if (strcmp(str, "()") != 0) //如果表尾广义表不为空,创建表尾广义表
head->val.sublist.tail = createGList(str);
else
head->val.sublist.tail = NULL;
}
return head;
}
以上就是该代码的思路和实现,通过对代码进行分析,我们可以更好地理解程序的具体过程和思路。在编写代码时,我们应该遵循这样的思路,不断将大问题分解为小问题,然后再对每个小问题进行具体的编程实现。