119 - Greedy Gift Givers
Moderator: Board moderators
119 WA...Help
Code: Select all
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int find_per(char *bas[], char *nam, int ran){
for(int i = 1; i <= ran; i++){
if(strcmp(bas[i], nam) == 0)
return i;
}
return 0;
}
int main(){
int num_per, rec[11], giv[11],
mon_hav, num_rec, mon_giv,
nth_per,
i, j;
char *nam[11], *per = new(char);
while(scanf("%d", &num_per) == 1){
memset(rec, 0, sizeof(rec));
memset(giv, 0, sizeof(giv));
for(i = 1; i <= num_per; i++){
nam[i] = new(char);
scanf("%s", nam[i]);
}
for(i = 1; i <= num_per; i++){
mon_giv = 0;
scanf("%s %d %d", per, &mon_hav, &num_rec);
nth_per = find_per(nam, per, num_per);
if(num_rec != 0){
mon_giv = mon_hav / num_rec;
giv[nth_per] = mon_giv * num_rec;
}
for(j = 1; j <= num_rec; j++){
scanf("%s", per);
rec[find_per(nam, per, num_per)] += mon_giv;
}
}
for(i = 1; i <= num_per; i++)
printf("%s %d\n", nam[i], rec[i] - giv[i]);
printf("\n");
}
return 0;
}
my code???
i think ur problem is that when (mon_hav/num_rec) yields a remainder, u loose that money, which should be added to rec[nth_per].
in the first example, dave gives 200 to 3 people. that means 198 is given and 2 remains to him. after getting 300 from others, he has in the end 302.
in the first example, dave gives 200 to 3 people. that means 198 is given and 2 remains to him. after getting 300 from others, he has in the end 302.
Understanding a problem in a natural way will lead to a natural solution
119 - Run Time Error
Code: Select all
#include<iostream>
using namespace std;
short n;
struct gift
{char cad[13];
int spend;
int rec;
gift *next;
};
char c[13];
int i,d,g,t,p,k;
gift *cab,*grupo,*aux;
int main()
{while(cin>>n)
{cab=new gift;
cin>>cab->cad;
grupo=cab;
grupo->rec=0;
for(i=1;i<n;i++)
{aux=new gift;
cin>>aux->cad;
aux->rec=0;
grupo->next=aux;
grupo=grupo->next;
}
grupo=cab;
for(i=0;i<n;i++)
{cin>>c>>d>>g;
grupo=cab;
for(;;)
{k=0;
while(c[k]!='\0'&&c[k]==grupo->cad[k])
k++;
if(c[k]==grupo->cad[k])
break;
grupo=grupo->next;
}
grupo->spend=d;
t=(d/g);
for(p=0;p<g;p++)
{cin>>c;
grupo=cab;
for(;;)
{k=0;
while(c[k]!='\0'&&c[k]==grupo->cad[k])
k++;
if(c[k]==grupo->cad[k])
break;
grupo=grupo->next;
}
grupo->rec+=t;
}
}
grupo=cab;
p=0;
do
{
i=0;
while(grupo->cad[i]!='\0')
{cout<<grupo->cad[i];
i++;
}
cout<<" "<<grupo->spend-grupo->rec<<endl;
grupo=grupo->next;
p++;
}while(p<n);
}
return 0;
}
WA119 help
i have tested my solution with every test case i could find on this forum but i still got WA. help!
Code: Select all
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
int main(void)
{
while( !cin.eof() )
{
int n;
vector<string> names;
map< string, int > give, got;
cin >> n;
names.resize(n);
for( int i = 0; i < n; ++i )
{
cin >> names[i];
give[names[i]] = got[names[i]] = 0;
}
for ( int i = 0; i < n; ++i )
{
string ime, tokom;
int st, to;
cin >> ime >> st >> to;
for( int j = to; j; --j )
{
cin >> tokom;
got[tokom] += st / to;
give[ime] += st / to;
}
}
for( int i = 0; i < n; ++i )
cout << names[i] << " " << got[names[i]] - give[names[i]] << endl;
if( !cin.eof() )
cout << endl;
}
return 0;
}
119 Compile error, using Java
Hello,
I would be frustrated if I got a WA, but a compile error is more maddening somehow, as of course it compiles and runs just fine "on my computer"
I even think I have the right answer 
I have not submitted in a while and am still pretty new to all this. I do have an inner class, but thought that was allowed. I am using Java. The other thing is that the input is terminated by "end of file" and I'm using "while((input = readLine(255)) !=null)" shown in the java example.
Anyway, here's my code, any help would be greatly appreciated, thanks!
Lisa
********pasting code below**********
I would be frustrated if I got a WA, but a compile error is more maddening somehow, as of course it compiles and runs just fine "on my computer"


