初级java程序员笔试题(6)

2018-11-21 14:29

int sum=0;

for(int j=1;j<=15;j++) {

sum+=f(j); }

System.out.println(sum); } }

14.编写程序,分别用do-while和for循环计算1+1/2!+1/3!+1/4!+…的前15项的和。

for循环代码:

public class For_FactorialSum {

static int f(int x) {

if (x<=0) return 1; else

return x*f(x-1);

}

public static void main(String[]args){

double sum=0;

for(int j=1;j<=15;j++) {

sum+=1.0/f(j); }

System.out.println(sum); } }

do-while循环代码:

public class DoWhile_FactorialSum {

static int f(int x) {

if (x<=0) return 1; else

return x*f(x-1); }

public static void main(String[]args){

double sum=0; int j=1; do {

sum+=1.0/f(j); j++; }

while(j<=15);

System.out.println(sum); } }

15.编写一个程序,用选择法对数组a[]={20,10,55,40,30,70,60,80,90,100}进行从大到小的排序。

(分别采用冒泡排序、选择排序和插入排序方法)

public class SortAll {

public static void main(String[] args) {

int a[]={20,10,55,40,30,70,60,80,90,100};

System.out.println(―—-冒泡排序的结果:―);

maoPao(a);

System.out.println();

System.out.println(―—-选择排序的结果:―);

xuanZe(a);

System.out.println();

System.out.println(―—-插入排序的结果:―);

chaRu(a); }

// 冒泡排序

public static void maoPao(int[] x) {

for (int i = 0; i < x.length; i++) {

for (int j = i + 1; j < x.length; j++) {

if (x[i] > x[j]) {

int temp = x[i];

x[i] = x[j];

x[j] = temp; } } }

for (int i : x) {

System.out.print(i + ‖ ‖); } }

// 选择排序

public static void xuanZe(int[] x) {

for (int i = 0; i < x.length; i++) {


初级java程序员笔试题(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:GPRS及远传组网方法

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: