Okay so I need to make a number pattern with day numbers for example: 1 - monday, 2 - tuesday, 3 - wednesday until 7 - sunday. If I put an input "n" I would get the following:
n=4
1 2 3 4
n=7
1 2 3 4 5 6 7
n=12
1 2 3 4 5 6 7 1 2 3 4 5
I've succeeded making this program if n<=14 but if n>14 I get:
n=17
1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 10
when it should be:
n=17
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3
this is my code
for (x=1;x<=n;x++){
System.out.print(x+" ");
if (x==7){
for (x=1;x<=(n-7);x++)
System.out.print(x+" ");
break;
}
}
thanks in advance
Try this instead:
for (int i = 0; i < n; i++)
System.out.print(i % 7 + 1 + " ");
Whenever you want to have that "repeating" behavior, where a sequence of numbers goes up to a certain value and then restarts, use the % operator and a bit of modular arithmetic to achieve the desired effect. For n = 17 the above will print:
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3
Related
I wanted to generate a board has some direction datas in each point.
public class practiceF {
static int [][][]p = new int [10][9][ ];
static int [][]PointDirection = {
{3,5,0},
{3,5,7,0},
{5,7,0},
{1,5,7,0},
{1,7,0},
{1,3,7,0},
{1,3,0},
{1,3,5,0},
{3,4,5,7,0},
{3,5,6,7,0},
{1,2,3,5,7,0},
{1,3,5,7,8,0},
{1,3,4,5,7,0},
{1,3,5,6,7,0},
{1,2,3,7,0},
{1,3,7,8,0},
{1,2,3,4,5,6,7,8,0},
{1,3,5,7,0}
};
public void textPrint() {
for(int i=0;i<10;i++) {
for(int j=0;j<9;j++) {
System.out.print("|point"+i+"."+j+" : ");
for(int k=0;k<p[i][j].length;k++) {
System.out.print(p[i][j][k]+" ");
}
}
System.out.println("|");
}
}
public static void main(String[] args) {
practiceF pf =new practiceF();
for(int i=0;i<10;i++) {
for(int j=0;j<9;j++) {
p[i][j]=PointDirection[17];//모든 칸 전후좌우 이동 지정
for(int k=0;k<p[i][j].length;k++) {
}
}
}
pf.textPrint();
System.out.println("-------------------");
p[1][2][3]=9;
pf.textPrint();
}
}
I've only selected the problematic part of the entire code.
array p is actual database of board.
p[x coordinate][y coordinate][direction int] this is each int means.
PointDirection array is inserted in p array.
this process create board and insert some Direction int in each point.
in main, print board on console, and change Direction int of (1,2)point 0 to 9.
then, next print have to display change just Direction int of (1,2)point, but this code changes Direction int of all points.
this is result.
|point0.0 : 1 3 5 7 0 |point0.1 : 1 3 5 7 0 |point0.2 : 1 3 5 7 0 |point0.3 : 1 3 5 7 0 |point0.4 : 1 3 5 7 0 |point0.5 : 1 3 5 7 0 |point0.6 : 1 3 5 7 0 |point0.7 : 1 3 5 7 0 |point0.8 : 1 3 5 7 0 |
|point1.0 : 1 3 5 7 0 |point1.1 : 1 3 5 7 0 |point1.2 : 1 3 5 7 0 |point1.3 : 1 3 5 7 0 |point1.4 : 1 3 5 7 0 |point1.5 : 1 3 5 7 0 |point1.6 : 1 3 5 7 0 |point1.7 : 1 3 5 7 0 |point1.8 : 1 3 5 7 0 |
|point2.0 : 1 3 5 7 0 |point2.1 : 1 3 5 7 0 |point2.2 : 1 3 5 7 0 |point2.3 : 1 3 5 7 0 |point2.4 : 1 3 5 7 0 |point2.5 : 1 3 5 7 0 |point2.6 : 1 3 5 7 0 |point2.7 : 1 3 5 7 0 |point2.8 : 1 3 5 7 0 |
|point3.0 : 1 3 5 7 0 |point3.1 : 1 3 5 7 0 |point3.2 : 1 3 5 7 0 |point3.3 : 1 3 5 7 0 |point3.4 : 1 3 5 7 0 |point3.5 : 1 3 5 7 0 |point3.6 : 1 3 5 7 0 |point3.7 : 1 3 5 7 0 |point3.8 : 1 3 5 7 0 |
|point4.0 : 1 3 5 7 0 |point4.1 : 1 3 5 7 0 |point4.2 : 1 3 5 7 0 |point4.3 : 1 3 5 7 0 |point4.4 : 1 3 5 7 0 |point4.5 : 1 3 5 7 0 |point4.6 : 1 3 5 7 0 |point4.7 : 1 3 5 7 0 |point4.8 : 1 3 5 7 0 |
|point5.0 : 1 3 5 7 0 |point5.1 : 1 3 5 7 0 |point5.2 : 1 3 5 7 0 |point5.3 : 1 3 5 7 0 |point5.4 : 1 3 5 7 0 |point5.5 : 1 3 5 7 0 |point5.6 : 1 3 5 7 0 |point5.7 : 1 3 5 7 0 |point5.8 : 1 3 5 7 0 |
|point6.0 : 1 3 5 7 0 |point6.1 : 1 3 5 7 0 |point6.2 : 1 3 5 7 0 |point6.3 : 1 3 5 7 0 |point6.4 : 1 3 5 7 0 |point6.5 : 1 3 5 7 0 |point6.6 : 1 3 5 7 0 |point6.7 : 1 3 5 7 0 |point6.8 : 1 3 5 7 0 |
|point7.0 : 1 3 5 7 0 |point7.1 : 1 3 5 7 0 |point7.2 : 1 3 5 7 0 |point7.3 : 1 3 5 7 0 |point7.4 : 1 3 5 7 0 |point7.5 : 1 3 5 7 0 |point7.6 : 1 3 5 7 0 |point7.7 : 1 3 5 7 0 |point7.8 : 1 3 5 7 0 |
|point8.0 : 1 3 5 7 0 |point8.1 : 1 3 5 7 0 |point8.2 : 1 3 5 7 0 |point8.3 : 1 3 5 7 0 |point8.4 : 1 3 5 7 0 |point8.5 : 1 3 5 7 0 |point8.6 : 1 3 5 7 0 |point8.7 : 1 3 5 7 0 |point8.8 : 1 3 5 7 0 |
|point9.0 : 1 3 5 7 0 |point9.1 : 1 3 5 7 0 |point9.2 : 1 3 5 7 0 |point9.3 : 1 3 5 7 0 |point9.4 : 1 3 5 7 0 |point9.5 : 1 3 5 7 0 |point9.6 : 1 3 5 7 0 |point9.7 : 1 3 5 7 0 |point9.8 : 1 3 5 7 0 |
-------------------
|point0.0 : 1 3 5 7 9 |point0.1 : 1 3 5 7 9 |point0.2 : 1 3 5 7 9 |point0.3 : 1 3 5 7 9 |point0.4 : 1 3 5 7 9 |point0.5 : 1 3 5 7 9 |point0.6 : 1 3 5 7 9 |point0.7 : 1 3 5 7 9 |point0.8 : 1 3 5 7 9 |
|point1.0 : 1 3 5 7 9 |point1.1 : 1 3 5 7 9 |point1.2 : 1 3 5 7 9 |point1.3 : 1 3 5 7 9 |point1.4 : 1 3 5 7 9 |point1.5 : 1 3 5 7 9 |point1.6 : 1 3 5 7 9 |point1.7 : 1 3 5 7 9 |point1.8 : 1 3 5 7 9 |
|point2.0 : 1 3 5 7 9 |point2.1 : 1 3 5 7 9 |point2.2 : 1 3 5 7 9 |point2.3 : 1 3 5 7 9 |point2.4 : 1 3 5 7 9 |point2.5 : 1 3 5 7 9 |point2.6 : 1 3 5 7 9 |point2.7 : 1 3 5 7 9 |point2.8 : 1 3 5 7 9 |
|point3.0 : 1 3 5 7 9 |point3.1 : 1 3 5 7 9 |point3.2 : 1 3 5 7 9 |point3.3 : 1 3 5 7 9 |point3.4 : 1 3 5 7 9 |point3.5 : 1 3 5 7 9 |point3.6 : 1 3 5 7 9 |point3.7 : 1 3 5 7 9 |point3.8 : 1 3 5 7 9 |
|point4.0 : 1 3 5 7 9 |point4.1 : 1 3 5 7 9 |point4.2 : 1 3 5 7 9 |point4.3 : 1 3 5 7 9 |point4.4 : 1 3 5 7 9 |point4.5 : 1 3 5 7 9 |point4.6 : 1 3 5 7 9 |point4.7 : 1 3 5 7 9 |point4.8 : 1 3 5 7 9 |
|point5.0 : 1 3 5 7 9 |point5.1 : 1 3 5 7 9 |point5.2 : 1 3 5 7 9 |point5.3 : 1 3 5 7 9 |point5.4 : 1 3 5 7 9 |point5.5 : 1 3 5 7 9 |point5.6 : 1 3 5 7 9 |point5.7 : 1 3 5 7 9 |point5.8 : 1 3 5 7 9 |
|point6.0 : 1 3 5 7 9 |point6.1 : 1 3 5 7 9 |point6.2 : 1 3 5 7 9 |point6.3 : 1 3 5 7 9 |point6.4 : 1 3 5 7 9 |point6.5 : 1 3 5 7 9 |point6.6 : 1 3 5 7 9 |point6.7 : 1 3 5 7 9 |point6.8 : 1 3 5 7 9 |
|point7.0 : 1 3 5 7 9 |point7.1 : 1 3 5 7 9 |point7.2 : 1 3 5 7 9 |point7.3 : 1 3 5 7 9 |point7.4 : 1 3 5 7 9 |point7.5 : 1 3 5 7 9 |point7.6 : 1 3 5 7 9 |point7.7 : 1 3 5 7 9 |point7.8 : 1 3 5 7 9 |
|point8.0 : 1 3 5 7 9 |point8.1 : 1 3 5 7 9 |point8.2 : 1 3 5 7 9 |point8.3 : 1 3 5 7 9 |point8.4 : 1 3 5 7 9 |point8.5 : 1 3 5 7 9 |point8.6 : 1 3 5 7 9 |point8.7 : 1 3 5 7 9 |point8.8 : 1 3 5 7 9 |
|point9.0 : 1 3 5 7 9 |point9.1 : 1 3 5 7 9 |point9.2 : 1 3 5 7 9 |point9.3 : 1 3 5 7 9 |point9.4 : 1 3 5 7 9 |point9.5 : 1 3 5 7 9 |point9.6 : 1 3 5 7 9 |point9.7 : 1 3 5 7 9 |point9.8 : 1 3 5 7 9 |
i just want to change one point Direction int. not all point.
Idon't know what's wrong.
You assigned the same instance PointDirection[17] to all the points. So any changes in it affect all the points. You should copy content of PointDirection[17] to a new array instance for each point.
Try this:
p[i][j] = Arrays.copyOf(PointDirection[17], PointDirection[17].length);
I am not sure what is wrong. I looked up other similar titled questions and answers on stack overflow but not sure what is wrong with my method. I am a beginner in java so any help would be great. Thank you.
My code is:
import java.util.*;
class LargestNumber {
static void printLargest(Vector<String> arr){
Collections.sort(arr, new Comparator<String>(){
#Override
public int compare(String X, String Y) {
String XY=X + Y;
String YX=Y + X;
return XY.compareTo(YX) > 0 ? -1:1;
}
});
Iterator it = arr.iterator();
while(it.hasNext())
System.out.print(it.next());
}
public static void main (String[] args) {
Scanner s=new Scanner(System.in);
int i,n;
n=s.nextInt();
Vector<String> arr;
arr = new Vector<>();
for(i=0;i<n;i++){
arr.add(s.next());
}
printLargest(arr);
}
}
error:
100
2 8 2 3 6 4 1 1 10 6 3 3 6 1 3 8 4 6 1 10 8 4 10 4 1 3 2 3 2 6 1 5 2 9 8 5 10 8 7 9 6 4 2 6 3 8 8 9 8 2 9 10 3 10 7 5 7 1 7 5 1 4 7 6 1 10 5 4 8 4 2 7 8 1 1 7 4 1 1 9 8 6 5 9 9 3 7 6 3 10 8 10 7 2 5 1 1 9 9 5
Your output:
Your stderr:
Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(TimSort.java:777)
at java.util.TimSort.mergeAt(TimSort.java:514)
at java.util.TimSort.mergeCollapse(TimSort.java:441)
at java.util.TimSort.sort(TimSort.java:245)
at java.util.Arrays.sort(Arrays.java:1512)
at java.util.Vector.sort(Vector.java:1345)
at java.util.Collections.sort(Collections.java:177)
at LargestNumber.printLargest(LargestNumber.java:6)
at LargestNumber.main(LargestNumber.java:32)
Correct output:
9999999998888888888887777777776666666666555555554444444443333333333222222222111111111111111101010101010101010
(Time used: 0.12/1.50, memory used: 23568384/536870912.)`enter code here`
Problem is that your statement XY.compareTo(YX) > 0 ? -1:1 does not return 0 when two variables are equal, which is IllegalArgumentException: Comparison method violates its general contract!
From doc:
Parameters:
o1 - the first object to be compared.
o2 - the second object to be compared.
Returns: a negative integer, zero, or a positive integer as the first
argument is less than, equal to, or greater than the second.
Use following code to compare:
arr.sort((X, Y) -> {
String XY = X + Y;
String YX = Y + X;
return XY.compareTo(YX);
});
What's happening is that you are entering
2 8 2 3 6 4 1 1 10 6 3 3 6 1 3 8 4 6 1 10 8 4 10 4 1 3 2 3 2 6 1 5 2 9 8 5 10 8 7 9 6 4 2 6 3 8 8 9 8 2 9 10 3 10 7 5 7 1 7 5 1 4 7 6 1 10 5 4 8 4 2 7 8 1 1 7 4 1 1 9 8 6 5 9 9 3 7 6 3 10 8 10 7 2 5 1 1 9 9 5
with spaces. When that happens, the input gets treated like a one string without another string to compare to, which is why you got IllegalArgumentException. To fix this code, change your for loop in your main method like this...
for(i=0;i<n;i++){
arr.add(String.valueOf(new Random().nextInt(17)));
}
I was in an interview today and I got the following exercice :
write a program to find all the 3x3 matrix X for each element x varying from 0 to 100
A B C
D E F
G H I
that meet the following requirements
A + B - C = 4
+ - -
D - E * F = 4
/ * -
G + H + I = 4
= = =
4 4 4
write a program in Java.
It is not really clear what your question is, but this seems quite straightforward to simply bruteforce by trying all choices in a sensible order.
For example, this Python code:
for G in range(1,4+1):
for H in range(4+1-G):
I = 4 - H - G
for A in range(0,4+1):
D = G*(4-A)
if not 0<=D<=100:
continue
for E in range(100+1):
for F in range(100+1):
if D-E*F==4:
for B in range(100+1):
C=A+B-4
if 0<=C<=100:
if B-E*H==4:
if C-F-I==4:
print A,B,C
print D,E,F
print G,H,I
print A+B-C,D-E*F,G+H+I,A+D/G,B-E*H,C-F-I
finds the following 4 solutions:
0 10 6
4 6 0
1 1 2
4 4 4 4 4 4
2 7 5
4 3 0
2 1 1
4 4 4 4 4 4
1 8 5
6 2 1
2 2 0
4 4 4 4 4 4
2 6 4
4 1 0
2 2 0
4 4 4 4 4 4
If we have a loop like this
for(int i=0; i<n; i+=2)
{
total+=1;
}
I assume there is one instruction in the header, and it executes one instruction in the body so the total number of instruction is (n-0)/2*1+1 (last condition). Is it the right way to do it? How would you count it?
Also, how about this?
for(int i=1; i<n; i*=2)
{
total+=1;
}
If n = 20, i goes like 1,2,4,8,16, and I'm not sure how to calculate it
Just write them out on paper and look for a pattern. For your first example:
for(int i=0; i<n; i+=2)
n i's total
0 [] 0
1 [0] 1
2 [0] 1
3 [0,2] 2
4 [0,2] 2
5 [0,2,4] 3
6 [0,2,4] 3
and so on..
That is floor((n+1)/2) (or integer division).
The second one; we can see it's going to be related to log2 just by examination, so let's compare the counts with log2(n) to see if we can find a pattern:
for(int i=1; i<n; i*=2)
n i's total log2(n)
1 [] 0 ---
2 [1] 1 1
3 [1,2] 2 1.58
4 [1,2] 2 2
5 [1,2,4] 3 2.32
6 [1,2,4] 3 2.58
7 [1,2,4] 3 2.81
8 [1,2,4] 3 3
9 [1,2,4,8] 4 3.17
10 [1,2,4,8] 4 3.32
11 [1,2,4,8] 4 3.46
12 [1,2,4,8] 4 3.58
13 [1,2,4,8] 4 3.70
14 [1,2,4,8] 4 3.81
15 [1,2,4,8] 4 3.91
16 [1,2,4,8] 4 4
17 [1,2,4,8,16] 5 4.09
Looking at this and thinking about it for a moment we can see it is floor(1+log2(n-1)), with special cases for n=0 (total=0) and n=1 (total=0).
The thought process in that last one is: we have to "shift" the log2 column down one to make it line up with the total column (hence log2(n-1)), and we have to add 1 to it to make the values match.
I have been doing web-scraping for a project and I couldn't figure out how to split retrieved strings into different variables.
Array of strings I have retrieved
K. KAYMAKLI TSK 6 5 0 1 19 4 15 15
YEN?CAM? AK 6 4 1 1 14 7 7 13
MORMENEK?E GB 6 4 0 2 10 7 3 12
LEFKE TSK 6 3 2 1 10 8 2 11
SERDARLI GB 6 2 2 2 6 5 1 8
HAM?TKÖY ?HSK 6 2 2 2 8 8 0 8
ÇET?NKAYA TSK 6 2 2 2 6 7 -1 8
DO?AN TBSK 6 2 2 2 12 15 -3 8
YEN? BO?AZ?Ç? DSK 6 2 1 3 9 8 1 7
B. BA?CIL SK 6 1 4 1 8 9 -1 7
MA?USA TÜRK GÜCÜ 6 2 1 3 7 9 -2 7
C?HANG?R GSK 6 1 3 2 8 8 0 6
GENÇL?K GÜCÜ SK 6 1 0 5 6 14 -8 3
YALOVA SK 6 0 2 4 4 18 -14 2
I would like to put the characters until the first integer (in this case 6) into one string
and then each integer into separate variables.
eg.
String team = "YALOVA SK";
int p = 6;
int x = 0;
int y = 2;
int z = 4;
int m = 4;
int n = 18;
int k = -14;
int h = 2;
I was able to split the string by checking character by character to find the first integer and split it there and then assign each character after that to an integer. How can I solve it by using regex?
Note ?'s are turkish characters that are not displayed correctly on the console but are display correctly in the application.
The split() method on a string can be used to split the string based on a particular 'regex'. For reference on split function check Oracle java documentation link. There is also a nice tutorial about using regex in java in the Vogella website.
You can split the string based on '\s'(short white space character) or '\s+'and then use the returned array.
The syntax will be something like this.
String retrievedString = "YALOVA SK 6 0 2 4 4 18 -14 2";
String[] teamInfo=retrievedString.split("\\s");
You can use the string you retrieve from web scraping in place of "retrievedString".
Note the \\ used in the split method is to escape \.
Hope this helps...
I think Scanner class fit perfect to your needs:
Description: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html