I have not submitted in a while and am still pretty new to all this. I do have an inner class, but thought that was allowed. I am using Java. The other thing is that the input is terminated by "end of file" and I'm using "while((input = readLine(255)) !=null)" shown in the java example.
Anyway, here's my code, any help would be greatly appreciated, thanks!
Lisa
********pasting code below**********
Code: Select all
/* begin_of_source_code */
/* @JUDGE_ID: 38154WT 119 Java */
import java.io.*;
import java.util.*;
class Main
{
public static void main(String[] args) throws IOException
{
class Person
{
private String per_strName;
private int per_iMoney;
public Person()
{
per_strName = "yo";
per_iMoney = 0;
}
public Person(String strName, int iMoney)
{
per_strName = strName;
per_iMoney = iMoney;
}
public String getName()
{
return per_strName;
}
public void setName(String strName)
{
per_strName = strName;
}
public int getMoney()
{
return per_iMoney;
}
public void setMoney(int iMoney)
{
per_iMoney = iMoney;
}
public void addMoney(int iMoney)
{
per_iMoney = per_iMoney + iMoney;
}
public void subtractMoney(int iMoney)
{
per_iMoney = per_iMoney - iMoney;
}
}//end class person
String input;
String strName;
String giver;
String luckyone;
int money = 0;
int sharedPerPerson = 0;
int retained = 0;
int countpresents = 0;
int iHowMany = 0;
int step = 1;
int count = 0;
StringTokenizer getter;
//boolean bFirstRun = true;
boolean bDone = false;
Person[] allPeople = new Person[50];;
while((input = readLine(255)) !=null)
{
getter = new StringTokenizer(input);
if (step == 1)
{
iHowMany = Integer.parseInt(getter.nextToken());
}
if (step == 2)
{
//System.out.println("getting all the names");
for (int i = 0; i < iHowMany; i++)
{
strName = getter.nextToken();
Person x = new Person(strName, 0);
allPeople[i] = x;
}
}
if (step == 3)
{
//for each person, read new line and parse
for (int i = 0; i <= iHowMany; i++)
{
getter = new StringTokenizer(input);
count = getter.countTokens();
giver = getter.nextToken();
money = Integer.parseInt(getter.nextToken());
; countpresents = Integer.parseInt(getter.nextToken());
if ((money != 0) && (countpresents != 0))
{
sharedPerPerson = money/countpresents;
retained = money%countpresents;
}
else
{
sharedPerPerson = 0;
retained = 0;
}
//for each name who is getting something
for (int j = 0; j < countpresents; j++)
{
luckyone = getter.nextToken();
String output = luckyone + " gets " + sharedPerPerson;
output += " by " + giver;
//iterate the array and check if have right person
for (int index = 0; index < iHowMany; index++)
{
if (allPeople[index].getName().equals(giver))
{
allPeople[index].subtractMoney(sharedPerPerson);
//System.out.println(allPeople[index].getName() + " now has $" + allPeople[index].getMoney());
}
if (allPeople[index].getName().equals(luckyone))
{
allPeople[index].addMoney(sharedPerPerson);
//System.out.println(allPeople[index].getName() + " now has $" + allPeople[index].getMoney());
}
}//iterating array
}//end for each name getting something loop
if (i < (iHowMany - 1))//read new line EXCEPT last time through
{
input = readLine(255);
}
}//end for each person total loop
//Output
for (int a = 0; a < iHowMany; a++)
{
String fun = allPeople[a].getName() + " ";
fun += allPeople[a].getMoney();
System.out.println(fun);
}
System.out.println();
//reset everything
strName = "";
giver = "";;
luckyone = "";;
money = 0;
sharedPerPerson = 0;
retained = 0;
countpresents = 0;
iHowMany = 0;
count = 0;
allPeople = new Person[50];
}//end if step 3 statemet
if (step == 3)
{
step = 1;
}
else
{
step++;
}
}
}//end main()
static String readLine(int maxLg) throws IOException {
byte[] lin = new byte[maxLg];
int lg = 0, car = -1;
while(lg < maxLg) {
car = System.in.read();
if(car<0 || car=='\n') break;
lin[lg++] += car;
}
if(car<0 && lg==0) return null;
return new String(lin, 0, lg);
}//end read line
}//end class
/* end_of_source_code */
-
- Learning poster
- Posts: 98
- Joined: Sat Jan 21, 2006 12:45 pm
- Location: Busan,Corea(Republic of)
119 Why WA??
#include<iostream.h>
#include<string.h>
int n,i,j;
int h[1000]={0,},q[1000]={0,};
int z,x,c,v,b;
char a[1000][1000],s[1000],d[1000];
int main(){
while(cin >> n){
for(i=0;i<1000;i++){
for(j=0;j<1000;j++)
a[j]=0;
s=0;d=0;q=0;h=0;
}
v=0;
b=0;
for(i=0;i<n;i++)
cin >> a;
for(i=0;i<n;i++){
cin >> s;
cin >> z >> x;
for(j=0;j<n;j++)
if(strcmp(a[j],s) == 0){
h[j] = z;
if (x != 0) q[j] += z%x;
}
for(j=0;j<x;j++){
cin >> d;
for(c=0;c<n;c++)
if(strcmp(a[c],d)==0)
q[c] += z/x;
}
}
for(i=0;i<n;i++)
cout << a << " " << q-h << endl;
cout << endl;
for(i=0;i<1000;i++){
for(j=0;j<1000;j++)
a[j]=0;
h[i]=s[i]=d[i]=q[i]=0;
}
}
return 0;
}
#include<string.h>
int n,i,j;
int h[1000]={0,},q[1000]={0,};
int z,x,c,v,b;
char a[1000][1000],s[1000],d[1000];
int main(){
while(cin >> n){
for(i=0;i<1000;i++){
for(j=0;j<1000;j++)
a[j]=0;
s=0;d=0;q=0;h=0;
}
v=0;
b=0;
for(i=0;i<n;i++)
cin >> a;
for(i=0;i<n;i++){
cin >> s;
cin >> z >> x;
for(j=0;j<n;j++)
if(strcmp(a[j],s) == 0){
h[j] = z;
if (x != 0) q[j] += z%x;
}
for(j=0;j<x;j++){
cin >> d;
for(c=0;c<n;c++)
if(strcmp(a[c],d)==0)
q[c] += z/x;
}
}
for(i=0;i<n;i++)
cout << a << " " << q-h << endl;
cout << endl;
for(i=0;i<1000;i++){
for(j=0;j<1000;j++)
a[j]=0;
h[i]=s[i]=d[i]=q[i]=0;
}
}
return 0;
}
Archaan
CAN YOU BEAT ME?
http://acm.uva.es/problemset/usersjudge.php?user=19788
AND,
http://acm.uva.es/problemset/submit.php
http://online-judge.uva.es/problemset/submit.php
SUBMIT AND GET AC!!!
CAN YOU BEAT ME?
http://acm.uva.es/problemset/usersjudge.php?user=19788
AND,
http://acm.uva.es/problemset/submit.php
http://online-judge.uva.es/problemset/submit.php
SUBMIT AND GET AC!!!
-
- New poster
- Posts: 8
- Joined: Tue May 16, 2006 9:31 am
- Location: Las Vegas, NV. USA.
- Contact:
119 special case
How about a special case:
There is a post on this forum discussing the following input/output:
Input:
2
a 10 0
b 10 0
I'm about to submit my code, and according to it, it will print a -10 and b-9.
Why?
Simple:
a spends 10 and b spends 9. They both received 0, so they lost 10 and 9 recpectively. It makes perfect sense to me according to the problem statement, but apparently it does not make sense for some people. What is even wose, somebody said that if they output 0 for both a and b they got ACC.
Any comments on this....??
I'll submit my code and I will tell you what I get.
c ya.
There is a post on this forum discussing the following input/output:
Input:
2
a 10 0
b 10 0
I'm about to submit my code, and according to it, it will print a -10 and b-9.
Why?
Simple:
a spends 10 and b spends 9. They both received 0, so they lost 10 and 9 recpectively. It makes perfect sense to me according to the problem statement, but apparently it does not make sense for some people. What is even wose, somebody said that if they output 0 for both a and b they got ACC.
Any comments on this....??
I'll submit my code and I will tell you what I get.
c ya.
The harder you work to find the solution, the more you learn in the process.
-
- New poster
- Posts: 8
- Joined: Tue May 16, 2006 9:31 am
- Location: Las Vegas, NV. USA.
- Contact:
???
Ok I got WA...
What the h.....???
Any body??
What the h.....???
Any body??
The harder you work to find the solution, the more you learn in the process.
-
- New poster
- Posts: 8
- Joined: Tue May 16, 2006 9:31 am
- Location: Las Vegas, NV. USA.
- Contact:
PE
Ok I got it but I get PE...
Anything wrong on my output??
Here is output code:
Anything wrong on my output??
Here is output code:
Code: Select all
for(i=0; i<n; i++)
{
sum=0;
for(j=0; j<n; j++)
{
sum+=matrix[j][i];
if(matrix[j][i]!=0)
{
zerocounter++;
}
}
cout<<p[i].name<<" ";
if(zerocounter==0)
{
cout<<0;
}
else
{
cout<<sum-capital[i];
}
cout<<endl;
}
The harder you work to find the solution, the more you learn in the process.
-
- Learning poster
- Posts: 54
- Joined: Mon Jan 02, 2006 3:06 am
- Location: Dhaka,Bangladesh
- Contact: