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);
}
}
【程序33】
题目:打印出杨辉三角形(要求打印出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();
}
}
}
【程序34】 题目:输入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]);
}
}
【程序35】 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
import java.util.*;
public class Ex35 {
public static void main(String[] args) {
int i, min, max, n, temp1, temp2;
int a[];
System.out.println("输入数组的长度:");
Scanner keyboard = new Scanner(System.in);
n = keyboard.nextInt();
a = new int[n];
for (i = 0; i < n; i++) {
System.out.print("输入第" + (i + 1) + "个数据");
a[i] = keyboard.nextInt();
}
//以上是输入整个数组
max = 0;
min = 0;
//设置两个标志,开始都指向第一个数
for (i = 1; i < n; i++) {
if (a[i] > a[max])
max = i; //遍历数组,如果大于a[max],就把他的数组下标赋给max
if (a[i] < a[min])
min = i; //同上,如果小于a[min],就把他的数组下标赋给min
}
//以上for循环找到最大值和最小值,max是最大值的下标,min是最小值的下标
temp1 = a[0];
temp2 = a[min]; //这两个temp只是为了在交换时使用