They might pay for them after all...
![:)](./images/smilies/icon_smile.gif)
Moderator: Board moderators
Code: Select all
//UVA-11340 Newspaper
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N;//number of tests
int K;//number of paid characters
long M;//number of lines of the article
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
map<unsigned char, double> charToValue;
map<unsigned char, double> :: iterator it;
scanf("%d", &K);
for(int j = 0; j < K; j++)
{
unsigned char ch;
int ch_value;
cin>>ch;
cin>>ch_value;
double temp = (ch_value * .01);
charToValue[ch] = temp;
}
scanf("%ld\n", &M);//taking number of lines of article
char str[M+1][10001];
for(long j = 0; j < M; j++)
{
gets(str[j]);
}
long double sum = 0;
for(long j = 0; j < M; j++)
{
//cout<<"j: "<<j<<" ";
//cout<<sum<<"$"<<endl;
int len = strlen(str[j]);
for(int k = 0; k < len; k++)
{
//it = charToValue.find(str[j][k]);
//if(it != charToValue.end())
if(charToValue[str[j][k]] != '\0')
{
sum += charToValue[str[j][k]];
}
else
{
sum += 0;
}
}
}
cout<<sum<<"$"<<endl;
charToValue.clear();
}
return 0;
}
Code: Select all
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
int calculateprice(string line, std::map<unsigned char, int> &P)
{
std::map<unsigned char, int>::iterator it;
int cost = 0.0;
for ( int i=0; i<line.size(); i++ )
{
it = P.find(static_cast<char>(line[i]));
if ( it != P.end())
{
cost = cost + it->second;
}
else
{
cost = cost + 0;
}
}
return cost;
}
int main()
{
//freopen("input.txt", "r", stdin);
int N, K, M;
cin >> N;
for( int i=0; i<N; i++ )
{
int total = 0;
std::map<unsigned char, int> price;
cin >> K;
unsigned char c; int p;
for ( int j = 0; j<K; j++)
{
cin >> c;
cin >> p;
price.insert(pair<unsigned char, int>(c, p));
}
cin >> M;
string line;
getline(cin, line);
for ( int k = 0; k<M; k++ )
{
getline(cin, line);
int res = calculateprice(line, price);
total = total + res;
}
cout << (double)(total/100.0) << "$" << endl;
price.clear();
}
return 0;
}
Code: Select all
#include <bits/stdc++.h>
using namespace std;
int liste[1000];
int main(){
int testcases,n,textlength,v,sum;
unsigned char c;
cin >> testcases;
while(testcases--){
for(int i=0;i<1000;i++)
liste[i]=0;
sum=0;
cin >> n;
for(int i=0;i<n;i++){
cin >> c >> v;
liste[c]=v;
}
cin >> textlength;
for(int i=-1;i<textlength;i++){
scanf("%c",&c);
while(c!=10){
sum+=liste[c];
scanf("%c",&c);
}
}
if(sum%100>=10)
cout << sum/100 << '.' << sum%100 << '$' << endl;
else
cout << sum/100 << ".0" << sum%100 << '$' << endl;
}
cout << endl;
return 0;
}