1.用递归方法编写:用java语言写!

输出如下所示:
1
1 2 1
1 2 3 2 1
的代码帮我写下!
2.用一维数组输出等腰的杨辉三角。
3.下面的代码为什么二维数组的值是0呢?帮我修改下我代码同时说明下出错的原因!
public class Saddlepoints {
public static int saddlePoints(int table[][],int m,int n) {
table = new int[m][n];
int locumn=0;
int temp = 0;
for(int i = 0; i < m ; i++) {
int max = table[i][0];
for(int j = 1; j < n; j++) {
if(max < table[i][j]){
max = table[i][j];
locumn++;
}
}
temp = max;
for(int z = 0; z < m;z ++){
if(temp > table[z][locumn]){
temp = table[z][locumn];
}
}
if(temp == max) {
System.out.println("二维数组的鞍点的位置为于: \n"+"第"+i+"行第"+locumn+"列;值为:"+max);
break;
}
locumn = 0;
}
return temp;
}
public static void main(String[] args) {
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
System.out.print(table[i][j]+"\t");
}
System.out.println();
}

System.out.println(saddlePoints(table,3,3));
}

}

删除不好做,因为你是一个文件内容的删除。
可以用字符串数组来保存每一行的值,删除了之后,再重新保存进去。
可以这样做:比如说你的文件路径:c:/a.txt
1.File file = new File("c:/a.txt");
2.BufferedReader reader = new BufferedReader(file);
3.byte[] strValue = new byte[(int)file.length()];
4.String str = reader.read(strValue);//按文件大小一次读入
5.String rows = str.split("\r\n");//按换行符拆分,即数组的每一条,对应文件每一行的内容。
6.如果要删除按条件删除某一行,只需要判断字符串数组是否存在这一行了,如果存在,替换为“”即可。
7.Writer writer = new FileWriter(file);
8.
for(int i=0;i<rows.length;i++){
writer.write(rows[i]);
}

只是分析了一下,具体步骤还是你来完成了。。

RandomAccessFile raf = new RandomAccess("wenjian.txt");
char ch[] = new char[3];
char to[] = {'s','s','x'};
boolean get = false;
while(get){
raf.readChars(ch);
if(ch[0]=='d' && ch[1]=='d' && ch[2]=='s'){
get = true;
}
}
raf.seek(raf.getFilePointer() -6 );
raf.writeChars(to);

这个代码就是把wenjian.txt中出现的第一个dds修改成ssx。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-03-16
使用数组
class YangHui{
int a[][]=new int[11][11];

public void show(){
for(int j=0;j<10;j++){
if(j==0){
a[0][j]=1;
}
else{
a[0][j]=0;
}
}

for(int i=0;i<10;i++){
a[i][0]=1;
}

for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(a[i][j]!=0){
System.out.printf("%d ",a[i][j]);
}
a[i+1][j+1]=a[i][j]+a[i][j+1];
}
System.out.println();
}
}
}
public class YangHuiTest{
public static void main(String args[]){
Yanghui y=new Yanghui();
y.show();
}
}
////////////////////////////////////////////////////////////////////////
使用递归
public class YangHuiTest{
public static void main(String args[]){
for(int i=0;i<10;i++){
for(int j=0;j<=i;j++){
System.out.printf("%d ",getYang(i,j));
}
System.out.println();
}
}
public static int getYang(int m, int n){
if(n==0||m==n){
return 1;
}
else{
return getYang(m-1,n-1)+getYang(m-1,n);
}
}
}
//////////////////////////////////////////////////////////
按照计算求值
class Triangle{
int s1=1;
public int cal(int m){
return m==0||m==1?1:m*cal(m-1);
}
}
public class Test{
public static void main(String args[]){
int d;
Triangle t=new Triangle();
for(int i=0;i<10;i++){
for(int j=0;j<=i;j++){
if(i==0||i==j)
d=1;
else
d=t.cal(i)/(t.cal(j)*t.cal(i-j));
System.out.printf("%d ",d);
}
System.out.println();
}
}
}追问

老兄你下面的程序没有帮我解决!
你可以用递归方法写个代码吗?
输出如下所示:
1
1 2 1
1 2 3 2 1
的代码帮我写下!

第2个回答  2011-03-16
你那个图好像不是什么杨辉三角吧?
我按照图的意思给你写了个程序,可以运行的:
public static void main(String[] args) {
pain(5);
}
//一共输出t行
public static void pain(int t){
for(int i=1;i<=t;i++){
paine(i,t);
}
}
//输出第e行,总行数为t行
public static void paine(int e,int t){
for(int i=0;i<t-e;i++){
System.out.print(" ");
}
for(int i=1;i<=e;i++){
System.out.print(i);
}
for(int i=e-1;i>=1;i--){
System.out.print(i);
}
System.out.println();
}
输出: 1
121
12321
1234321
123454321
***************************************************************************************
你在saddlepoints方法中重新new了一个table,所以每次table被初始化为全零矩阵
改成这样就好了,
public static int saddlePoints(int table[][], int m, int n) {
int locumn = 0;
int temp = 0;
for (int i = 0; i < m; i++) {
int max = table[i][0];
for (int j = 1; j < n; j++) {
if (max < table[i][j]) {
max = table[i][j];
locumn++;
}
}
temp = max;
for (int z = 0; z < m; z++) {
if (temp > table[z][locumn]) {
temp = table[z][locumn];
}
}
if (temp == max) {
System.out.println("二维数组的鞍点的位置为于: \n" + "第" + i + "行第" + locumn
+ "列;值为:" + max);
break;
}
locumn = 0;
}
return temp;
}

public static void main(String[] args) {
int[][] table = new int[3][3];
table[0][0]=1;
table[0][1]=2;
table[0][2]=3;
table[1][0]=4;
table[1][1]=5;
table[1][2]=6;
table[2][0]=7;
table[2][1]=8;
table[2][2]=9;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(table[i][j] + "\t");
}
System.out.println();
}

System.out.println(saddlePoints(table, 3, 3));
}
相似回答
大家正在搜