187 - Transaction Processing

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Moha
Experienced poster
Posts: 216
Joined: Tue Aug 31, 2004 1:02 am
Location: Tehran
Contact:

Diffrence between cout/printf?

Post by Moha » Thu Sep 20, 2007 7:13 am

To day I saw a very strange thing. I had gotten PE in 186,187. I didn't know what to do to correct them. But today I changed output part of code into famous printf(It was in C++) code and my code got AC.
Here is my old version of output for 187:

Code: Select all

cout.setf(ios::fixed|ios::showpoint);
cout.precision(2);
cout<<i->first<<' ';
cout.setf(ios::left);
cout.width(30);
cout<<account[i->first];
cout<<' ';
cout.setf(ios::right);
cout.width(10);
cout<<i->second/100.0<<endl;
and the printf version is:

Code: Select all

printf("%s %-30s %10.2lf\n",i->first.c_str(),account[i->first].c_str(),i->second/100.0);
Is there any difference between these two codes?

lnr
Experienced poster
Posts: 142
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: Diffrence between cout/printf?

Post by lnr » Mon Oct 06, 2008 4:12 pm

I tested with these.
But got nothing difference.

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 187 - Transaction Processing

Post by uDebug » Fri Sep 12, 2014 2:27 pm

On this problem, since I employed C++, I used the following statement to print the values in a table

Code: Select all

printf("%-3s %-30s %10.2f\n", accountNumberStr, accountNameStr, value);
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

novaten
New poster
Posts: 2
Joined: Wed Dec 23, 2015 7:38 am

Re: 187 - Transaction Processing

Post by novaten » Mon Mar 28, 2016 10:04 pm

Hi , I am also keep getting WA, i was suspicious about the division by 100 and then % 100 like others comment but since I got a double I do not think is the case. Does anyone detect something wrong in My Code ? Thanks.

/**187 - Transaction Processing
* Simulation
* @author Octavio Diaz
*/
import java.io.*;
import java.util.*;

class Account{
public double balance;
public List<Pair> accounts= new ArrayList<>();
public Account(String account, double amount){
this.accounts.add(new Pair(account,amount));
}
}
class Pair{
public String number;
public double amount;

public Pair(String number, double amount) {
this.number = number;
this.amount = amount;
}
}

class Main {
static Map<String,String> accountsNames= new HashMap<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Map<String,Account> accountTrans =new HashMap<>();

String line;
boolean accountsDone=false;
while((line=br.readLine())!=null){

if(!accountsDone){
String accountNumber = line.substring(0, 3);
String accountName = line.substring(3, line.length());
if(accountNumber.equals("000")) {
accountsDone=true;
continue;
}
accountsNames.put(accountNumber,accountName);
}
else{

String tn = line.substring(0, 3);
if(tn.equals("000")) break;
String account = line.substring(3, 6);
double amount = Double.parseDouble(line.substring(7,line.length()));
if(accountTrans.containsKey(tn)){
List accounts= accountTrans.get(tn).accounts;
accounts.add(new Pair(account, amount));
}
else{
accountTrans.put(tn, new Account(account,amount));
}

accountTrans.get(tn).balance+=amount;
}

}

processOutput(accountTrans);

}

private static void processOutput(Map<String,Account> accounts){
int cont=0;
for (Map.Entry<String, Account> entry : accounts.entrySet()) {
String key = entry.getKey();
Account value = entry.getValue();
if(value.balance!=0.0d){
System.out.println("*** Transaction "+key+" is out of balance ***");
for (int i = 0; i < value.accounts.size(); i++) {
String accountNumber =value.accounts.get(i).number;
System.out.format("%s %-30s %10.2f\n",accountNumber,accountsNames.get(accountNumber) ,value.accounts.get(i).amount/100.00);
}
System.out.format("%s %-30s%10.2f\n","999","Out of Balance",value.balance*-1/100.00);
}
if(cont<accounts.size()-2)
System.out.println("");
cont++;
}
}

}

Post Reply

Return to “Volume 1 (100-199)”