zoukankan      html  css  js  c++  java
  • java基础编程题练习(二)

    1.回文数

    思路一:使用java特有解法,将原数字以字符串存储,翻转后赋值给新的字符串变量,再使用equals与原字符串进行对比

     1 import java.util.Scanner;
     2 
     3 public class huiwen {
     4     public static void main( String[] args ){
     5         System.out.println("请输入一个整数:");
     6         Scanner sc = new Scanner( System.in );
     7         int num = sc.nextInt();
     8         String str1 = String.valueOf(num);
     9         String str2 = new StringBuilder( str1 ).reverse().toString();
    10         /****
    11          * 上面的语句是先创建新对象然后在将str1翻转后的值赋给str2,对str1本身的值没有影响
    12          *
    13          * 如果将str1改为StringBuilder
    14          * String str2 = new StringBuilder( str1.reverse().toString() );
    15          * 按这个语句写,会先调用str1的reverse()方法,将str1翻转赋值给自己后在对str2赋值,对str1的值有影响
    16          */
    17         System.out.println(str1);
    18         if ( str1.equals(str2) ){
    19             System.out.println(str1 + "是一个回文数");
    20         } else {
    21             System.out.println(str1 + "不是回文数");
    22         }
    23     }
    24 }
    字符串实现回文数判定

    思路二:常规解法,将原数字从个位开始到中间位数逐位取出组成新的数字,比较是否与原数字相等(注意奇位数中间的那个要去除,不比较)

    import java.util.Scanner;
    
    public class huiwen2 {
        public static void main(String[] args){
            System.out.println("请输入一个整数a:");
            Scanner sc = new Scanner( System.in );
            int a = sc.nextInt();
            int t = 0;
            if ( a%10==0 ){ //个位为0一定不是
                System.out.println( a + "不是回文数。");
            }else {
                while( a>t ){      //将a的后半段数字逐位取出组成新数字t
                    t = t*10 + a%10;
                    a /= 10;        //从个位到中间位储位去除
                }
                if ( a==t || a==t/10 ){ //注意奇位数中间的不比较
                    System.out.println( "是回文数。");
                }else {
                    System.out.println( "不是回文数。");
                }
            }
        }
    }
    回文数常规解法

    2.输入一行字符分别统计出其英文字母,数字,空格和其他字符的个数。

    思路一:使用java字符串操作,分四次将字符串中的字母,数字,空格和三者去除,用去除前的字符串长度减去去除后的长度得到相应字符的个数。

     1 import java.util.Scanner;
     2 
     3 public class countNumOfString {
     4     public static int letterCount(String s){
     5         //计量字母的个数
     6         int count;
     7         count = s.length() - s.replaceAll("[a-zA-Z]","").length();
     8         //调用replaceAll方法将所有字母去掉
     9         return count;
    10     }
    11 
    12     public static int numCount(String s){
    13         //计量数字的个数
    14         int count;
    15         count = s.length() - s.replaceAll("\d","").length();
    16         //   表示转义字符,d表示0~9的数字,因为java中没有数字的字符,所以需要转义
    17         return count;
    18     }
    19 
    20     public static int spaceCount(String s) {
    21         //计量空格的个数
    22         int count;
    23         count = s.length() - s.replaceAll(" ", "").length();
    24         return count;
    25     }
    26 
    27     public static int otherCount(String s){
    28         //计量其他字符的个数
    29         int count;
    30         count = s.length() - s.replaceAll("[^a-zA-Z\d ]","").length();
    31         //  ^非运算,将非字母,数字,空格的字符去除
    32         return count;
    33     }
    34 
    35     public static void main(String[] args){
    36         Scanner sc = new Scanner(System.in);
    37         System.out.println("请输入一个字符串:");
    38         String s = sc.nextLine();//整行读取,如果使用next则无法读取空格
    39         int a,b,c,d;
    40         countNumOfString Count = new countNumOfString();
    41         a = Count.letterCount(s);
    42         b = Count.numCount(s);
    43         c = Count.spaceCount(s);
    44         d = Count.otherCount(s);
    45         System.out.println("字母的个数为:" + a);
    46         System.out.println("数字的个数为:" + b);
    47         System.out.println("空格的个数为:" + c);
    48         System.out.println("其他字符的个数为:" + d);
    49     }
    50 }
    统计字符个数

    思路二:使用常规方法,将输入字符存入字符型数组,在循环体中与ASCII码对照计数,循环结束后输出各个计数变量。

     1 import java.util.Scanner;
     2 
     3 public class countNumOfString {
     4     public static void main(String[] args){
     5         Scanner sc = new Scanner(System.in);
     6         System.out.println("请输入一个字符串:");
     7         int numcount=0,lettercount=0,spcount=0,othercount=0;
     8         String str = sc.nextLine();
     9         char[] arr = str.toCharArray();//字符串转化为数组
    10         for (int i=0;i<arr.length;i++){ 
    11             if ( arr[i]>='0' && arr[i]<='9'){   //判断是否为数字
    12                 numcount++;
    13             }else if ( (arr[i]>='a' && arr[i]<='z' )||(arr[i]>='A' && arr[i]<='Z')){
    14                 //判断是否为字母
    15                 lettercount++;
    16             }else if ( arr[i]==' ' ){  //判断是否为空格
    17                 spcount++;
    18             }else {
    19                 othercount++;
    20             }
    21         }
    22         System.out.println("数字的个数为:" + numcount);
    23         System.out.println("字母的个数为:" + lettercount);
    24         System.out.println("空格的个数为:" + spcount);
    25         System.out.println("其他字符的个数为:" + othercount);
    26     }
    27 }
    ASCII码判别计数

    3.有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数

    4.计算字符串中字串出现的次数。

    5.输入数组,最大的与第一个元素交换,最小的与最后一个元素交换。

    6.编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n

    7.一球从h米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第n次落地时,共经过多少米?第n次反弹多高?

    8.海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

    9.报到3的人退出圈子

    10.企业发放的奖金根据利润提成。 

    利润(I)低于或等于10万元时,奖金可提10%;

    利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;

    20万到40万之间时,高于20万元的部分,可提成5%; 

    40万到60万之间时高于40万元的部分,可提成3%;

    60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,

    从键盘输入当月利润I,求应发放奖金总数?(倒过来才能激励人啊)

    11.求s=a+aa+aaa+aaaa+aa...a的值

    12.有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

    13.一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

    14.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和

    15.求1+2!+3!+...+20!的和

    16.利用递归方法求5!

    17.两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。

    有人向队员打听比赛的名单。

    a说他不和x比,c说他不和x,z比,

    请编程序找出三队赛手的名单

     

  • 相关阅读:
    <HTTP>ASI实现的注册方法:利用http的get和post两种方式
    <Ruby>社区服务端启动流程
    <iOS>ASIHTTPRequest和ASIDownloadCache实现本地缓存
    <iOS>关于Xcode上的Other linker flags
    <HTTP>ASI实现的登陆方法
    【pool drain】和【pool release】区别
    <UI>TableViewCELL长按事件
    <UI>UIView的autoresizingMask属性
    <UI>自定义UITableView的右侧索引
    <cocos2D>ccLabel相关
  • 原文地址:https://www.cnblogs.com/edward-life/p/10549094.html
Copyright © 2011-2022 走看看