Forum Comments

Class 3 hw 5/2
In Java Question Bank
stephenzhuang2004
May 30, 2020
import java.util.*; class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); boolean board[][] = new boolean [8][8]; for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ board[i][j] = true; } } System.out.println("input r, which indicates the number of rows that have tiles turned on"); int r = in.nextInt(); for(int i = 0; i < r; i++){ System.out.println("input coordinate of tiles turned off"); int turned_on = in.nextInt(); String str = turned_on + ""; int Y = turned_on / ((int)Math.pow(10,str.length()-1)); for(int k = str.length()-2 ; k >= 0; k--){ int X = 0; if(k!=0){ X = (turned_on / (int)Math.pow(10,k))%10; } else if(k==0){ X = turned_on % 10; } board[Y-1][X-1] = false; System.out.println(" " + Y + "," + X); } } System.out.println("input coordinate of center tile"); int t = in.nextInt(); int y = t / 10; int x = t % 10; int X_front = x - 2; int X_back = x + 2; if(x < 3){ X_front = 1; } if(x > 6){ X_back = 8; } int Y_down = y - 2; int Y_up = y + 2; if(y < 3){ Y_down = 1; } if(y > 6){ Y_up = 8; } for(int l = Y_down - 1; l <= Y_up - 1; l++){ if(l == Y_down - 1 || l == Y_up - 1){ int j = x -1; board[l][j] = !board[l][j]; } else if( l == Y_down || l == Y_up - 2){ if(x > 6){ for(int j = X_front; j < X_back; j++){ board[l][j] = !board[l][j]; } } else if(x < 3){ for(int j = 0; j <= X_back - 2; j++){ board[l][j] = !board[l][j]; } } else{ for(int j = X_front; j <= X_back - 2; j++){ board[l][j] = !board[l][j]; } } } else if( l == y - 1){ for(int j = X_front - 1; j <= X_back - 1; j++){ board[l][j] = !board[l][j]; } } } int count = 0; for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ if(board[i][j] == false){ count++; } } } System.out.println(count); } }
0
0
1.7 Workshop: what is your algorithm?
In Introduction to Java
stephenzhuang2004
Apr 23, 2020
3. Write an algorithm to create a tile pattern composed of black and white tiles, with a fringe of black tiles all around and two or three black tiles in the center, equally spaced from the boundary. Amount of rows = x Amount of columns = y Amount of spacing between side tiles and center tiles = z Repeat the following step for y times Print out one black tile Move to the next row Repeat the following steps for (x-3)/2 times Print out one black tile Repeat the following steps for y-2 times Print out one white tile Print out one black tile Move to next row Repeat the following for x-2-(2*((x-3)/2)) times  Print out one black tile  Repeat the following step for z times Print out one white tile Repeat the following step for x-2-2*z times Print out one black tile Repeat the following step for z times Print out one white tile Print out one black tile  Move to next row Repeat the following steps for (x-3)/2 times Print out one black tile Repeat the following steps for y-2 times Print out one white tile Print out one black tile Move to next row Repeat the following step for y times Print out one black tile 4. Given an integer, print out each digit of that integer. user input = integer Amount of digits in the integer = #digits Repeat the following steps for #digits times Print out the last digit of the most current form of integer cut the last digit (just counted) off from the most current form of integer 5. Given a 16-digit credit card number, print out the sum of all digits. sum of all digits = sum 16-digit credit card number = integer Repeat the following steps for 16 times add the last digit of the most current form of integer to sum cut the last digit (just counted) off from the most current form of integer Print out sum
1
0
s
stephenzhuang2004
More actions