24h購物| | PChome| 登入
2013-08-11 16:40:23| 人氣1,458| 回應0 | 上一篇 | 下一篇

[UVA] 11816 - HST

推薦 0 收藏 0 轉貼0 訂閱站台

Problem D: HST

On July 1st, Ontario's Provincial Sales Tax (PST) was merged with the Federal Goods and Services Tax (GST), creating the Harmonized Sales Tax (HST). This changed the rate of tax Ontarians pay on various items.

Dalton would like to calculate how this change will affect his personal monthly budget.

Input Specification

The first line of input contains a single integer, the number of test cases to follow. Each test case begins with a line containing two integers N, M, the number of categories of items and the number of purchases Dalton makes each month, respectively. Each of these integers is between 1 and 100000, inclusive. N lines follow, each describing a category of item. Each of these lines contains a category name, which is a string of at most 30 uppercase letters, followed by three percentages, giving the PST, GST, and HST rate on that category. Each percentage is a number with up to two digits after the decimal point, and each percentage is followed by the % symbol. Each percentage is at least 0% and at most 100%. These lines are followed by M more lines, each describing one of Dalton's purchases. Each of these lines contains a category name and a price in dollars and cents and prefixed by the $ symbol. Each such line indicates the amount of pre-tax money that Dalton spends on an item in the specified category. The amount of each tax to be paid on an item is rounded to the nearest cent; if the tax amount is exactly half a cent more than a whole number of cents, it is rounded up to the nearest greater whole number of cents.

Sample Input

1
1 1
gas 0% 5% 13%
gas $100.00

Output Specification

For each test case, output the difference, in dollars and cents, between the total HST payable and the total sum of PST and GST payable on Dalton's monthly purchases. If the HST is more than the PST+GST, output a positive amount. If the HST is less than the PST+GST, output a negative amount.

Output for Sample Input

8.00

題目描述:

由於將兩種稅收(PST, GST) 合併成一個新的 HST 稅收制度,給定這三種稅收格式,
問新的制度與舊的制度的稅收差值。

題目解法:

每個稅率百分比最多 2 位,為了防止小數點誤差,考慮將所有數字儲存成整數。
而每筆花費也是最多 2 位,對於 0 位 1 位要特別處理。

而計算單筆消費時採用四捨五入計算稅收。

#include <stdio.h>
#include <string.h>
#include <map>
#include <iostream>
using namespace std;
struct E {
int PST, GST, HST;
E(int a, int b, int c):
PST(a), GST(b), HST(c) {}
E():
PST(0), GST(0), HST(0) {}
};
int readPercent() {
//most two digit after '.', try a.bb => a*100+bb
char s[100], s2[100];
scanf("%s", s);
int a, b;
if(sscanf(s, "%d.%s", &a, &s2) == 2) {
sscanf(s2, "%d", &b);
if(strlen(s2) == 2)
a = a*100 + b*10;
else
a = a*100 + b;
} else {
sscanf(s, "%d", &a);
a = a*100;
}
return a;
}
long long readDollar() {
//most two digit after '.', try a.bb => a*100+bb
char s[100], s2[100];
scanf("%s", s);
long long a, b;
if(sscanf(s, "$%lld.%s", &a, &s2) == 2) {
sscanf(s2, "%lld", &b);
if(strlen(s2) == 1)
a = a*100 + b*10;
else
a = a*100 + b;
} else {
sscanf(s, "$%lld", &a);
a = a*100;
}
return a;
}
long long calcTax(long long n, long long p) {
long long tax = n*p;//n/100 * p/100 round up
return tax/10000 + (tax%10000 >= 5000);
}
int main() {
int testcase;
int n, m;
int i, j, k;
char name[105], s[105];
scanf("%d", &testcase);
while(testcase--) {
scanf("%d %d", &n, &m);
map<string, E> R;
int pst, gst, hst;
for(i = 0; i < n; i++) {
scanf("%s", name);
pst = readPercent();
gst = readPercent();
hst = readPercent();
R[name] = E(pst, gst, hst);
}
long long ret = 0;
for(i = 0; i < m; i++) {
scanf("%s", name);
long long val = readDollar();
ret -= calcTax(val, R[name].PST);
ret -= calcTax(val, R[name].GST);
ret += calcTax(val, R[name].HST);
}
if(ret < 0) printf("-"), ret = -ret;
printf("%lld.%02lld\n", ret/100, ret%100);
}
return 0;
}

台長: Morris
人氣(1,458) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][dp] 10218 - Let's Dance !!!
此分類上一篇:[UVA] 613 - Numbers That Count

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文