简单的猴子吃桃子反应一下,int double的最大数限制
/** ?
* ??
* ? 有一只猴子,第一天摘了若干个桃子 , ?
* ?当即吃了一半,但还觉得不过瘾 ,就又多吃了一个。 ?
* ?第2天早上又将剩下的桃子吃掉一半,还是觉得不过瘾,就又多吃了两个。 ?
* ?以后每天早上都吃了前一天剩下的一半加天数个 ?
* ?(例如,第5天吃了前一天剩下的一般加5个)。 ?
* ?到第n天早上再想吃的时候,就只剩下一个桃子了。 ?
* ?输入:天数n ?
* ?输出:第一天的桃子个数 ?
* ?
?*/?
?这个是一简单的猴子吃桃子
?可以学习一下,int double的最大数限制
?
?#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <math.h>
double numbers(int day)
{
? ?double result=1;
? ?int i;
? ?for(i=day-1;i>0;i--)
? ?{
? ? ? if((result+i)>(DBL_MAX/2))
? ? ? ? {
? ? ? ? return -50;
? ? ? ? __asm__("\n\t");
? ? ? ? }
? ? ? result=(result+i)*2;
? ?}
? ?return result;
}
double strtoint(char str[])
{
? ?double tint=0;
? ?int i,j;
? ?for(i=0;str[i]!='\0';i++)
? ? {
? ? ? ?if((str[i]<'0')||(str[i]>'9'))
? ? ? ?{
?return 0;
? ? ? ?}
? ? }
? ?for(j=0;j<i;j++)
? ?{
? ? ? tint=tint+((int)(str[j]-'0')*(pow(10,i-j-1)));
? ?}
? ?if(tint>INT_MAX)
? ? {
? ? return -50;
? ? }
? ?return tint;
}
int main(int argc,char *argv[])
{
? ?int day;
? ?double x;
? ?if(argc<2)
? ?{
? ? ? printf("Usage: [command] [days]\n");
? ? ? printf(" ? ? ? ? ? ? ? ? [days] The number of days the monkeys eat\n");
? ? ? exit(0);
? ?}
? ?day=(int)strtoint(argv[1]);
? ?if((day<1)||(day==-50))
? ?{
? ? ? printf("Parameters must be numeric (int)\n");
? ? ? printf("Must be greater than one day\n");
? ? ? exit(0);
? ?}
? ?printf("The monkey eat %d days \n",day);
? ?x=numbers(day);
? ?if(x==-50)
? ? ?{
? ? ?printf("The number of days you too much\n");
? ? ?printf("For example,1014 max day,Can only save so much \nBecause of number (double)\n");
? ? ?exit(0);
? ? ?}
? ?printf("Need a total of %.0f peaches\n",x);
? ?return 0;
}