Remove one element in each row from a solved Sudoku puzzle [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to create a method that will remove one random element in each row from a solved Sudoku matrix that I have generated. How can I achieved this?
Solved Sudoku Generator:
public static void generate_Sud (int n, int [][] S){ // Generates Sudoku method
for (int r=0; r<=n-1; r++){
int startNum = (int) (Math.sqrt(n) * (r % Math.sqrt(n)) + (r/Math.sqrt(n)));
for (int c=0; c<=n-1; c++){
S[r][c] = ((startNum + c) % n) + 1;
}
}
}
Sudoku Matrix Generated

for a 3x3 Sodoku:
for(int i = 0; i < 3; i++){
int randomPosition = (int) Math.floor(Math.random() * 3) //Random number between 0 and 2
S[i][randomPosition] = -1; // -1 or whatever you use to represent blank
}

Related

Finding longest array in a three-dimensional array and returning a one-dimensional array that contains the largest array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
A method public static int[] largestArray(int[][][]a)
returns a single dimensional array that contains the largest array in the 3D array
If you wish to get the first element which matches your condition, you can try this code.
for(int i = 0; i < arr.length; i++) {
int[][] arrI = arr[i];
for(int j = 0; j < arrI.length; j++) {
int[] arrJ = arr[j];
if(arrJ.length > saved.length) {
saved = arrJ;
}
}
}
Hope that helps.

Traverse the array and each time you encounter and change the value in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
if Take an array of size 10 and take user inputs.how can i Traverse the array and each time you encounter '6' or '7', replace them with 60 and 70 respectively in java??
Maybe you can try this,
int[] input = {1,2,3,4,5,6,7,8,9};
for(int i = 0; i < input.length; i++)
if(input[i]==6 || input[i]==7)
input[i]*=10;
//Printing
for(int i : input)
System.out.println(i);
Here is a simplistic approach. Loop through the array and check each positions value. If it's a 6 or 7 replace as expected.
public static void main(String[] args){
int[] array = {1,2,3,4,5,6,7,8,9};
for(int i = 0; i < array.length; i++){
if(array[i] == 6){
array[i] = 60;
}
else if(array[i] == 7){
array[i] = 70;
}
}
for(int i : array){
System.out.println(i);
}
}

trying to use recursion to find the possibilities of two known numbers adding to N [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have this recursion problem that I spent hours on but unable to transform some of my thoughts into code. I have a problem where I need to build a function that tries to fit 3's and 1's into a given number(n). I try to get n from user but then I cannot go further. Basically I need to calculate the possibility of 3's and 1's that add up to n. I know that a constant solution is that I can fit 1n times into n.
Any help is appreciated. Thanks in advance
public static void main(String[] args) {
// TODO code application logic here
Scanner ask = new Scanner(System.in);
System.out.println("input number: ");
int n = ask.nextInt();
}
public static int getPossibility(int n, int k, int p) {
if (n == 0) {
return 0;
} else if (n / p == n ) {
return n;
} else {
int ctr = n;
for (int i = 1; i <= n; i++) {
ctr += getPossibility(n, k - 1, p - 1) ;
}
return ctr;
}
}
}
I'm not sure if I understood what you meant. With this code you try to figure out how many combinatios start by 1 and how many start by 3. Then you just need to sum up and you'll get your solution.
public int possibilities (int n){
int pos1=0;
int pos3 =0;
if (n>2){
pos3 = possibilities(n-3);
}
if (n>0) {
pos1 = possibilities(n-1);
}
return pos1 + pos3;
}
I'm not sure what you want to do, but if you want to calculate how many times 3s and 1s fit into a number you can do it like this:
int threes = n / 3;
int ones = n % threes * 3;

Printing indices of an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm having trouble figuring out why my j won't print out with the current boolean statement. Its only if I put != will it then print 100,000-1 indices.
public static void main(String[] args) {
//array size and name
double[] largearr;
largearr = new double[100000];
double index = 0.00149;
int j = 0;
//population of array and the printout of the elements
for(int i = 0; i < 100000; i++) {
Arrays.fill(largearr, index);
index += 0.00149;
// System.out.println(largearr[i] + " ");
}
for(j = 0; j < largearr.length; j++) {
if(largearr[j] == 58.00122999995376) {
System.out.println("j");
}
}
}
Because you don't have that value in any position of your largerarr. The first for loop in your code fills all the elements of largearr array with the newly incremented index value, and it does it 100000 times: at the end you'll have largearr filled with the last iteration's value of index, that surely isn't 58.00122999995376. So the second for loop doesn't enter the if check for any value and the program quits without printing anything.
If you could explain what are you trying to achieve maybe we could help you better.

Insertion sort array java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So I have to write code for an insertion sort that will sort an array of random ints, the array is already set up and working okay and everything but my sort is not, heres what i have:
for(int i =1; i< numberSort.length-1;i++){
int temp = numberSort[i];
int j = i-1;
while((j >= 0) && (numberSort[j]>temp)){
numberSort[j+1] = numberSort[j];
j = j-1;
}
numberSort[j+1] = temp;
}
}
It seems to me that that should work, however it does not, it moves the numbers around from their original position but does not order them in ascending order. Thanks for any help you may be able to offer.
This code works for me:
public static void main(String[] args) {
int[] numberSort = {22,7,2, 5, 7, 1, 2, 9,33,55,12,1,0};
for (int i = 1; i < numberSort.length; i++) {
int temp = numberSort[i];
int j = i - 1;
while ((j >= 0) && (numberSort[j] > temp)) {
numberSort[j + 1] = numberSort[j];
j = j - 1;
}
numberSort[j + 1] = temp;
}
for (int i = 0; i < numberSort.length; i++) {
System.out.println(numberSort[i]);
}
}
Gives output:
0
1
1
2
2
5
7
7
9
12
22
33
55

Categories

Resources