不廢話... 看程式.
E1) # include <stdio.h>
# include <stdlib.h>
# include <math.h>
int main()
{
double x;
puts("I need a number.");
if (scanf("%lf", &x) < 1)
exit(1);
printf("The square root of %f is %f.\n", x, sqrt(x));
return 0;
}
E2) # include <stdio.h>
# include <stdlib.h>
double mysqrt(double);
int main()
{
double x;
puts("I need a number.");
if (scanf("%lf", &x) < 1)
exit(1);
printf("The square root of %f is %f.\n", x, mysqrt(x));
return 0;
}
double mysqrt(double x)
{
double l, u, t;
if (x < 0)
return -1;
for (l = 0, t = u = x / 2; u - l > 0.0005; t = (l + u) / 2)
if (t * t < x)
l = (l + t) / 2;
else
u = (u + t) / 2;
return t;
}
E3) # include <stdio.h>
# include <time.h>
# include <stdlib.h>
int rolldice(void);
int main()
{
int cnt[6] = {0, }, i;
srand(time(NULL));
for (i = 0; i < 6000; i++)
cnt[rolldice()]++;
for (i = 0; i < 6; i++)
printf("Number: %d, Count: %d, Probability: %f\n",
i + 1, cnt[i], cnt[i] / 6000.0);
return 0;
}
int rolldice()
{
return rand() % 6;
}
--
新詩練習:新鮮。踩破初春裡的狗大便;不經意的滄桑,滿溢著嫩黃的喜悅。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.224.163.185