UVa 10137 The Trip

UVa 10137 The Trip
通过这道题目学会了如何截去浮点数的部分小数位,可以通过如下代码实现:
sprintf(tmp, " %.mlf " ,n);
sscanf(tmp,
" %lf " , & n);
其中tmp为一个字符数组,n为一个浮点数,m为要保留的小数位。
以下是我的代码:
#include < stdio.h >
int  main()
{
    
/*
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    //
*/
    
const   long  maxn = 1007 ;
    
long  n;
    
while (scanf( " %ld " , & n) == 1 )
    {
       
if (n == 0 break ;
       
double  a[maxn],ave = 0 ,ans1 = 0 ,ans2 = 0 ;
       
char  tmp[maxn];
       
for ( long  i = 1 ;i <= n;i ++ )
       {
          scanf(
" %lf " , & a[i]);
          ave
+= a[i];
       }
       ave
/= n;
       sprintf(tmp,
" %.2lf " ,ave);
       sscanf(tmp,
" %lf " , & ave);
       
for ( long  i = 1 ;i <= n;i ++ )
         
if (a[i] < ave)
           ans1
+= (ave - a[i]);
         
else  ans2 += (a[i] - ave);
       
if (ans1 < ans2) printf( " $%.2lf\n " ,ans1);
       
else  printf( " $%.2lf\n " ,ans2);
    }
return   0 ;
}

你可能感兴趣的:(UVa 10137 The Trip)