zoro's hub
BAN USERpublic int[][] rotateMatrix(int[][] x){
int[][] m = new int[x[0].length][x.length];
for(int i=0; i<x[0].length; i++){
for(int j=0; j<x.length; j++){
// System.out.format("%4d",m[i][j]=x[j][x[0].length-1-i]); //rotate by 90 degree anticlockwise.
System.out.format("%4d",m[i][j]=x[x.length-1-j][i]); //rotate by 90 degree clockwise.
}
System.out.println();
}
return m;
}
Here's the java program to rotate a matrix by 90 degree (clockwise/anticlockwise)
public class Matrix {
static int[][] getMcrossN(int m, int n){
int[][] a = new int[m][n];
int cnt = 1;
for(int i=0; i<m; i++){
for(int j=0;j<n; j++){
System.out.format("%4d",a[i][j]=cnt++);
}
System.out.println();
}
System.out.println("\n\n");
return a;
}
static int[][] rotateMatrix(int[][] x){
int[][] m = new int[x[0].length][x.length];
for(int i=0; i<x[0].length; i++){
for(int j=0; j<x.length; j++){
// System.out.format("%4d",m[i][j]=x[j][x[0].length-1-i]); //rotate by 90 degree anticlockwise.
System.out.format("%4d",m[i][j]=x[x.length-1-j][i]); //rotate by 90 degree clockwise.
}
System.out.println();
}
return m;
}
/**
* @param args
*/
public static void main(String[] args) {
rotateMatrix(getMcrossN(3, 4));
}
}
public class Matrix {
static int[][] getMcrossN(int m, int n){
int[][] a = new int[m][n];
int cnt = 1;
for(int i=0; i<m; i++){
for(int j=0;j<n; j++){
System.out.format("%4d",a[i][j]=cnt++);
}
System.out.println();
}
System.out.println("\n\n");
return a;
}
static int[][] rotateMatrix(int[][] x){
int[][] m = new int[x[0].length][x.length];
for(int i=0; i<x[0].length; i++){
for(int j=0; j<x.length; j++){
// System.out.format("%4d",m[i][j]=x[j][x[0].length-1-i]); //rotate by 90 degree anticlockwise.
System.out.format("%4d",m[i][j]=x[x.length-1-j][i]); //rotate by 90 degree clockwise.
}
System.out.println();
}
return m;
}
/**
* @param args
*/
public static void main(String[] args) {
rotateMatrix(getMcrossN(3, 4));
}
}
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class PrimeTest2 {
/**
* @param args
*/
public static void main(String[] args) {
int count=0, p, numberOfPrimes;
List<Integer> primeList = new LinkedList<Integer>();
System.out.print("Enter the number of primes you want to get: ");
Scanner scan = new Scanner(System.in);
while(true){
if(!scan.hasNextInt()){
System.out.print("please enter only integer: ");
scan.next();
continue;
}else{
numberOfPrimes = scan.nextInt();
break;
}
}
p=1;
while(count<numberOfPrimes){
if(checkPrime(p)){
primeList.add(p);
count++;
}
p++;
}
System.out.println("First "+numberOfPrimes+" primes: "+primeList);
}
public static boolean checkPrime(int x){
if(x==1 || x ==2) return true;
if(x%2==0) return false;
int k=3;
int a=x/k+x%k;
while(k<=a){
if(x%k==0){
return false;
}else{
k+=2;
a = x/k+x%k;
}
}
return true;
}
}
one approach is using co-ordinate system. Based on degree of rotation, rotate the co-ordinates and the shift the origin.
- zoro's hub July 12, 2013public int[][] rotateMatrix(int[][] x){
int[][] m = new int[x[0].length][x.length];
for(int i=0; i<x[0].length; i++){
for(int j=0; j<x.length; j++){
// System.out.format("%4d",m[i][j]=x[j][x[0].length-1-i]); //rotate by 90 degree anticlockwise.
System.out.format("%4d",m[i][j]=x[x.length-1-j][i]); //rotate by 90 degree clockwise.
}
System.out.println();
}
return m;
}