+ 1
New Driverâs License problem
Hello ! So when I compile the code on my computer works fine and the calculations are right. But here I have some problem with the input of the names of the other four customers. Does somebody see the mistake? import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner in=new Scanner(System.in); String myName=in.nextLine(); int agents=in.nextInt(); String four=in.nextLine(); String[]names=four.split(" "); int myPlace=1; int howLong=0; for (int i=0; i<names.length; i++) { if(myName.compareTo(names[i])>0) myPlace++; } if(myPlace==1||names.length+1==agents) howLong=20; else howLong=(Math.round(myPlace/agents))*20; System.out.println(howLong); } }
7 Answers
+ 8
well, try that, it workedđ:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class Program
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String myname = sc.nextLine();
int agent = sc.nextInt();
String dummy = sc.nextLine(); // <-- ??????????????
String others = sc.nextLine();
sc.close();
String[] alt = others.split(" ");
ArrayList<String> names = new ArrayList<String>();
for (int i=0; i<alt.length; i++) {
names.add(alt[i]);
}
names.add(myname);
Collections.sort(names);
int pos = names.indexOf(myname) + 1;
int a = 0;
do {
pos -= agent;
a++;
} while (pos>0);
System.out.println(a*20);
}
}
+ 3
After inputting the number of agents, add this line:
String dummy = sc.nextLine()
The reason why your code is not working is because after inputting the int, there is a newline character which is not inputted. So storing that in a dummy String will make the program work and the rest of the inputs will work fine.
+ 3
Then you would have to check the second letter and follow the alphabetical order. If 2nd letter also matches then check the 3rd and so on.
+ 3
so finally how is the result?????'
+ 1
that fixed it ! thanks ! 2 tests fail though..i wonder why.. for example if the name of someone else starts with the same letter as yours what would be the case then
+ 1
it worked. the problem were the number of agents not the names
0
myname = input()
num_agents = int(input())
other_names = list(input().split())
other_names.append(myname)
other_names.sort()
ind_myname = other_names.index(myname)+1
print('20' if ind_myname <=num_agents else (ind_myname-num_agents)*20+20)