TopCoder算法竞赛题5:SRM 149 DIV 2, 250-point
2010-02-03 12:42
387 查看
Problem Statement
In documents, it is frequently necessary to write monetary amounts in a standard format. We have decided to format amounts as follows:
the amount must start with '$'
the amount should have a leading '0' if and only if it is less then 1 dollar.
the amount must end with a decimal point and exactly 2 following digits.
the digits to the left of the decimal point must be separated into groups of three by commas (a group of one or two digits may appear on the left).
Create a class FormatAmt that contains a method amount that takes two int's, dollars and cents, as inputs and returns the properly formatted string.
Definition
Class: FormatAmt
Method: amount
Parameters: int, int
Returns: string
Method signature: string amount(int dollars, int cents)
(be sure your method is public)
Notes
One dollar is equal to 100 cents.
Constraints
dollars will be between 0 and 2,000,000,000 inclusive
cents will be between 0 and 99 inclusive
Examples
0)
123456
0
Returns: "$123,456.00"
Note that there is no space between the $ and the first digit.
1)
49734321
9
Returns: "$49,734,321.09"
2)
0
99
Returns: "$0.99"
Note the leading 0.
3)
249
30
Returns: "$249.30"
4)
1000
1
Returns: "$1,000.01"
Source code
#include<iostream>
#include<vector>
//#include<list>
//#include<map>
//#include<deque>
//#include<stack>
//#include<bitset>
//#include<algorithm>
//#include<functional>
//#include<numeric>
//#include<utility>
#include<sstream>
//#include<iomanip>
//#include<cstdio>
//#include<cmath>
//#include<cstdlib>
//#include<ctime>
using namespace std;
class FormatAmt
{
public:
string amount(int, int);
};
string FormatAmt::amount(int dollars, int cents)
{
string str = "";
int n = 0;
int a[100];
while(dollars > 0)
{
a[n++] = dollars%10;
dollars /= 10;
}
int count = 0;
for(int i=0; i<n; i++)
{
str = char(a[i]+48) + str;
count++;
if(count % 3 == 0)
str = "," + str;
}
if(str.length() == 0) str = "0";
if(str[0] == ',') str.erase(0, 1);
str = '$' + str + '.';
memset(a, 0, sizeof(a));
n = 0;
while(cents > 0)
{
a[n++] = cents % 10;
cents /= 10;
}
for(i=1; i>=0; i--)
{
str += char(a[i] + 48);
}
return str;
}
int main(void)
{
FormatAmt amt;
cout<<amt.amount(123456,0)<<endl;
cout<<amt.amount(49734321,9)<<endl;
cout<<amt.amount(0,99)<<endl;
cout<<amt.amount(100,1)<<endl;
return 0;
}
In documents, it is frequently necessary to write monetary amounts in a standard format. We have decided to format amounts as follows:
the amount must start with '$'
the amount should have a leading '0' if and only if it is less then 1 dollar.
the amount must end with a decimal point and exactly 2 following digits.
the digits to the left of the decimal point must be separated into groups of three by commas (a group of one or two digits may appear on the left).
Create a class FormatAmt that contains a method amount that takes two int's, dollars and cents, as inputs and returns the properly formatted string.
Definition
Class: FormatAmt
Method: amount
Parameters: int, int
Returns: string
Method signature: string amount(int dollars, int cents)
(be sure your method is public)
Notes
One dollar is equal to 100 cents.
Constraints
dollars will be between 0 and 2,000,000,000 inclusive
cents will be between 0 and 99 inclusive
Examples
0)
123456
0
Returns: "$123,456.00"
Note that there is no space between the $ and the first digit.
1)
49734321
9
Returns: "$49,734,321.09"
2)
0
99
Returns: "$0.99"
Note the leading 0.
3)
249
30
Returns: "$249.30"
4)
1000
1
Returns: "$1,000.01"
Source code
#include<iostream>
#include<vector>
//#include<list>
//#include<map>
//#include<deque>
//#include<stack>
//#include<bitset>
//#include<algorithm>
//#include<functional>
//#include<numeric>
//#include<utility>
#include<sstream>
//#include<iomanip>
//#include<cstdio>
//#include<cmath>
//#include<cstdlib>
//#include<ctime>
using namespace std;
class FormatAmt
{
public:
string amount(int, int);
};
string FormatAmt::amount(int dollars, int cents)
{
string str = "";
int n = 0;
int a[100];
while(dollars > 0)
{
a[n++] = dollars%10;
dollars /= 10;
}
int count = 0;
for(int i=0; i<n; i++)
{
str = char(a[i]+48) + str;
count++;
if(count % 3 == 0)
str = "," + str;
}
if(str.length() == 0) str = "0";
if(str[0] == ',') str.erase(0, 1);
str = '$' + str + '.';
memset(a, 0, sizeof(a));
n = 0;
while(cents > 0)
{
a[n++] = cents % 10;
cents /= 10;
}
for(i=1; i>=0; i--)
{
str += char(a[i] + 48);
}
return str;
}
int main(void)
{
FormatAmt amt;
cout<<amt.amount(123456,0)<<endl;
cout<<amt.amount(49734321,9)<<endl;
cout<<amt.amount(0,99)<<endl;
cout<<amt.amount(100,1)<<endl;
return 0;
}
相关文章推荐
- TopCoder算法竞赛题2:SRM 146 DIV 2, 250-point
- TopCoder算法竞赛题3:SRM 147 DIV 2, 250-point
- TopCoder算法竞赛题4:SRM 148 DIV 2, 250-point
- TopCoder算法竞赛题6:SRM 150 DIV 2, 250-point
- TopCoder算法竞赛题1:SRM 144 DIV 2, 250-point
- SRM 667 DIV 2 PointDistance 250-point
- SRM 669 DIV 2 LiveConcert 250-point
- SRM 670 DIV 2 Cdgame 250-point
- TopCoder 250 points 11-SRM 149 DIV 1 86.62/250 34.65%
- 小朋友学TopCoder(7):SRM726 DIV2 250-point
- TopCoder 250 points 12-SRM 149 DIV 2 103.92/250 41.57%
- SRM 399 DIV2 [250]
- SRM 668 DIV 2 AnArray 1000-point
- Topcoder SRM 661 (Div.1) 250 MissingLCM - 数论
- topcoder SRM 541 div2 250
- TC SRM 593 DIV1 250(dfs)
- TC SRM 655 DIV1 250,500pt
- Topcoder Arena SRM 144 DIV2 1100 point 图遍历(非递归)+动态规划
- SRM149 - SRM150(少SRM150-DIV1-LV3)
- TopCoder 250 points 4-SRM 145 DIV 2 128.72/250 51.49%