187 - Transaction Processing
Moderator: Board moderators
187 - Transaction Processing
Can anybody help me why I always got Runtime Error(signal 11 (SIGSEGV) Invalid memory reference)?
![:cry:](./images/smilies/icon_cry.gif)
Code: Select all
#include<iostream.h>
#include<string.h>
typedef struct /* structure for transaction */
{
char accountnum[3]; /* transaction account */
float money; /* transaction charge */
}transac;
typedef struct /* structure for account data */
{
char num[3]; /* account number */
char name[30]; /* account name (the last element store the name length) */
short len;
}account;
char cursor;
account data[101];
char snum[3],prenum[3];
transac transaction[11];
float result,temp;
short index,i,j,k,len;
void main(void)
{
index = 0;
while(1) /* process the account data */
{
for (i=0;i<3;i++) /* read in account number */
{
cin>>data[index].num[i];
}
if ((data[index].num[0] == '0')&&(data[index].num[1] == '0')&&(data[index].num[2] == '0')) /* end of account number */
break;
cin.get(cursor); /* get the account name */
for (i=0;i<30;i++)
{
data[index].name[i] = cursor;
cin.get(cursor);
if (cursor == 10)
{
i++;
break;
}
}
data[index].len = i; /* store name length */
index++; /* update the index */
}
len = index;
cin.get(cursor); /* skip the No such accout */
while (cursor != 10) /* move cursor the end of line */
cin.get(cursor);
index = 0;
result = 0;
cout.setf(ios::fixed);
cout.precision(2);
while(1) /* process the transaction */
{
for (i=0;i<3;i++) /* read in sequential number */
cin>>snum[i];
if ((strcmp(snum,prenum) != 0)&&(index > 0)) /* when sequential number change, do print result */
{
if (result != 0)
{
cout<<"*** Transaction "<<prenum[0]<<prenum[1]<<prenum[2]<<" is out of balance ***\n"; /* print title */
for (i=0;i<index;i++)
{
for (j=0;j<3;j++) /* print account nnumber */
cout<<transaction[i].accountnum[j];
for (k=0;k<len;k++) /* check account */
{
if (transaction[i].accountnum[0] == data[k].num[0])
{
if (transaction[i].accountnum[1] == data[k].num[1])
{
if (transaction[i].accountnum[2] == data[k].num[2])
break;
}
}
}
cout<<" ";
for (j=0;j<data[k].len;j++) /* print account name */
cout<<data[k].name[j];
for (j=data[k].len;j<32;j++)
cout<<" ";
cout.width(9);
if (transaction[i].money < 0)
temp = 0 - ((0 - transaction[i].money) / 100);
else
temp = (transaction[i].money / 100);
cout<<temp<<endl;
} /* end for */
if (result > 0) /* print out of balance */
result = 0 - (result / 100);
else
result = (0 - result) / 100;
cout<<"999 Out of Balance ";
cout.width(9);
cout<<result<<endl<<endl;
} /* end if */
result = 0; /* reset the state */
index = 0;
} /* end if */
if ((snum[0] == '0')&&(snum[1] == '0')&&(snum[2] == '0')) /* end of transactions */
break;
strcpy(prenum,snum); /* update sequential number */
for (i=0;i<3;i++) /* read in the account number */
cin>>transaction[index].accountnum[i];
cin>>transaction[index].money; /* read in the transaction money */
result = result + transaction[index].money;
index++;
}
}
I have the same problem
It is not much, but atleast you know you are not alone. If I accept it or receive some other information I will write. ![:roll:](./images/smilies/icon_rolleyes.gif)
![:roll:](./images/smilies/icon_rolleyes.gif)
finaly got Accepted
My problem was when dividing by 100. I solved it by dividing not by 100.0, but using integer division by 100 and %100 ![:lol:](./images/smilies/icon_lol.gif)
![:lol:](./images/smilies/icon_lol.gif)
187 submission
this problem is just too easy..
the only catch is the problemsetters forgot to mention that transactions to be debit and credit need not be 1 and 1 transactions only.
e.g.
101121 105
101241 -5
101401 -100
this would mean, 1 debit, and 2 credits cancel it out. The transactions for debit and corresponding credit aren't scattered anywhere, but follow each other,
e.g
101121 105
101151 12
101241 -5
101401 -100
here, lines 1, 3, and 4 don't cancel out.
pretty dumb... it's just a badly worded problem hence i guess some people failed.
the only catch is the problemsetters forgot to mention that transactions to be debit and credit need not be 1 and 1 transactions only.
e.g.
101121 105
101241 -5
101401 -100
this would mean, 1 debit, and 2 credits cancel it out. The transactions for debit and corresponding credit aren't scattered anywhere, but follow each other,
e.g
101121 105
101151 12
101241 -5
101401 -100
here, lines 1, 3, and 4 don't cancel out.
pretty dumb... it's just a badly worded problem hence i guess some people failed.
problem 187 in java - WA
Hi, i always receive wrong answer with my code. Where is the problem please (in my opinion it works good)???
[java]
import java.io.IOException;
import java.text.*;
import java.util.*;
class Main {
static Ucet[] ucet = new Ucet[101];
static Transakce[] transakce = new Transakce[1001];
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
public static void main(String[] args) {
String s;
DecimalFormat df = new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.UK));
int maxUcet = 0, maxTransakce = 0;
for (int i = 0 ; i < 101 ; i++) {
s = ReadLn(34);
ucet = new Ucet(s);
maxUcet++;
if (ucet.cislo.equals("000")) break;
}
for (int i = 0 ; i < 1001 ; i++) {
s = ReadLn(16);
transakce = new Transakce(s);
if (transakce.cislo.equals("000") && transakce.ucet.equals("000"))
break;
maxTransakce++;
}
int ii = 0;
while (ii < maxTransakce) {
String testuj = transakce[ii].cislo;
int i = ii;
float castka = 0;
for ( ; testuj.equals(transakce.cislo) ; i++) castka += transakce.castka;
if (castka != 0) {
System.out.println("*** Transaction " + transakce[i-1].cislo + " is out of balance ***");
for (int j = ii ; testuj.equals(transakce[j].cislo) ; j++) {
System.out.print(transakce[j].ucet + " ");
for (int k = 0 ; k < 100 ; k++) {
if (ucet[k].cislo.equals(transakce[j].ucet)) {
System.out.print(ucet[k].nazev);
for (int f = 0 ; f < (30 - ucet[k].nazev.length()) ; f++)
System.out.print(" ");
for (int f = 0 ; f < (10 - new Float((df.format(transakce[j].castka))).toString().length()) ; f++)
System.out.print(" ");
System.out.println(df.format(transakce[j].castka));
break;
}
}
}
System.out.print("999 Out of Balance ");
for (int f = 0 ; f < (10 - new Float((df.format(0-castka))).toString().length()) ; f++)
System.out.print(" ");
System.out.println(df.format(0-castka));
System.out.println("");
}
ii = i;
}
}
}
class Ucet {
String cislo;
String nazev;
Ucet(String s) {
cislo = s.substring(0,3);
nazev = s.substring(3,s.length()-1);
}
}
class Transakce {
String cislo;
String ucet;
float castka;
Transakce(String s) {
boolean znamenko = false;
cislo = s.substring(0,3);
ucet = s.substring(3,6);
s = s.substring(7,s.length()).trim();
if ("-".equals(s.substring(0,1))) {
znamenko = true;
s = s.substring(1, s.length());
}
int length = s.length();
if (length == 2) {
if (znamenko) s = "-0." + s;
else s = "0." + s;
}
else if (length == 1) {
if (znamenko) s = "-0.0" + s;
else s = "0.0" + s;
}
else {
if (znamenko) s = "-" + s.substring(0,length-2) + "." + s.substring(length-2,length);
else s = s.substring(0,length-2) + "." + s.substring(length-2,length);
}
castka = (new Float(s)).floatValue();
}
}
[/java]
[java]
import java.io.IOException;
import java.text.*;
import java.util.*;
class Main {
static Ucet[] ucet = new Ucet[101];
static Transakce[] transakce = new Transakce[1001];
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
public static void main(String[] args) {
String s;
DecimalFormat df = new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.UK));
int maxUcet = 0, maxTransakce = 0;
for (int i = 0 ; i < 101 ; i++) {
s = ReadLn(34);
ucet = new Ucet(s);
maxUcet++;
if (ucet.cislo.equals("000")) break;
}
for (int i = 0 ; i < 1001 ; i++) {
s = ReadLn(16);
transakce = new Transakce(s);
if (transakce.cislo.equals("000") && transakce.ucet.equals("000"))
break;
maxTransakce++;
}
int ii = 0;
while (ii < maxTransakce) {
String testuj = transakce[ii].cislo;
int i = ii;
float castka = 0;
for ( ; testuj.equals(transakce.cislo) ; i++) castka += transakce.castka;
if (castka != 0) {
System.out.println("*** Transaction " + transakce[i-1].cislo + " is out of balance ***");
for (int j = ii ; testuj.equals(transakce[j].cislo) ; j++) {
System.out.print(transakce[j].ucet + " ");
for (int k = 0 ; k < 100 ; k++) {
if (ucet[k].cislo.equals(transakce[j].ucet)) {
System.out.print(ucet[k].nazev);
for (int f = 0 ; f < (30 - ucet[k].nazev.length()) ; f++)
System.out.print(" ");
for (int f = 0 ; f < (10 - new Float((df.format(transakce[j].castka))).toString().length()) ; f++)
System.out.print(" ");
System.out.println(df.format(transakce[j].castka));
break;
}
}
}
System.out.print("999 Out of Balance ");
for (int f = 0 ; f < (10 - new Float((df.format(0-castka))).toString().length()) ; f++)
System.out.print(" ");
System.out.println(df.format(0-castka));
System.out.println("");
}
ii = i;
}
}
}
class Ucet {
String cislo;
String nazev;
Ucet(String s) {
cislo = s.substring(0,3);
nazev = s.substring(3,s.length()-1);
}
}
class Transakce {
String cislo;
String ucet;
float castka;
Transakce(String s) {
boolean znamenko = false;
cislo = s.substring(0,3);
ucet = s.substring(3,6);
s = s.substring(7,s.length()).trim();
if ("-".equals(s.substring(0,1))) {
znamenko = true;
s = s.substring(1, s.length());
}
int length = s.length();
if (length == 2) {
if (znamenko) s = "-0." + s;
else s = "0." + s;
}
else if (length == 1) {
if (znamenko) s = "-0.0" + s;
else s = "0.0" + s;
}
else {
if (znamenko) s = "-" + s.substring(0,length-2) + "." + s.substring(length-2,length);
else s = s.substring(0,length-2) + "." + s.substring(length-2,length);
}
castka = (new Float(s)).floatValue();
}
}
[/java]
From the problem statement:
Each of these records is one entry of a transaction. A transaction consists of between two and ten entries with identical sequence numbers. Each transaction will be contiguous within the input data.
I don't think that it is "badly worded" at all - it might be a bit confusing though. All you have to check is if each transaction balances (as a whole) - there is no suggestion that it has to be one credit to one debit.
Each of these records is one entry of a transaction. A transaction consists of between two and ten entries with identical sequence numbers. Each transaction will be contiguous within the input data.
I don't think that it is "badly worded" at all - it might be a bit confusing though. All you have to check is if each transaction balances (as a whole) - there is no suggestion that it has to be one credit to one debit.
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
187
what should be the output of this input
I alwas get WA why
here is my code in C++
any idea plz help me
Code: Select all
111Cash
121Accounts Receivable
211Accounts Payable
241Sales Tax Payable
401Sales
555Office Supplies
000No such account
100111100000000
100121-50000000
100134-50000001
101121 105
101241 -7
101401 -100
102211 -70000
102555 40000
103111 -40000
103555 40000
000000 0
here is my code in C++
Code: Select all
ACCEPTED after two years
Last edited by emotional blind on Tue Jun 19, 2007 8:15 am, edited 1 time in total.
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
Hi,
My AC code outputs:
My AC code outputs:
Code: Select all
*** Transaction 100 is out of balance ***
111 Cash 1000000.00
121 Accounts Receivable -500000.00
134 -500000.01
999 Out of Balance 0.01
*** Transaction 101 is out of balance ***
121 Accounts Receivable 1.05
241 Sales Tax Payable -0.07
401 Sales -1.00
999 Out of Balance 0.02
*** Transaction 102 is out of balance ***
211 Accounts Payable -700.00
555 Office Supplies 400.00
999 Out of Balance 300.00
-
- Learning poster
- Posts: 56
- Joined: Tue Jun 13, 2006 5:18 pm
- Location: (CSE, SUST) Sylhet, Bangladesh
- Contact:
I getting P.E in 187
I am getting P.E. How can I remove this? Anybody help please.
Here's my code:
Here's my code:
Code: Select all
Removed After ACC
Last edited by Mushfiqur Rahman on Mon Mar 12, 2007 12:37 pm, edited 1 time in total.
Mushfiq , you have the problem with fieldwitdh for the value (ara[k].value/100.00) and fabs(sum/100.00) .
Fixed it and got ACC .
Fixed it and got ACC .
_______________________________________
http://acm.uva.es/problemset/usersnew.php?user=6114
http://acm.uva.es/problemset/usersnew.php?user=6114
-
- Learning poster
- Posts: 56
- Joined: Tue Jun 13, 2006 5:18 pm
- Location: (CSE, SUST) Sylhet, Bangladesh
- Contact:
-
- Learning poster
- Posts: 56
- Joined: Tue Jun 13, 2006 5:18 pm
- Location: (CSE, SUST) Sylhet, Bangladesh
- Contact: