黑马程序员技术交流社区

标题: java基础编程题 ~~版主説不要带答案~~这次没答案 [打印本页]

作者: 柳超    时间: 2014-8-12 22:59
标题: java基础编程题 ~~版主説不要带答案~~这次没答案
【程序3】
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。



【程序4】
题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数n,重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
【程序5】
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
程序分析:(a>b)?a:b这是条件运算符的基本例子。
【程序6】
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
程序分析:利用辗除法。
【程序7】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
程序分析:利用while语句,条件为输入的字符不为'\n'.

有需要答案的直接M我吧!或者参考大家的代码!



作者: 柳超    时间: 2014-8-12 23:01
自顶一下 喜欢的朋友给个评分,需要技术分考黑马!谢谢
作者: 柳超    时间: 2014-8-12 23:18
要 25个技术分啊
作者: ZhangYL    时间: 2014-8-12 23:27
我也缺黑马币= =
作者: Faith-    时间: 2014-8-12 23:35
顶一个   
作者: 柳超    时间: 2014-8-12 23:47
ZhangYL 发表于 2014-8-12 23:27
我也缺黑马币= =

加油吧! 发点有质量的东西!
作者: 柳超    时间: 2014-8-13 09:02
  1. public class Prog3{
  2.         public static void main(String[] args){
  3.                 for(int i=100;i<1000;i++){
  4.                         if(isLotus(i))
  5.                            System.out.print(i+" ");
  6.                 }
  7.                 System.out.println();
  8.         }
  9.         //判断水仙花数
  10.         private static boolean isLotus(int lotus){
  11.                 int m = 0;
  12.                 int n = lotus;
  13.                 int sum = 0;
  14.                 m = n/100;
  15.                 n  -= m*100;
  16.                 sum = m*m*m;
  17.                 m = n/10;
  18.                 n -= m*10;
  19.                 sum += m*m*m + n*n*n;
  20.                 if(sum==lotus)
  21.                         return true;
  22.                 else
  23.                         return false;
  24.                 }
  25. }
复制代码

作者: 柳超    时间: 2014-8-13 09:03
  1. public class Prog4{
  2.         public static void main(String[] args){
  3.                 int n = 13;
  4.                 decompose(n);
  5.         }
  6.         private static void decompose(int n){
  7.                 System.out.print(n+"=");
  8.                 for(int i=2;i<n+1;i++){
  9.                         while(n%i==0 && n!=i){
  10.                                 n/=i;
  11.                                 System.out.print(i+"*");
  12.                         }
  13.                         if(n==i){
  14.                                 System.out.println(i);
  15.                                 break;
  16.                         }
  17.                 }
  18.         }
  19. }
复制代码

作者: 柳超    时间: 2014-8-13 09:04
public class Prog6{
        public static void main(String[] args){
                int m,n;
                try{
                        m = Integer.parseInt(args[0]);
                        n = Integer.parseInt(args[1]);
                }catch(ArrayIndexOutOfBoundsException e){
                        System.out.println("输入有误");
                        return;
                }
                max_min(m,n);
        }
        //求最大公约数和最小公倍数
        private static void max_min(int m, int n){
                int temp = 1;
                int yshu = 1;
                int bshu = m*n;
                if(n<m){
                        temp = n;
                        n = m;
                        m = temp;
                }
                while(m!=0){
                        temp = n%m;
                        n = m;
                        m = temp;
                }
                yshu = n;
                bshu /= n;
                System.out.println(m+"和"+n+"的最大公约数为"+yshu);
                System.out.println(m+"和"+n+"的最小公倍数为"+bshu);
        }
}

作者: 柳超    时间: 2014-8-13 09:05
import java.util.Scanner;
public class Prog7_1{
        public static void main(String[] args){
                System.out.print("请输入一串字符:");
                Scanner scan = new Scanner(System.in);
                String str = scan.nextLine();//将一行字符转化为字符串
                scan.close();
                count(str);
        }
        //统计输入的字符数
        private static void count(String str){
                String E1 = "[\u4e00-\u9fa5]";//汉字
                String E2 = "[a-zA-Z]";
                String E3 = "[0-9]";
                String E4 = "\\s";//空格
                int countChinese = 0;
                int countLetter = 0;
                int countNumber = 0;
                int countSpace = 0;
                int countOther = 0;
                char[] array_Char = str.toCharArray();//将字符串转化为字符数组
                String[] array_String = new String[array_Char.length];//汉字只能作为字符串处理
                for(int i=0;i<array_Char.length;i++)
                  array_String[i] = String.valueOf(array_Char[i]);
                //遍历字符串数组中的元素
                for(String s:array_String){
                        if(s.matches(E1))
                          countChinese++;
                        else if(s.matches(E2))
                          countLetter++;
                        else if(s.matches(E3))
                          countNumber++;
                        else if(s.matches(E4))
                          countSpace++;
                        else
                          countOther++;
                }
                System.out.println("输入的汉字个数:"+countChinese);
                System.out.println("输入的字母个数:"+countLetter);
                System.out.println("输入的数字个数:"+countNumber);
                System.out.println("输入的空格个数:"+countSpace);
                System.out.println("输入的其它字符个数:"+countSpace);
        }
}

作者: 柳超    时间: 2014-8-13 09:06
  1. import java.util.*;
  2. public class Prog7_2{
  3.         public static void main(String[] args){
  4.           System.out.println("请输入一行字符:");
  5.           Scanner scan = new Scanner(System.in);
  6.           String str = scan.nextLine();
  7.           scan.close();
  8.           count(str);
  9.         }
  10.         //统计输入的字符
  11.         private static void count(String str){
  12.                 List<String> list = new ArrayList<String>();
  13.                 char[] array_Char = str.toCharArray();
  14.                 for(char c:array_Char)
  15.                   list.add(String.valueOf(c));//将字符作为字符串添加到list表中
  16.                 Collections.sort(list);//排序
  17.                 for(String s:list){
  18.                         int begin = list.indexOf(s);
  19.                         int end = list.lastIndexOf(s);
  20.                         //索引结束统计字符数
  21.                         if(list.get(end)==s)
  22.                           System.out.println("字符‘"+s+"’有"+(end-begin+1)+"个");
  23.                 }
  24.         }
  25. }
复制代码

作者: wfaly    时间: 2014-8-13 10:45
支持下楼主....
作者: _童金辉    时间: 2014-8-13 11:15
顶一个                                                      
作者: 想做开发    时间: 2014-8-13 12:03
学习了哈
作者: @for    时间: 2014-8-13 13:00
先暂时留一个标记。。
作者: 641283812    时间: 2014-8-13 13:22
学习下。
作者: 董帅军    时间: 2014-8-13 17:34
支持啊,希望多来些题啊
作者: 酋长第一    时间: 2014-8-13 22:30
水仙花的
class Pratices{
        public static void main(String[] args){
                for(int x=100;x<=999;x++){
                        int ge=x%10;
                        int shi=x/10%10;
                        int bai=x/100%10;
                        if(x==(ge*ge*ge+shi*shi*shi+bai*bai*bai)){
                        System.out.println(x);
                        }
                }
        }
}
作者: 酋长第一    时间: 2014-8-13 22:31
我晕,写完才看到有答案的啊。
作者: 冒牌高手    时间: 2014-8-14 07:47
嵌套

  1. import java.util.Scanner;
  2. public class Chengjipaixu {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) {
  7.                 Scanner s=new Scanner(System.in);
  8.                 System.out.println("please input the score");
  9.                 double d=s.nextDouble();
  10.                 String str="";
  11.                 str=(d>=90)?"A":((d>=60)?"B":"C");
  12.                 System.out.println("the score is in "+str);
  13.         }

  14. }
复制代码



求质数

  1. import java.util.Scanner;
  2. public class Shuixianhua {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) {
  7.                 Scanner s=new Scanner(System.in);
  8.                 System.out.println("please input your num:");
  9.                 int num=s.nextInt();
  10.                 System.out.println("the num you input is:"+num);
  11.                
  12.                 fenJie(num);
  13.                
  14.                
  15.         }
  16.         public static void fenJie(int num)
  17.         {
  18.                 int n=num;
  19.                 System.out.print(num+"=");
  20.                 while (n!=1) {
  21.                         for (int i = 2; i <=n; i++) {
  22.                                
  23.                                 if (n%i==0) {
  24.                                         System.out.print(i);       
  25.                                         n=n/i;
  26.                                         //id=i;
  27.                                         break;
  28.                                        
  29.                                 }
  30.                                
  31.                         }
  32.                         if (n!=1) {
  33.                                
  34.                                 System.out.print("*");
  35.                         }
  36.                        
  37.                 }
  38.                 //System.out.println(n);
  39.         }

  40. }
复制代码

作者: 柳超    时间: 2014-8-16 00:15
酋长第一 发表于 2014-8-13 22:31
我晕,写完才看到有答案的啊。

就是要自己写啊 !
这个是老师给的题就是要我们写的   我发出来给大家做做
作者: liang926    时间: 2014-8-16 00:24
好好学习学习!
作者: 塞肥肥塞牙人    时间: 2014-9-6 22:26
学习了,继续哈
作者: baiiiu    时间: 2014-9-7 09:31
额 水仙花数运行不出结果来
作者: 柳超    时间: 2014-9-7 18:20
baiiiu 发表于 2014-9-7 09:31
额 水仙花数运行不出结果来
  1. public class Prog3{
  2.         public static void main(String[] args){
  3.                 for(int i=100;i<1000;i++){
  4.                         if(isLotus(i))
  5.                            System.out.print(i+" ");
  6.                 }
  7.                 System.out.println();
  8.         }
  9.         //判断水仙花数
  10.         private static boolean isLotus(int lotus){
  11.                 int m = 0;
  12.                 int n = lotus;
  13.                 int sum = 0;
  14.                 m = n/100;
  15.                 n  -= m*100;
  16.                 sum = m*m*m;
  17.                 m = n/10;
  18.                 n -= m*10;
  19.                 sum += m*m*m + n*n*n;
  20.                 if(sum==lotus)
  21.                         return true;
  22.                 else
  23.                         return false;
  24.                 }
  25. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2