ray-coding-in-rays

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 }

 

分类:

技术点:

相关文章:

  • 2021-10-09
  • 2021-11-25
  • 2021-05-31
  • 2021-10-09
  • 2017-11-27
  • 2021-10-09
  • 2021-11-15
  • 2021-12-13
猜你喜欢
  • 2021-10-09
  • 2021-10-09
  • 2021-10-09
  • 2021-10-09
  • 2021-10-29
  • 2021-10-09
  • 2018-04-10
相关资源
相似解决方案