Re: 11340 - Newspaper
Posted: Tue Nov 18, 2014 11:20 pm
Print a newline char at the end of a line.
Try:
printf("%.2f$\n",sum/100);
Try:
printf("%.2f$\n",sum/100);
Code: Select all
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.StringTokenizer;
class Main {
BufferedReader in;
PrintWriter out;
HashMap<Character, Integer> payments;
double total;
public static void main(String[] args) throws IOException {
new Main().run();
}
void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out, true);
solve();
out.flush();
}
void solve() throws IOException {
payments = new HashMap<Character, Integer>();
int N = Integer.parseInt(in.readLine());
for (int i = 0; i < N; i++) {
int K = Integer.parseInt(in.readLine());
for (int j = 0; j < K; j++) {
String line = in.readLine();
StringTokenizer str = new StringTokenizer(line, " ");
while (str.hasMoreTokens()) {
char[] key = str.nextToken().toCharArray();
payments.put(key[0], Integer.parseInt(str.nextToken()));
}
}
int M = Integer.parseInt(in.readLine());
for (int l = 0; l < M; l++) {
payment(in.readLine());
}
total /= 100;
out.printf("%.2f$\n", total);
}
}
double payment(String statement) {
for (int j = 0; j < statement.length(); j++)
if (payments.containsKey(statement.charAt(j)))
total += payments.get(statement.charAt(j));
return total;
}
}
The UVa Jugde return WA and I don't know why
Help me! please
Code: Select all
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <sstream>
#include <utility>
using namespace std;
long long int ch_counter[128] ;
int main()
{
long long int i,j,k,t_case,t_case2,ch_value,str_line,sum ;
//double result,sum ;
char str[10005] ,ch,str1[100] ;
string s;
cin >> t_case ;
for(i = 1 ; i <= t_case ; i++)
{
memset(ch_counter , 0 , sizeof(ch_counter)) ;
cin >> t_case2 ;
getchar() ;
for(j = 1 ; j <= t_case2 ; j++)
{
int flag = 0 ;
gets(str1) ;
int len = strlen(str1) ;
int index = 0 ;
for(k = 0 ; k < len ; k++)
{
if(str1[0] == ' ')
{
flag = 1 ;
break ;
}
if(str1[0] != ' ')
{
ch = str1[0] ;
}
if(str1[k] >= '0' && str1[k] <= '9')
{
s[index++] = str1[k] ;
}
}
if(flag == 0)
{
s[index++] = '\0' ;
ch_value = atoi(s.c_str()) ;
ch_counter[ch] = ch_value ;
}
}
cin >> str_line ;
getchar() ;
sum = 0 ;
for(j = 1 ; j <= str_line ; j++)
{
gets(str) ;
int len = strlen(str) ;
for(k = 0 ; k < len ; k++)
{
if(ch_counter[str[k]] > 0)
{
sum = sum + ch_counter[str[k]] ;
}
}
}
printf("%.2lf$\n",sum/100.0) ;
}
return 0;
}
Code: Select all
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <sstream>
#include <utility>
using namespace std;
long long int ch_counter[128] ;
int main()
{
long long int i,j,k,t_case,t_case2,ch_value,str_line,sum ;
//double result,sum ;
char str[10005] ,ch,str1[100] ;
string s;
cin >> t_case ;
for(i = 1 ; i <= t_case ; i++)
{
memset(ch_counter , 0 , sizeof(ch_counter)) ;
cin >> t_case2 ;
getchar() ;
for(j = 1 ; j <= t_case2 ; j++)
{
int flag = 0 ;
gets(str1) ;
int len = strlen(str1) ;
int index = 0 ;
for(k = 0 ; k < len ; k++)
{
if(str1[0] == ' ')
{
flag = 1 ;
break ;
}
if(str1[0] != ' ')
{
ch = str1[0] ;
}
if(str1[k] >= '0' && str1[k] <= '9')
{
s[index++] = str1[k] ;
}
}
if(flag == 0)
{
s[index++] = '\0' ;
ch_value = atoi(s.c_str()) ;
ch_counter[ch] = ch_value ;
}
}
cin >> str_line ;
getchar() ;
sum = 0 ;
for(j = 1 ; j <= str_line ; j++)
{
gets(str) ;
int len = strlen(str) ;
for(k = 0 ; k < len ; k++)
{
if(ch_counter[str[k]] > 0)
{
sum = sum + ch_counter[str[k]] ;
}
}
}
printf("%.2lf$\n",sum/100.0) ;
}
return 0;
}
rio wrote:Finally figured out the trick after 20 few submissions..
Character code has range 0 ~ 255, so use unsigned char.
(Is the spoiler ?)
faiem wrote:![]()
In this problem must use "unsigned char"...Or u will get WA...again and again just like me.
brianfry713 wrote:Don't assume that the characters in this problem have an ASCII value less than 128. Use an unsigned char.
I read so many posts in this thread saying to use unsigned char. But I got accepted using char and i didn't add 128 to make it positive. Here is my code. http://ideone.com/YMbgGqbrianfry713 wrote:Read this thread:http://acm.uva.es/board/viewtopic.php?t=24824
A signed 1 byte char always has range of -128 to 127.
For some reason the problem setter decided to use more than the standard range of 0 to 127 characters in the judge's input.
Code: Select all
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
long long n,i,j,a[257],p,t,k,z,l;
double s;
char b[100000];
char ch;
scanf("%lld",&t);
while(t>0)
{
t--;
for(i=0;i<=256;i++)
a[i]=0;
scanf("%lld",&n);
for(i=0;i<n;i++)
{
while(getchar()!='\n');
scanf("%c",&ch);
scanf("%lld",&p);
a[ch+128]=p;
}
scanf("%lld",&k);
s=0;
//printf("hui %d",k);
for(j=0;j<k;j++)
{
//printf("hui");
scanf("%*c%[^\n]",b);
z=strlen(b);
//printf("%d",z);
for(l=0;l<z;l++)
{
s=s+(double)a[b[l]+128];
}
}
s=s/100;
printf("%.2lf$\n",s);
}
return 0;
}
Code: Select all
scanf("%lld",&k);
while(getchar()!='\n');
for (s = j = 0; j < k; j++)
{
cin.getline(b, 100000);
for (l = 0; b[l]; l++) s += a[ b[l] + 128 ];
}
Code: Select all
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <iterator>
#include <map>
#include <set>
#define mx 1000000
#define D double
#define L long
#define LL long long
#define ULL unsigned long long
#define isc1(a) scanf("%d", &a)
#define isc2(a,b) scanf("%d%d", &a, &b);
#define isc3(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define llsc1(a) scanf("%I64d", &a)
#define llsc2(a,b) scanf("%I64d%I64d", &a, &b)
#define llsc3(a,b,c) scanf("%I64d %I64d %I64d", &a,&b,&c)
#define f(a,n) for(a=0; a<n; a++)
#define all(a) a.begin(), a.end()
#define ms(arr) memset(arr, 0, sizeof(arr))
#define cl(a) a.clear()
#define sz(a) a.size()
#define sc scanf
#define pf printf
#define pu push_back
#define pb pop_back
#define vc vector
using namespace std;
int main()
{
L a,b,d, x;
L c;
L tst, pnum, line, cval, len;
L val[300];
string str;
char ch;
D tot;
sc("%ld",&tst);
while(tst--)
{
ms(val);
sc("%ld",&pnum);
f(a, pnum)
{
cin>>ch>>cval;
d=ch;
val[d]=cval;
}
sc("%ld\n", &line); c=0;
for(a=0; a<line; a++)
{
getline(cin,str);
len=sz(str);
for(b=0; b<len; b++)
{
//if(val[str[b]])
c+=val[str[b]];
}
}
tot=(D)c/100.00;
pf("%0.2lf$\n", tot);
}
return 0;
}