通过键盘的输入两个整型数,比入你 输入 4和3,打印如下的样式
1 1 1
1 2 1
1 2 1
1 1 1
输入3和4就打印
1 1 1 1
1 2 2 1
1 1 1 1
样式基本如下:
1 1 1 1 1 1
1 2 2 2 2 1
1 2 3 3 2 1
1 2 2 2 2 1
1 1 1 1 1 1
哪位大哥给个答案或者提示
要是行列不相等呢,怎么处理啊
[此贴子已经被作者于2007-10-25 22:11:11编辑过]
应该是这样的吧:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 2 2 2 2 1 1 2 2 2 2 1
1 1 1 1 1 1 --> 1 2 2 2 2 1 --> 1 2 3 3 2 1
1 1 1 1 1 1 1 2 2 2 2 1 1 2 2 2 2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
for(int m=0;m<=6/2-1;m++)
{
for(int i=m;i<=4-m;i++)
for(int j=m;j<=5-m;j++)
a[i][j]=m+1;
}
好久没写了不知道对不?
写了个,不知道是不是楼主想要的。
[CODE]
import java.io.*;
public class pailie {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int r, c;
r = Integer.parseInt(in.readLine());
c = Integer.parseInt(in.readLine());
int[][] a = new int[r][c];
seta(a, 0, r, c);
for (int i = 0; i < r; i++) {
for (int i1 = 0; i1 < c; i1++)
System.out.print(a[i][i1] + " ");
System.out.println();
}
}
public static void seta(int[][] aa, int k, int rr, int cc) {
for (int i = k; i < rr; i++) {
for (int j = k; j < cc; j++) {
aa[i][j] += 1;
}
}
if (rr - 1 >= k + 1 && cc - 1 >= k + 1) {
seta(aa, k + 1, rr - 1, cc - 1);
}
}
}
[/CODE]
import java.io.*;
public class Hello{
public static void main(String []args){
int r = 0;
int c = 0;
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入两个正整数,且用回车键将两数分割:");
do{
r = Integer.parseInt(in.readLine());
c = Integer.parseInt(in.readLine());
if(r <= 0||c <= 0){
System.out.println("您输入的两个数不合格,请重新输入!");
}
}
while(r <= 0 || c <= 0);
}
catch(Exception e){
e.printStackTrace();
}
int temp1;
int temp2;
int a[][] = new int[r][c];
for(int i = 0;i < r;i ++){
for(int j = 0;j < c;j ++){
temp1 = r-i-1;
temp2 = c-j-1;
a[i][j] = (new Hello()).min(i,j,temp1,temp2)+1;
}
}
for(int i = 0;i < r;i ++){
for(int j = 0;j < c;j ++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public int min(int a,int b,int c,int d){
if(a <= b && a <= c && a <= d) return a;
else if(b <= a && b <= c && b <= d) return b;
else if(c <= a && c <= b && c <= d) return c;
else if(d <= a && d <= b && d <= c) return d;
else return 0;
}
}
import java.io.*;
public class Hello{
public static void main(String []args){
int r = 0;
int c = 0;
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入两个正整数,且用回车键将两数分割:");
do{
r = Integer.parseInt(in.readLine());
c = Integer.parseInt(in.readLine());
if(r <= 0||c <= 0){
System.out.println("您输入的两个数不合格,请重新输入!");
}
}
while(r <= 0 || c <= 0);
}
catch(Exception e){
e.printStackTrace();
}
int temp1;
int temp2;
int a[][] = new int[r][c];
for(int i = 0;i < r;i ++){
for(int j = 0;j < c;j ++){
temp1 = r-i-1;
temp2 = c-j-1;
a[i][j] = (new Hello()).min(i,j,temp1,temp2)+1;
}
}
for(int i = 0;i < r;i ++){
for(int j = 0;j < c;j ++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public int min(int a,int b,int c,int d){
if(a <= b && a <= c && a <= d) return a;
else if(b <= a && b <= c && b <= d) return b;
else if(c <= a && c <= b && c <= d) return c;
else if(d <= a && d <= b && d <= c) return d;
else return 0;
}
}
a[i][j] = (new Hello()).min(i,j,temp1,temp2)+1;这句太销魂了,受教了!
可是这句是什么道理,阁下怎么想到这个算法的?
[此贴子已经被作者于2007-11-4 22:35:39编辑过]