两道未知的编程题!!
我主要学的C#,今天朋友给我出了两道难题
编程题(C、JAVA、C++等均可)
1.编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n。(利用指针函数)
2.输入某年某月某日,判断这一天是这一年的第几天?
*大神门,我该怎么解决?C#不是很流行么?
[解决办法]
#include<stdio.h>
void main()
{
int n;
double (*p)(int);
double odd(int);
double d(int);
printf("请输入一个整数\n");
scanf("%d",&n);
if(n%2)
p=odd;
else
p=d;
printf("结果是:%lf",p(n));
}
double odd(int n)
{
int i;
double s=0.0;
for(i=1;i<=n;i+=2)
s+=1.0/i;
return s;
}
double d(int n)
{
int i;
double s=0.0;
for(i=2;i<=n;i+=2)
s+=1.0/i;
return s;
}
[解决办法]
#include<stdio.h>
void main()
{
int year,month,day;
int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,sum=0;
scanf("%d%d%d",&year,&month,&day);
if((year%400==0)
[解决办法]
(year%4==0&&year%100!=0))//润年
if(month>2)
m[0]=1;
for(i=0;i<month;i++)
sum+=m[i];
sum+=day;
printf("%d",sum);
}
[解决办法]
2.COleDateTime::GetDayOfYear
int GetDayOfYear( ) const;
Return Value
The day of the year represented by the value of this COleDateTime object.
Remarks
Call this member function to get the day of the year represented by this date/time value.
Valid return values range between 1 and 366, where January 1 = 1. If the status of this COleDateTime object is not valid, the return value is AFX_OLE_DATETIME_ERROR.
For information on other member functions that query the value of this COleDateTime object, see the following member functions:
GetDay
GetMonth
GetYear
GetHour
GetMinute
GetSecond
GetDayOfWeek
COleDateTime Overview
[解决办法]
Class Members
[解决办法]
Hierarchy Chart
See Also COleDateTime::COleDateTime, COleDateTime::SetDateTime, COleDateTime::operator =, COleDateTime::GetStatus