Re: 10226 - Hardwood Species submission error?
Posted: Wed Feb 12, 2014 5:41 am
@brianfry No. I did not get AC for this code. It shows WA.Can please you look into the code and tell if you find anything wrong?
Looks like you finally got an AC after all. Well done!No. I did not get AC for this code. It shows WA.Can please you look into the code and tell if you find anything wrong?
Code: Select all
1
Ash
Code: Select all
Ash 100.0
Code: Select all
Ash 100.0000
Code: Select all
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Stack;
import java.util.TreeMap;
public class HardwoodSpecies {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int w=0;
int e=0;
String s;
s=bf.readLine();
w=Integer.parseInt(s);
String tu;
while(e<w) {
tu=bf.readLine();
Map<String,Integer> d = new TreeMap<String,Integer>();
StringBuilder sb =new StringBuilder();
String line;
while ((line=bf.readLine())!=null && !line.trim().equals("")){
if (d.get(line)==null){
d.put(line, 1);
}
else {
d.put(line, d.get(line)+1);
}
}
Collection<Integer> lista;
Collection <String> lista1;
lista=d.values();
lista1=d.keySet();
Integer[] lista2= lista.toArray(new Integer[d.size()]);
String [] lista3= lista1.toArray(new String[d.size()]);
int c=0;
int i=0;
while (i<lista3.length){
c=c+lista2[i];
i++;
}
i=0;
int y=0;
while(i<lista3.length){
float z=(d.get(lista3[i])/(float)c)*100 ;
String u;
u=Float.toString(z);
y=u.indexOf(".");
if (y==-1){
u=u.substring(0,y+5);
}
sb.append(lista3[i]+" " + u +"\n");
i++;
}
System.out.println(sb);
d.clear();
e++;
}
}
}
As a new poster here's some points you may want to keep in mind:Mrsuit wrote:Anyone help me pls, i'm getting runtime error. Btw, i know my code isn't right, i mean, only one point of it
Code: Select all
System.out.printf ("%.2f", 4.2312414);
Code: Select all
4.23
First, sorry for answering late, i've been working on others problems too.v1n1t wrote:As a new poster here's some points you may want to keep in mind:Mrsuit wrote:Anyone help me pls, i'm getting runtime error. Btw, i know my code isn't right, i mean, only one point of it
(1) Spend some time reading the thread you're posting to. This is a place that people have come to when they've had issues with this problem. So, there's a good chance reading the thread carefully will help you out big time.
(2) Look at the UVa Online Judge submission specification page
http://uva.onlinejudge.org/index.php?op ... &Itemid=30
What does it say about what the class name should be?
On the code front:
(1) Use double instead of float.
(2) Try running your code on the test input provided on this thread. Your code does not produce the correct output (regardless of the decimal precision).
(3) You can use printf in Java. So, for example you can do something like this to output 2 digits of decimal precisionand this will outputCode: Select all
System.out.printf ("%.2f", 4.2312414);
Code: Select all
4.23
Code: Select all
#include <stdio.h>
#include <map>
#include <string>
using namespace std;
int main(){
int TC,tot;
double p;
string name;
map<string,int> m;
scanf("%d\n\n",&TC);
while(TC--){
tot=0;
while(scanf("%30s",name.c_str())==1){
m[name]++;
tot++;
}
for(map<string,int>::iterator it=m.begin();it!=m.end();it++){
p=(100*(double)(it->second))/tot;
printf("%s %0.4lf\n",(it->first).c_str(),p);
}
}
return 0;
}
Code: Select all
1
hello
hello2
hello
hello3
helloWorld
helloSpain
Code: Select all
helloSpain 100.0000
Your post reminded me of something I know as "the 100% rule". I'm not sure where I picked it up (maybe a book, or a friend, or a teacher...) but I know it's something that has helped me a lot since I started applying it, many years ago (I remember being a teenager at the time, so a long time ago).apcastelein wrote:I'd like to solve it using STL map but I'm having some issues.
Code: Select all
#include <stdio.h>
#include <map>
#include <string>
using namespace std;
int main(){
int TC,tot;
double p;
string name;
char* str;
map<string,int> m;
scanf("%d",&TC);
while(TC--){
tot=0;
while(scanf("%30s",str)==1){
name=str;
m[name]++;
tot++;
}
for(map<string,int>::iterator it=m.begin();it!=m.end();it++){
p=(100*(double)(it->second))/tot;
printf("%s %0.4lf\n",(it->first).c_str(),p);
}
}
return 0;
}
Okay, let's go step by step..apcastelein wrote:I've modified my code. I haven't figured out how to address all the issues you mentioned but here it is
It's not that it shouldn't be used, it's that it has a specific behaviour that programmers should be aware of, but there are certain situations where it can be useful (for example, when reading a single character after any number of whitespace).apcastelein wrote: I understand now why \n or spaces shouldn't really be used at the start or end of scanf statements since it ignores whitespace.
The first thing you mention is true. Sometimes I/O is the bottleneck, so programmers should at least have some idea about this and what to do in those cases. A few alternatives are:apcastelein wrote:I've read that it can be necessary to use scanf on problems with longer input as opposed to other io functions in order to avoid getting a TLE verdict. If scanf can't be used on problems where whitespace is a key factor how are problems handled when they have long input and whitespace?
The right tool for the right job, my friend. In general, using gets() is certainly dangerous and probably a very bad idea. However, when you're writing programs for algorithmic problems to be tested in online judges, the situation is different. In any case, if the documentation you read said it's deprecated, it probably mentions the preferred alternative, which is fgets().apcastelein wrote: I read in the documentation that gets() is depreciated so I prefer not to use that. What are other alternatives?
The idea is good, but the code you wrote not so muchapcastelein wrote: In order to fix the c_str issue I've read the variable in as a c-string variable and then converted it afterward so I use it as a c++-string. Is this a valid solution?
Code: Select all
char *a; // a char pointer, initially pointing nowhere
char b[31]; // an array of size 31, b is pointing to a chunk of 31 bytes
scanf("%30s", a); // not ok -- a is not pointing to any reserved memory space
scanf("%30s", b); // ok
Code: Select all
import java.util.Set;
import java.util.Scanner;
import java.util.Map;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.io.PrintWriter;
import java.util.HashMap;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Miles Stevenson
*/
class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task10226 solver = new Task10226();
solver.solve(1, in, out);
out.close();
}
}
class Task10226 {
int n;
Map<String, Integer> dataset = new HashMap<String, Integer>();
public void solve(int testNumber, Scanner in, PrintWriter out) {
n = Integer.parseInt(in.nextLine());
int count = 0;
String line;
while (count <= n) {
if (count > 0)
out.println();
double trees = 0;
while (in.hasNext() && !(line = in.nextLine()).isEmpty()) {
if (dataset.containsKey(line)) {
Integer val = dataset.get(line);
dataset.put(line, ++val);
}
else
dataset.put(line, 1);
++trees;
}
int i = 0;
String[] values = new String[dataset.size()];
for (Map.Entry<String, Integer> entry : dataset.entrySet()) {
values[i] = entry.getKey();
++i;
/*String key = entry.getKey();
Integer value = entry.getValue();
DecimalFormat df = new DecimalFormat("#.####");
out.println(key + " " + df.format(value/trees * 100));*/
}
Arrays.sort(values);
for (int j = 0; j < values.length; ++j)
out.printf("%s %.4f\n", values[j], dataset.get(values[j])/trees * 100);
++count;
dataset.clear();
}
}
}