借花獻(xiàn)佛
科爾沁右翼中ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
/**
*?實(shí)現(xiàn)二維數(shù)組的轉(zhuǎn)置
*?@author?HAN
*
*/
public?class?transposition_Arrays2D_ch6_4?{
final?static?double?PI=3.1415;
public?static?void?main(String[]?args)?{
/*StaticTest?st1=new?StaticTest();
StaticTest?st2=new?StaticTest();
st1.method2("HAN");*/
/*****定義要用于轉(zhuǎn)置的二維數(shù)組*******/
int?arr2D[][]={{1,2,3},{4,5,6},{7,8,9}};
/*****構(gòu)造結(jié)果新二維數(shù)組用于存放轉(zhuǎn)置結(jié)果*******/
/*定義結(jié)果數(shù)組變量,注意?一定要先開辟一個內(nèi)存,
否則只是地址傳遞,也就是說兩個數(shù)組名實(shí)際上指向的是同一塊內(nèi)存*/
//而構(gòu)造二維數(shù)組可以為維度來進(jìn)行,不一定是一個矩陣,即每一行的長度不一定相同
int?result_arr[][]=new?int[arr2D.length][];//先實(shí)現(xiàn)第一維
for(int?i=0?;?iarr2D.length;i++){?//再實(shí)現(xiàn)第二維
result_arr[i]=new?int[arr2D[i].length];
}
// int?result_arr[][]=Arrays.copyOf(arr2D,?arr2D.length);
//上面這個命令行行不通!
/*****輸出用于轉(zhuǎn)置的二維數(shù)組*******/
for?(int?x[]:arr2D){
for(int?e:x){
System.out.print(e+"?");
}
System.out.println();
}
System.out.println();
/*******進(jìn)行元素倒置******/
for(int?i=0?;?iarr2D.length;i++){
for(int?j=0;?jarr2D[i].length;j++){
result_arr[j][i]=arr2D[i][j];?//轉(zhuǎn)置核心
}
}
/*****show?the?result?in?the?result?matrix*******/
for?(int?x[]:result_arr){
for(int?e:x){
System.out.print(e+"?");
}
System.out.println();
}
}
}
//import?java.util.Arrays;
//public?class?transposition_Arrays2D?{
//
// public?static?void?main(String[]?args)?{
// int?arr2D[][]={{1,2,3},{4,5,6},{7,8,9}};
// /*定義結(jié)果數(shù)組變量,注意?一定要先開辟一個內(nèi)存,
// 否則只是地址傳遞,也就是說兩個數(shù)組名實(shí)際上指向的是同一塊內(nèi)存*/
// int?result_arr[][]=new?int[arr2D.length][];
// for(int?i=0?;?iarr2D.length;i++){
// result_arr[i]=new?int[arr2D[i].length];
// }
//
//????????//?進(jìn)行元素倒置
//????????for(int?i=0?;?iarr2D.length;i++){
//???????? for(int?j=0;?jarr2D[i].length;j++){
//???????? result_arr[j][i]=arr2D[i][j];
//???????? }
//????????}
//????????
//????????//?show?the?result?in?matrix
//????????for?(int?x[]:result_arr){
//???????? for(int?e:x){
//???????? System.out.print(e);
//???????? }
//???????? System.out.println();
//????????}
//????????
// }
//
//}
Java程序:
public?class?Main?{
public?static?void?main(String[]?args)?{
int?LEN?=?8;
int[][]?arr?=?new?int[LEN][LEN];
int?i?=?0,?j?=?0;
int?n?=?1;
arr[i][j]?=?n;
while(n??LEN?*?LEN)?{
while(j+1LEN??arr[i][j+1]==0){
j++;
arr[i][j]?=?++n;
}
while(i+1LEN??arr[i+1][j]==0){
i++;
arr[i][j]?=?++n;
}
while(j-1=0??arr[i][j-1]==0){
j--;
arr[i][j]?=?++n;
}
while(i-1=0??arr[i-1][j]==0){
i--;
arr[i][j]?=?++n;
}
}
for(i=0;?iLEN;?i++)?{
for(j=0;?jLEN;?j++){
System.out.printf("%4d",?arr[i][j]);
}
System.out.println();
}
}
}
運(yùn)行結(jié)果:
1???2???3???4???5???6???7???8
28??29??30??31??32??33??34???9
27??48??49??50??51??52??35??10
26??47??60??61??62??53??36??11
25??46??59??64??63??54??37??12
24??45??58??57??56??55??38??13
23??44??43??42??41??40??39??14
22??21??20??19??18??17??16??15
/**
* @author 熙熙
* @2011-4-20
*/
public class JuZhenZhuanZhi {
/**
* @param args
*/
public static void main(String[] args) {
int a[][]={
{11,12,13}
,{21,22,23}
,{31,32,33}
};
System.out.println("轉(zhuǎn)置前的矩陣:");
print(a);
zhuanZhi(a);
System.out.println("轉(zhuǎn)置后的矩陣:");
print(a);
}
public static void zhuanZhi(int a[][]){
for(int i=0;ia.length;i++){
for(int j=i+1;ja[i].length;j++){
int tem = a[i][j];
a[i][j] = a[j][i];
a[j][i] = tem;
}
}
}
public static void print(int a[][]){
for(int i=0;ia.length;i++){
int j=0;
for(;ja[i].length-1;j++){
System.out.print(a[i][j]+",");
}
System.out.println(a[i][j]);
}
}
}
import java.io.*;public class RingDemo {
public static void main(String[] args) {
String strIn = "";
System.out.print("請輸入矩陣的行列數(shù):");
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader buff = new BufferedReader(input);
try {
strIn = buff.readLine();
} catch (IOException e) {
System.out.println(e.toString());
}
int int1 = Integer.parseInt(strIn);
int n = int1;
System.out.println("這是行列數(shù)為" + n + "的螺線型數(shù)組:");
int intA = 1; // 初始化
int[][] array = new int[n][n];
int intB;
if (n % 2 != 0) {
intB = n / 2 + 1; // 奇數(shù)時i循環(huán)次數(shù)
} else
intB = n / 2; // 偶數(shù)時i循環(huán)次數(shù)
for (int i = 0; i intB; i++) { // 從外到里循環(huán)
// 從左到右橫的開始
for (int j = i; j n - i; j++) {
array[i][j] = intA;
intA++;
}
// 從上到下縱
for (int k = i + 1; k n - i; k++) {
array[k][n - i - 1] = intA;
intA++;
}
// 從右到左橫
for (int l = n - i - 2; l = i; l--) {
array[n - i - 1][l] = intA;
intA++;
}
// 從下到上縱
for (int m = n - i - 2; m i; m--) {
array[m][i] = intA;
intA++;
}
}
// 輸出數(shù)組
for (int i = 0; i n; i++) {
for (int j = 0; j n; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
} }
}