12道java经典入门算法题!

12道java经典入门算法题!
【程序1】   题目:将一个数组逆序输出。  
程序分析:用第一个与最后一个交换。  
其实,用循环控制变量更简单:
for(int k=11;k>=1;k–)
System.out.print(myarr[k]+",");

【程序2】   题目:取一个整数a从右端开始的4~7位。  
程序分析:可以这样考虑:  
(1)先使a右移4位。  
(2)设置一个低4位全为1,其余全为0的数。可用(0 < <4)  
(3)将上面二者进行&运算。

public class Ex32 {
public static void main(String[] args)
{
int a=0;
long b=18745678;
a=(int) Math.floor(b % Math.pow(10,7)/Math.pow(10, 3));
System.out.println(a);
}
}
【程序3】  
题目:打印出杨辉三角形(要求打印出10行如下图)  
1.程序分析:  
1  
1   1  
1   2   1  
1   3   3   1  
1   4   6   4   1  
1   5   10   10   5   1  
public class Ex33 {
public static void main(String args[]){
int i,j;
int a[][];
a=new int[8][8];
for(i=0;i<8;i++){
a[i][i]=1;
a[i][0]=1;
}
for(i=2;i<8;i++){
for(j=1;j<=i-1;j++){
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
for(i=0;i<8;i++){
for(j=0;j<i;j++){
System.out.printf(" "+a[i][j]);
}
System.out.println();
}
}
}

【程序4】   题目:输入3个数a,b,c,按大小顺序输出。  
1.程序分析:利用指针方法。  
public class Ex34 {
public static void main(String[] args)
{
int []arrays = {800,56,500};
for(int i=arrays.length;–i>=0;)
{
for(int j=0;j<i;j++)
{
if(arrays[j]>arrays[j+1])
{
int temp=arrays[j];
arrays[j]=arrays[j+1];
arrays[j+1]=temp;
}
}
}
for(int n=0;n<arrays.length;n++)
System.out.println(arrays[n]);
}

}
【程序5】   题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。  
import java.util.*;
public class Ex35 {
public static void main(String[] args) {
int i, min, max, n, temp1, temp2;
int a[];
Syst

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值