① c語言讀入一個年份和月份,列印出該月有多少天(考慮閏年)
呃。。。太長了
試試我的吧:
輸入樣例:年份 月份(2012 2)
#include<stdio.h>
intmonth[2][12]={31,28,31,30,31,30,31,31,30,31,30,31,
31,29,31,30,31,30,31,31,30,31,30,31};/*把月份的天數儲存下來*/
intrun(intyear){//判斷閏年的函數,1代表閏年
if(year%4==0&&!(year%100==0&&year%400!=0))return1;
elsereturn0;
}
intmain(){
while(1){
intyear,mon;
scanf("%d%d",&year,&mon);
if(year==0)break;//輸入年份為0,程序結束
printf("當月天數為:%d ",month[run(year)][mon-1]);
}
return0;
}
② C語言編程 編程序實現如下功能:輸入任意一個年份和月份,求出從公元1年1月1日到這個月的1日總共有
#include<stdio.h>
intMonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
intprint_month[7][8];
boolleap_year(intyear)
{
returnyear%4==0&&year%100!=0||year%400==0;
}
intget_days(intyear,intmonth,intday)
{//返回從公元1年1月1日到該日期的天數
inti;
intsum=0;
for(i=1;i<year;i++)
{
if(leap_year(i))
sum+=366;
elsesum+=365;
}
if(leap_year(year))
Month[2]=29;
for(i=1;i<month;i++)
sum+=Month[i];
Month[2]=28;
returnsum+day;
}
voidPrint(intyear,intmonth)
{//列印該月的日歷
inti,j;
for(i=0;i<7;i++)
for(j=0;j<8;j++)
print_month[i][j]=0;
intsum=get_days(year,month,1);
if(leap_year(year))
Month[2]=29;
intt=1;
j=sum%7;
i=0;
while(t<=Month[month])
{
print_month[i][j]=t++;
j++;
if(j>=7)
{
i++;
j=0;
}
}
printf(" %d月 ");
printf("**************************** ");
printf("日一二三四五六 ");
for(i=0;i<6;i++)
{
for(j=0;j<7;j++)
{
if(print_month[i][j]==0)printf("");
elseprintf("%-3d",print_month[i][j]);
}
printf(" ");
}
printf("*************************** ");
Month[2]=28;
}
intmain()
{
intyear,month,day;
printf("輸入日期:");
scanf("%d%d%d",&year,&month,&day);
printf("%d年%d月%d日是星期",year,month,day);
intt=get_days(year,month,day)%7;
if(t%7==0)printf("日 ");
elseprintf("%d ",t%7);
Print(year,month);
return0;
}
③ C語言:給出年份和月份,計算並顯示該年該月的天數
#include
int
main()
{
int
year,month,day;
printf("請輸入年份與月份:");
scanf("%d",&year);
scanf("%d",&month);
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
printf("%d是閏年\n",year);
else
printf("%d不是閏年\n",year);
}
else
printf("%d是閏年\n",year);
}
else
printf("%d不是閏年\n",year);
switch
(month)
{
case
1:
case
2:
case
3:
printf("%d是春季。\n",month);
break;
case
4:
case
5:
case
6:
printf("%d是夏季。\n",month);
break;
case
7:
case
8:
case
9:
printf("%d是秋季。\n",month);
break;
case
10:
case
11:
case
12:
printf("%d是冬季。\n",month);
break;
default:
printf("輸入錯誤.\n");
}
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||
(month==12))
printf("該月為31天!\n");
if((month==4)||(month==6)||(month==9)||(month==11))
printf("該月為30天!\n");
if(month==2)
{
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
printf("該月29天");
else
printf("該月28天");
}
else
printf("該月29天\n");
}
else
printf("該月28天\n");
}
return
0;
}
④ c語言輸入年份和天數,輸出月份和日期,如何寫啊,各為大俠幫幫忙吧,thanks,急!!
#include<stdio.h>
int leap(int a)
{
if(a%4==0&&a%100!=0||a%400==0)
return 1;
else
return 0;
}
int numble(int year,int m,int d)
{
int sum=0,i,j,a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
if(leap(year)==1)
for(i=0;i<m-1;i++)
sum+=b[i];
else
for(i=0;i<m-1;i++)
sum+=a[i];
for(j=2000;j<year;j++)
if(leap(j)==1)
sum+=366;
else
sum+=365;
sum+=d;
return sum;
}
void main()
{
int year,month,day,n;
printf("pleass input year,month,day\n");
scanf("%d%d%d",&year,&month,&day);
n=numble(year,month,day);
if((n%5)<4&&(n%5)>0)
printf("%d%d%d is fishing\n",year,month,day);
else
printf("%d%d%d is bask\n",year,month,day);
}
/*這個是漁夫三天打魚,兩天曬網的問題。你的問題和這個差不多。你先看看能不能給你提示解決你的問題。不能的話我在把它改改。*/
⑤ c語言編寫程序從鍵盤輸入年份和月份,計算出這一年的這一個月有多少天
#include<stdio.h>
int main()
{
int year,month,days,day;
printf("請輸入年月日");
scanf("%d-%d-%d",&year,&month,&days);
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: day=31;break;
case 4:
case 6:
case 9:
case 11:day=30;break;
case 2:
if(year%4==0&&year%100!=0||year%400==0)
day=29;
else
day=28;break;
default :printf("error ");
}
printf("這個月有%d天",day);
}
⑥ C語言中,怎樣獲得當前年份和月份
若用標准c語言庫
#include<stdio.h>
#include<time.h>
int main()
{
time_t now;
time(&now);
printf("\nNow is:%s\n",ctime(&now));
return 0;
}
若用TC編程器
#include<dos.h>
#include<stdio.h>
main()
{
struct date d;
struct time t;
getdate(&d);
gettime(&t);
printf("the current day is:%d-%d-%d\n",d.da_year,d.da_mon,d.da_day);
printf("the current time is:%d:%d:%d",t.ti_hour,t.ti_min,t.ti_sec);
getch();
}
⑦ C語言輸入年份和天數輸出對應的年月日
C語言輸入年份和天數輸出對應的年月日的源代碼如下:
#include<iostream>
intday(int&year,int&month);
intmain()
{
intyear{};
intmonth{};
std::cout<<"請輸入年和月(空格隔開):";
std::cin>>year>>month;
std::cout<<"該月天數:"<<day(year,month)<<' ';
return0;
}
(7)c語言怎麼獲取年份和月份擴展閱讀
1、C++ 標准庫沒有提供所謂的日期類型。C++ 繼承了 C 語言用於日期和時間操作的結構和函數。為了使用日期和時間相關的函數和結構,需要在 C++ 程序中引用 <ctime> 頭文件。
2、有四個與時間相關的類型:clock_t、time_t、size_t和tm。類型 clock_t、size_t 和 time_t 能夠把系統時間和日期表示為某種整數。
⑧ 一道關於c語言選擇結構簡單的題:編寫程序,接收用戶輸入的年份和月份,輸出該月的天數
1.注意英文半形標點,
2.if語句,if(month=1……)應該是==,if(month==1……);
3閏年判斷,能被4整除但不能被100整除,或者能被400整除,才是閏年if((year%4==0)&&(year%100!=0)||(year%400==0))……
4.輸入的時候,年月之間用逗號「,」隔開
應該沒問題了^_^
下面是改好的
#include <stdio.h>
void main()
{
int year,month,days,t;
printf("Please input year and month\n");
scanf("%d,%d",&year,&month);
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
days=31;
else if(month==2)
{
if((year%4==0)&&(year%100!=0)||(year%400==0))
days=28;
else
days=29;
}
else
days=30;
printf("days=%d",days);
}
⑨ C語言編程 從鍵盤輸入一個年份和月份,輸出該月有多少天(考慮閏年)
用switch選擇的:
#include<stdio.h>
int main()
{
int y,m;
printf("請依次輸入年,月: ");
scanf("%d,%d",&y,&m); //上機時注意打逗號,也可修改為其他格式
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:printf("這個月有31天 ");break;
case 2 : if(y%4==0&&y%100!=0||y%400==0) //最基本的判斷閏年的條件
printf("這個月有29天 ");
else printf("這個月有28天 ");break;
case 4:
case 6:
case 9 :
case 11:printf("這個月有30天 ");break;
}
return 0;
}用指針型列舉的:
#include<stdio.h>
int a[]={31,28,31,30,31,30,31,31,30,31,30,31},*p1,*p2,*p3,i=1,k=0;
int main()
{
int p1,p2;
printf("請依次輸入年·月: ");
scanf("%d%d",&p1,&p2);
if(p1%4==0&&p1%100!=0||p1%400==0)
a[1]=29;
if(p2>12)
printf("您的輸入有誤! ");
printf("這個月的天數為:%d ",a[p2-1]);
return 0;
}#include <stdio.h>
main()
{int a,b;
printf("請輸入年份和月份: ");
scanf("%d%d",&a,&b);
if (a%4==0&&a%100!=0)
if(b==2)
printf("這個月有29天 ");
else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("這個月有31天 ");
else
printf("這個月有30天 ");
else if(b==2)
printf("這個月有28天 ");
else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("這個月有31天 ");
else
printf("這個月有30天 ");}普通
#include <stdio.h>
main(){
int a,b;
printf("請輸入年份和月份: ");
scanf("%d%d",&a,&b);
if (a%4==0&&a%100!=0)
if(b==2)
printf("這個月有29天 ");
else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("這個月有31天 ");
else
printf("這個月有30天 ");
else if(b==2)
printf("這個月有28天 ");
else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("這個月有31天 ");
else
printf("這個月有30天 ");}
(9)c語言怎麼獲取年份和月份擴展閱讀
switch 語句可以處理多分支選擇問題,根據其中break 語句的使用方法,一般分三種情況。
在switch 語句的每個語句段中都使用break 語句,這是switch 語句的主要使用方法,一般形式為:
switch (表達式)
case 常量表達式1: 語句段1; break;
case 常量表達式2: 語句段2; break;
case常量表達式n: 語句段n; break;
case常量表達式n+1:語句段n+ 1; break;
default :
參考資料c語言網路經驗
⑩ C語言入門問題:輸入年份和月份,求該月有多少天
#include<stdio.h>
main(){
int year,mounth,days;
printf("input year,mounth ");
scanf("%d,%d",&year,&mounth);
if(mounth%2!=0||mounth==8){
days=31;//不管什麼年份,這幾個月天數都是固定的
}
else{
if(mounth==2){
if((year%4==0)&&(year%100!=0)||(year%400==0))
//判斷是否是閏年能被4整除不能被100整除,能被400整除的年份位閏年
days=29;
else
days=28;
}
else
days=30;
}
printf("該月份共有:%d天",days);
}
運行結果:
(10)c語言怎麼獲取年份和月份擴展閱讀:
printf()用法:
printf()函數的調用格式為:printf("<格式化字元串>",<參量表>)。
其中格式化字元串包括兩部分內容:一部分是正常字元,這些字元將按原樣輸出;另一部分是格式化規定字元,以"%"開始,後跟一個或幾個規定字元,用來確定輸出內容格式。
參量表是需要輸出的一系列參數,其個數必須與格式化字元串所說明的輸出參數個數一樣多,各參數之間用","分開,且順序一一對應,否則將會出現意想不到的錯誤。
比如:
int a=5678;
printf("a=%d ",a);
輸出結果為a=5678。