POJ1004 Financial Management【保留小数】

题目:

Description

Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The first step is to figure out what's been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance.

Input

The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular month. Each number will be positive and displayed to the penny. No dollar sign will be included.

Output

The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters in the output.

Sample Input

100.00
489.12
12454.12
1234.10
823.05
109.20
5.27
1542.25
839.18
83.99
1295.01
1.75

Sample Output

$1581.42

思路:

这题非常简单,就是求平均数。主要难点在于如何保留小数,本C++菜鸟很不熟练,专门去学习了一下。

代码:

#include<iostream>

using namespace std;

int main(){
	double total=0;
	double n;
	for(int i=0;i<12;i++){
		cin>>n;
		total+=n;
	}
	cout.setf(ios::showpoint);
	cout.precision(2);
	cout.setf(ios::fixed);
    cout<<"$"<<(total/12)<<endl;
	return 0;
}


看了一下其他人怎么做的,大家保留小数的方法都不太一样,最终都能AC。

一位:

#include<iostream>  
#include<iomanip>  
using namespace std;  
  
int main(void)  
{  
    double sum=0.0;  
    for(int i=1;i<=12;i++)  
    {  
        double temp;  
        cin>>temp;  
        sum+=temp;  
    }  
    cout<<fixed<<setprecision(2)<<'$'<<sum/12.0<<endl;  
    return 0;  
}  
注意哦,使用setprecision(...)这个函数,需要引用#include<iomanip>

另一位:

#include <cstdio>  
using namespace std;  
int main(){  
    double result=0,t=0;  
    for(int i=0;i<12;i++){  
        scanf("%lf",&t);  
        result+=t;  
    }  
    printf("$%.2lf\n",result/12);  
    return 0;  
}   

我试了一下,如果引用了#include<iostream>的话,那么这里的#include<cstdio>没有也没事。但是如果没有#include<iostream>的话,就必须有#include<cstdio>。

另外,因为这里是double,所以是%.2lf,如果是float的话,就是%.2f了。


这里整理了一些关于C++保留小数的知识:

c++保留小数是“四舍六入五成双”。

1. 被修约的数字小于5时,该数字舍去;
2. 被修约的数字大于5时,则进位;
3. 被修约的数字等于5时,要看5前面的数字,若是奇数则进位,若是偶数则将5舍掉,即修约后末尾数字都成为偶数;若5的后面还有不为“0”的任何数,则此时无论5的前面是奇数还是偶数,均应进位。

例子:

9.8249=9.82, 9.82671=9.83
9.8350=9.84, 9.8351 =9.84
9.8250=9.82, 9.82501=9.83

保留小数:

1 使用printf。
C++完整兼容C语言,所以同样可以使用C的输出函数printf。
在输出精度控制上C的printf更为简单,但需要根据数据类型调整输出格式字符。
对于float可以用%.2f, 而double则需要用%.2lf。
以float为例,代码如下:

#include<cstdio>//printf所在头文件
using namespace std;
int main()
{
float v = 1.54321;
printf(“%.2f\n”, v);
return 0;
}

2.使用setprecision(n)可控制输出流显示浮点数的数字个数。C++默认的流输出数值有效位是6。可是这个是保留“有效数字”,而不是“小数”!

保留小数:setprecision(n)与setiosflags(ios::fixed)合用,可以控制小数点右边的数字个数。setiosflags(ios::fixed)是用定点方式表示实数。

例子:

double amount = 22.0/7;
cout <<amount <<endl;
cout <<setiosflags(ios::fixed);
cout <<setprecision(8) <<amount <<endl;

输出:

3.14286  //因为默认是保留6位有效数字

3.14285714 //这下子就成功保留8位小数啦~

这个方法要记得#include <iomanip.h> //用到格式控制符

3.下面来自于博客:http://blog.csdn.net/kid_u_forfun/article/details/8628732

关于 C++ 输出时的小数点后的位数是很基础的东西,应该掌握。

double  a  =  3.141592, b = 2213242.329843;
cout.precision(
6 );
cout
  <<  a << endl << b;

输出的是:

3.14159
221324

上面输出的是“6个有效数字”,而想要输出 3.141592 和 2213242.329843 的数字,不管小数点前面有几位,那就要:

double  a  =  3.141592 , b  =  2213242.329843 ;
cout.setf(
ios:: showpoint);  // 设置为始终输出小数点后的数字,就是说 a = 3,它也输出 3.00000 这样
cout.precision( 6 );
cout.setf(ios::
fixed );  // 设置为小数位始终有 6 位,没有这个的话就会像上面那个代码那样固定的不是小数点后面的数字了。
cout  <<  a  <<  endl  <<  b;

输出就是:

3.141592
2213242.329843


下面再介绍一个方法(也就是上面一位POJ解手的方法):

比如pi=3.14159265333,保留三位小数:

cout<<fixed<<setprecision(3);  
cout<<pi<<endl;

备注:这个方法必须加头文件iomanip。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值