从键盘输入年、月、日,输出它是当年的第几天
- 格式:doc
- 大小:24.00 KB
- 文档页数:1
求某月某日是该年的第多少天/*1、一个函数days,实现求某月某日是该年的第多少天。
由主函数将年、月、日传递给days函数,计算后将日数传回主函数输出,用结构体实现程序*/#include<stdio.h>struct{int year;int month;int day;}data;void main(){int i,days;int day_a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //补0是为了对齐,因为数组下标是从0开始while(1){printf("请输入年月日:\n");scanf("%d%d%d",&data.year,&data.month,&data.day);days=0;for(i=1;i<data.month;i++){days+=day_a[i];}days+=data.day; //求天数和if(((data.year%4==0 && data.year%100!=0)||(data.year%400==0))&&data.month>=3)//判断是否是闰年和是否大于三个月{days+=1;}printf("是该年的第%d天\n",days);}}运行结果如下:输入天数计算出是今年的几月几日/*2.定义一个结构体变量(包括年、月、日)。
任意输入小于或者等于365天的天数;计算出是今年的几月几日?*/#include<stdio.h>struct{int year;int month;int day;}data;void main(){int i=1,j=0,day;int day_a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};while(1){printf("请输入任意小于或者等于365的天数;\n");scanf("%d",&data.day);if(data.day>365||data.day<=0){printf("您输入的天数有误:\n");break;}for(i=1;(data.day>0)&&(data.day<=365);i++){day=data.day;data.day-=day_a[i];}data.month=i-1;printf("%d月 %d日\n",data.month,day);}}运行结果如下:广西民族师范学院何炯林2015年7月27日。
青岛理工大学课程实验报告
开始
输入x,y
X>y
Gcd=y Gcd=x
x/gcd!=0或y/gcd!=0
输出gcd Gcd=gcd-1
Lcm=x*y/gcd
输出lcm
结束
调试过程及
实验结果
总结(对实验结果进行分析,问题回答,实验心得体会及改进意见)
1)编译过程中cout格式不正确。
已经改正,也可用printf输出,但在c++程序中要用cout和cin来输出和输入,一般不用printf。
2)编译过程中switch语句格式不恰当。
经改正后运行良好。
3)在第二个程序中忘记声明函数对象,修正后可以运行。
4)第二个程序主要运用了函数调用和for语句,这一个程序的关键是如何声明一个函数,调用一个函数,。
附录1)#include<iostream>
#include<math.h>
using namespace std;。
java输⼊某年某⽉某⽇,判断这⼀天是这⼀年的第⼏天题⽬:输⼊某年某⽉某⽇,判断这⼀天是这⼀年的第⼏天?程序分析:以3⽉5⽇为例,应该先把前两个⽉的加起来,然后再加上5天即本年的第⼏天,特殊情况,闰年且输⼊⽉份⼤于3时需考虑多加⼀天。
程序设计:import java.util.*;public class test {public static void main (String[]args){int day=0;int month=0;int year=0;int sum=0;int leap;System.out.print("请输⼊年,⽉,⽇\n");Scanner input = new Scanner(System.in);year=input.nextInt();month=input.nextInt();day=input.nextInt();switch(month) /*先计算某⽉以前⽉份的总天数*/{case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;default:System.out.println("data error");break;}sum=sum+day; /*再加上某天的天数*/if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/leap=1;elseleap=0;if(leap==1 && month>2)/*如果是闰年且⽉份⼤于2,总天数应该加⼀天*/sum++;System.out.println("It is the the day:"+sum);}}。
控制台输⼊年⽉⽇,输出该⽇期为该年的第多少天?控制台输⼊年⽉⽇,输出该⽇期为该年的第多少天?(java基础开发只⽤if..else if..和switch...case)⽅法⼀:只⽤if..elseSystem.out.print("请输⼊年份:");int year=input.nextInt();int sum = 0;if(year>=1900&&year<=2099){//较验年份System.out.print("请输⼊⽉份:");int mouth = input.nextInt();boolean isLeapYear = (year%4==0 && year%100!=0)||year%400==0;//判断闰年if(mouth<=12&&mouth>=1) {//校验⽉份System.out.print("请输⼊⽇:");int day = input.nextInt();int cheakDay = 0;for(int i = 1;i<=mouth;i++){if(i == 4 || i == 6 || i == 9 || i == 10 || i == 11){sum += 30;cheakDay = 30;}else if(isLeapYear && i == 2){sum += 29;cheakDay = 29;}else if(!isLeapYear && i == 2){sum += 28;cheakDay = 28;}else {sum += 31;cheakDay = 31;}}if(day>0&&day<=cheakDay){//校验⽇sum = (sum+day)-cheakDay;System.out.println(year+"年"+mouth+"⽉"+day+"⽇,在该年中的第"+sum+"天");}else {System.out.println("输⼊正确的⽇");}}else{System.out.println("请输⼊正确的⽉份");}}else{System.out.println("请输⼊正确的年份");}⽅法⼆:加⼊switch...caseint year, month, day;System.out.println("请输⼊4位年");year = input.nextInt();System.out.println("请输⼊⽉");month = input.nextInt();System.out.println("请输⼊⽇");day = input.nextInt();if (year < 1900 || year > 2099) {System.out.println("请输⼊正确的年,在1900-2099之间");} else if (month < 1 || month > 12) {System.out.println("请输⼊正确的⽉,在1-12之间");} else {boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;//判断闰年int days = 31;switch (month) {case 4:case 6:case 9:case 11:days = 30;break;case 2:days = isLeapYear ? 29 : 28;break;}if (day < 1 || day > days) {System.out.println("请输⼊1⾄" + days + "之间的天数");} else {int total = day;for (int i = 1; i < month; i++) {days = 31;switch (i) {case 4:case 6:case 9:case 11:days = 30;break;case 2:days = isLeapYear ? 29 : 28;break;}total += days;}String info = MessageFormat.format("{0}年{1}⽉{2}⽇是{0}年的第{3}天", year, month, day, total); System.out.println(info);}}。
Java从键盘上输⼊year“month”和“day”,要求通过程序输出输⼊的⽇期为第⼏年的第⼏天1/**2 * 编写程序:3 * 从键盘上输⼊"year"“month”和“day”,要求通过程序输出4 * 输⼊的⽇期为第⼏年的第⼏天5 * 注:判断⼀年是否是闰年的标准:6 * 可以被4整除,但不可被100整除7 * 或8 * 可以被400整除9 *10*/111213 Scanner scan = new Scanner(System.in);14 System.out.println("请输⼊年份year:");15int year = scan.nextInt();16 System.out.println("请输⼊"+year+"年的month:");17int month = scan.nextInt();18 System.out.println("请输⼊"+year+"年⽉份的day:");19int day = scan.nextInt();2021//定义变量保存总天数 if-else ⽅法冗余22int sumDays = 0;23/*2425 if(month == 1)26 {27 sumDays = day;28 }29 else if(month == 2)30 {31 sumDays = 31+ day;32 }33 //......34 else35 {36 //month == 1237 sumDays = 31+31+....+day;38 }39*/4041//⽅式⼆------------------------------------42/*43 switch(month)44 {45 case 1:46 sumDays = day;47 break;48 case 2:49 sumDays = 31 + day;50 break;51 case 3:52 sumDays = 31+31+day;53 break;54 ..............55 }56*/5758//⽅式三 break在switch-case结构中是可选的。
输入年月日计算这个日期是本年多少天<!DOCTYPE html><html><head><meta charset="utf-8"><title></title><script>let year = parseInt(prompt("请输入年:"));let month = parseInt(prompt("请输入月:"));let day = parseInt(prompt("请输入日:"));let sum = parseInt(0);if((year%4===0&&year%100!==0||year%400===0)&&month>2){sum++;}if(month<2){sum=day;}else if(month<3){sum=31+day;}else if(month<4){sum=31+28+day;}else if(month<5){sum=31+28+31+day;}else if(month<6){sum=31+28+31+30+day;}else if(month<7){sum=31+28+31+30+31+day;}else if(month<8){sum=31+28+31+30+31+30+day;}else if(month<9){sum=31+28+31+30+31+30+31+day;}else if(month<10){sum=31+28+31+30+31+30+31+31+day;}else if(month<11){sum=31+28+31+30+31+30+31+31+30+day;}else if(month<12){sum=31+28+31+30+31+30+31+31+30+31+day;}else{alert('日期错误')}document.write(month + '月' + day + '日是,' + year + '年的第' + sum + '天!')</script></head><body></body></html>。
python输⼊某年某⽉某⽇,判断这⼀天是这⼀年的第⼏天?1 year=int(input("请输⼊年份:"))2 month=int(input("请输⼊⽉份:"))3 day=int(input("请输⼊⽇期:"))4 bmonth=(1,3,5,7,8,10,12)5 smonth=(4,6,9,11)6 pday=(0,31,59,90,120,151,181,212,243,273,304,334)7 rday=(0,31,60,91,121,152,182,213,244,274,305,335)8if year<0:9print("您输⼊的年份有误")10elif year>=0 and year%400==0 or year % 100 != 0 and year % 4 == 0:11if month>12 or month<1:12print("您输⼊的⽉份有误")13else:14if month in bmonth and day <=31 and day >0 or month in smonth and day <=30 and day >0 or month==2 and day<=29 and day>0:15print("输⼊的⽇期为%d年的第%d天" % (year, int(rday[month - 1] + day)))16else:17print("您输⼊的⽇期有误")18else:19if month>12 or month<1:20print("您输⼊的⽉份有误")21else:22if month in bmonth and day <=31 and day >0 or month in smonth and day <=30 and day >0 or month==2 and day<=28 and day>0:23print("输⼊的⽇期为%d年的第%d天" % (year, int(pday[month - 1] + day)))24else:25print("您输⼊的⽇期有误")其实我有点疑问就是python的if语句可不可以连接多个or和and⼀起使⽤?这代码在PyCharm跑出来是没问题的。
python中输⼊某年某⽉某⽇,判断这⼀天是这⼀年的第⼏天?
输⼊某年某⽉某⽇,判断这⼀天是这⼀年的第⼏天?
程序分析特殊情况,闰年时需考虑⼆⽉多加⼀天:
直接上代码
#定义⼀个函数,判断是否为闰年
def leapyear(y):
return (y % 400 == 0 or (y % 4 ==0 and y % 100 ==0))
#定义⼀个数组,每个⽉的天数,由于python中的数组是从0开始,⽽⽉份是从1开始,所以数组第⼀个数为0
days = [0,31,28,31,30,31,30,31,31,30,31,30]
#存储⽉份的天数
res = 0
#由⽤户输⼊年⽉⽇
year = int(input("请输⼊年份:"))
month = int(input("请输⼊⽉份:"))
day = int(input("请输⼊⽇期:"))
#如果是闰年的话,2⽉份加⼀天
if leapyear(year):
days[2] += 1
#遍历⼀次days,对应⽉份中的天数,把对应的天数传递给res存储
for i in range(month):
res += days[i]
#打印出天数!
print(f"这是{year}年的第{res+day}天")
*******************新⼿,有不对的地⽅望指证!。