第1个回答 推荐于2016-01-22
#include <math.h>
//最笨的方法
int NumPick()
{
int num1 = 0, num2 = 0;
int i = 0;
for(i=0; i<100000; i++)
{
num1 = i + 100;
num2 = (int)sqrt(num1);
if(num2*num2 == num1)
{
num1 += 168;
num2 = (int)sqrt(num1);
if(num2*num2 == num1)
{
break;
}
}
}
return i;
}
//第2题
//记录每一个月的天数
int m_nDay[2][13]={
{365,31,28,31,30,31,30,31,31,30,31,30,31},
{366,31,29,31,30,31,30,31,31,30,31,30,31}
};
//判断是否闰年
int Isnonleapyear(int year){
if (year%100 == 0) {
if (year%400 == 0) return 1;
else return 0 ;
}
else if (year%4 == 0) return 1;
else return 0;
}
//计算是哪一天
int GetTheDayOfYear(int year, int month, int day)
{
int dayCount = 0;
int i=0;
if(Isnonleapyear(year) == 1)
{
for(i=1; i<13; i++)
{
dayCount += m_nDay[1][i];
}
dayCount += day;
}
else if(Isnonleapyear(year) == 0)
{
for(i=1; i<13; i++)
{
dayCount += m_nDay[0][i];
}
dayCount += day;
}
return dayCount;
}
//第3题
//假设他们近亲结婚,然后从一而终啊
int HowManyRabbit(int month)
{
int cnt = month / 3 + 1;//有多少个3个月
return pow(2,cnt-1)*2;
}本回答被提问者采纳
第2个回答 2014-05-18
#include <stdlib.h>
#include <math.h>
//-----------------------------------------------------
//第一题
//判断是否是完全平方数
bool IsSquare(unsigned int num)
{
doubledblNum = num;
unsignedint nSqrtVal = sqrt(dblNum);
if(nSqrtVal*nSqrtVal == num)
returntrue;
nSqrtVal++;
if(nSqrtVal*nSqrtVal == num)
returntrue;
returnfalse;
}
void FindTheNums(const unsigned intnMaxNum)
{
for(unsignedint n=0; n<=nMaxNum; n++)
{
if(IsSquare(n+100) && IsSquare(n+168))
printf("符合条件的数:%d\n",n);
}
}
//-----------------------------------------------------
//第二题
void CalcTotalRabbits(const unsigned intnMonths)
{
unsignedint nOneMonthRabbits = 1;
unsignedint nTowMonthsRabbits = 0;
unsignedint nAdultRabbits = 0;
for(intnMonth=2; nMonth<=nMonths; nMonth++)
{
nAdultRabbits+= nTowMonthsRabbits;
nTowMonthsRabbits= nOneMonthRabbits;
nOneMonthRabbits= nAdultRabbits;
printf("%d个月时兔子共有%d对\n",nMonth, nOneMonthRabbits+nTowMonthsRabbits+nAdultRabbits);
}
}
//-----------------------------------------------------
//第三题
// 1 2 3 4 5 6 7 8 9 10 11 12
const int daysArray[] = {31, 28, 31, 30,31, 30, 31, 31, 30, 31, 30, 31};
bool IsLeapYear(int nYear)
{
return(nYear%4==0&&nYear%100!=0||nYear%400==0);
}
int GetDaysOfMonth(int nYear, int nMonth)
{
if(nMonth==2 && IsLeapYear(nYear))
return29;
returndaysArray[nMonth-1];
}
int CalcDay(int nYear, int nMonth, intnDay)
{
intnRet = 0;
if(nMonth>=1 && nMonth<=12)
{
if(nDay <= GetDaysOfMonth(nYear, nMonth))
{
for(intm=1; m<nMonth; m++)
nRet+= GetDaysOfMonth(nYear, m);
nRet+= nDay;
printf("%d年%d月%d日是当年的第%d天\n",nYear, nMonth, nDay, nRet);
}
}
if(nRet==0)
printf("错误的日期参数,%d年%d月%d日:\n",nYear, nMonth, nDay);
returnnRet;
}
//主函数
int main(int argc, char* argv[])
{
printf("第一题:\n");
FindTheNums(100000);
printf("\n第二题:\n");
CalcTotalRabbits(8);
printf("\n第三题:\n");
CalcDay(2014,5, 18);
printf("\n\n");
system( "pause" ) ;
return0 ;
}
以下是执行结果:
第一题:
符合条件的数:156
第二题:
2个月时兔子共有1对
3个月时兔子共有2对
4个月时兔子共有3对
5个月时兔子共有5对
6个月时兔子共有8对
7个月时兔子共有13对
8个月时兔子共有21对
第三题:
2014年5月18日是当年的第138天