华中农业大学新生赛C题

http://acm.hzau.edu.cn/problem.php?id=1099

题意:

输入两个整数 l 和 n,代表半径和output的保留小数点位数。

输出圆的面积,保留n位小数。

 

一开始觉得用抑制符就可以了,然后发现它保留的时候会四舍五入,所以不行。

知识主要用到了floor函数(返回不大于传参值的最大整数(double)类型)以及sprintf。

AC代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #define PI 3.1415926535
 5 int main(void)
 6 {
 7     double l;
 8     int n;
 9     while (scanf("%lf %d", &l, &n) != EOF)
10     {
11         if (!l) printf("0");
12         else if (!n) printf("%.lf", floor(PI*l*l));
13         else
14         {
15             char str[1000];
16             sprintf(str, "%.*lf", n + 2, PI*l*l);
17             printf("%.lf.", floor(PI*l*l));
18             for (int i = 0; ; i++)
19             {
20                 if (str[i] == '.') {
21                     for (int j = i + 1; j <= i + n; j++)
22                         printf("%c", str[j]);
23                     goto end;
24                 }
25             }
26         end:;
27         }
28         putchar('\n');
29     }
30     return 0;
31 }

 

posted @ 2016-12-19 01:19  codinRay  阅读(371)  评论(0编辑  收藏  举报