首页 > 其他 > 详细

华中农业大学新生赛C题

时间:2016-12-19 08:39:39      阅读:160      评论:0      收藏:0      [点我收藏+]

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 }

 

华中农业大学新生赛C题

原文:http://www.cnblogs.com/ray-coding-in-rays/p/6196146.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!