前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >编程题1-6答案

编程题1-6答案

作者头像
三哥
发布2018-06-15 13:41:59
4960
发布2018-06-15 13:41:59
举报
文章被收录于专栏:java工会java工会
  1. 用冒泡排序方法实现对整数数组的排序 public class Test { public void bubbleSort(int[] arr) { int temp;//定义一个临时变量 for(int i=0;i<arr.length-1;i++){//冒泡趟数 for(int j=0;j<arr.length-i-1;j++){ //如果顺序不对,则交换两个元素 if(arr[j+1]<arr[j]){ temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } public static void main(String[] args) { Test t = new Test(); int arr[] = new int[]{13,26,22,22,35,18}; t.bubbleSort(arr); System.out.println(Arrays.toString(arr)); } }
  2. 编程求一元二次方程的根 public class Test { /** * △=b^2-4ac的值,若△小于0,一元二次方程无根.若△等于0,一元二次方程有两个相等的根. * 若△大于0,一元二次方程有两个不相等的实数根 */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("输入2次方的系数"); int a = sc.nextInt(); System.out.println("输入1次方的系数"); int b = sc.nextInt(); System.out.println("输入0次方的系数"); int c = sc.nextInt(); if ((b * b - 4 * a * c) < 0) { // 判断方程是否有解 System.out.println("方程无解!"); return; } else { System.out.println("方程有解!"); } double x1 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 * a; double x2 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 * a; System.out.println("根分别是 " + x1 + "\t" + x2); } }
  3. 输入三个正数判断能否构成三角形 public class Test { public static void main(String[] args) { int a; int b; int c; System.out.println("请输入三个正整数:"); Scanner in = new Scanner(System.in); a = in.nextInt(); b = in.nextInt(); c = in.nextInt(); if (a <= 0 || b <= 0 || c <= 0) { System.out.println("输入的必须是正整数!"); } if ((a + b) > c && (a + c) > b && (b + c) > a) { System.out.println("能构成三角形!"); } else { System.out.println("不能构成三角形!"); } } }
  4. 编写程序,从键盘输入一个 0~99999 之间的任意数,判断输入的数是几位数 public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入一个0~99999 之间的任意数"); int number = sc.nextInt(); if (number / 10000 >= 1 && number / 10000 < 10) { System.out.println(number + "\t是5位数"); } else if (number / 1000 >= 1) { System.out.println(number + "\t是4位数"); } else if (number / 100 >= 1) { System.out.println(number + "\t是3位数"); } else if (number / 10 >= 1) { System.out.println(number + "\t是2位数"); } else if (number / 1 >= 1) { System.out.println(number + "\t是1位数"); } } }
  5. 编写程序,输出 200~500 之间的所有素数 public class Test { public static void main(String[] args) { int num = 200; while (num <= 500) { boolean tag = true; //素数标记 for (int d = 2; d <= num - 1; d++) { if (num % d == 0) { tag = false; break; } } if (tag) { //如果是素数 System.out.println(num); } num++; } } }
  6. 编写程序解决“百钱买百鸡”问题。公鸡五钱一只,母鸡三钱一只,小鸡一钱三只,现有百钱欲买百鸡,共有多少种买法 public class Test { public static void main(String[] args) { /* 、编写程序解决“百钱买百鸡”问题。 * 公鸡五钱一只,母鸡三钱一只, * 小鸡 一钱三只, * 现有百钱欲买百鸡,共有多少种买法? */ for (int g = 0; g <= 20; g++) { for (int m = 0; m <= 33; m++) { for (int x = 0; x <= 100 - g - m; x++) { if (x % 3 == 0 && 5 * g + m * 3 + x / 3 == 100 && g + m + x == 100) { System.out.println("公鸡" + g + "只母鸡" + m + "只小鸡" + x + "只"); } } } } } }
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-04-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 java工会 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档