JAVA作业1

7-1 一个整数各个位上的最大数字 (30分)

编写一个类的方法,其输入参数为一个整数,输出为该整数各个位上的最大数字。

输入格式:
输入一个整数N

输出格式:
输出该整数N各个位上的最大数字

输入样例:
在这里给出一组输入。例如:

59274

输出样例:
在这里给出相应的输出。例如:

9

将每一位数都与当前最大值进行比较即可

ans:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int n;
        Scanner s=new Scanner(System.in);
        n=s.nextInt();
        int re=getMAX(n);
        System.out.println(re);
    }
    public static int getMAX(int num)
        {
            int max = 0;
            while (num != 0) {
                if (num % 10 > max) {
                    max = num % 10;
                }
                num = num / 10;
            }
            return max;
        }

}

7-2 十进制转二进制 (20分)

编写代码,要求:输入参数是一个正整数,输出该整数所对应的二进制数对应的字符串。

输入格式:
正整数

输出格式:
输入的正整数对应的二进制字符串“1001”

输入样例:
在这里给出一组输入。例如:

9

输出样例:
在这里给出相应的输出。例如:

1001

除2取余,逆序输出

ans:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
                       int a,b,i=0;
                       int c[]=new int[10000];
                       Scanner s=new Scanner(System.in);
                       a=s.nextInt();
                       if(a==1)
                           System.out.print(a);
                       else {
                           while (a / 2 != 0) {
                               b = a % 2;
                               c[i] = b;
                               i++;
                               a /= 2;
                               if (a == 1) {
                                   c[i] = 1;
                               }
                           }
                       }
                       for(;i>=0;i--)
                       {
                           System.out.print(c[i]);
                       }
                    }
}

7-3 判断回文 (20分)

编码实现:输入一个字符串,判断该字符串是否是回文(回文是指将该字符串含有的字符逆序排列后得到的字符串和原字符串相同的字符串)如果是回文,则输出“Yes”;否则输出“No”。

输入格式:
判定是否是回文的字符串

输出格式:
“Yes”或者“No”

输入样例:
在这里给出一组输入。例如:

TooooT

输出样例:
在这里给出相应的输出。例如:

Yes

ans:

import java.util.Scanner;
public class Main
{
    public static <string> void main(String[] args) {
        Scanner s=new Scanner(System.in);
        String a= s.nextLine();
        if(judge(a))
            System.out.print("Yes");
        else
            System.out.print("No");
        s.close();
    }
    public static boolean judge(String a)
    {
        char A[]=a.toCharArray();// toCharArray()将此字符串转换为新的字符数组。
        int top=0;
        int end=A.length-1;
        if(a.equals("")||a.equals(null))//equals(Object anObject) 将此字符串与指定对象进行比较。
        {
            return false;//非法输入
        }
        while(top<end)
        {
            if(A[top++]==A[end--])
                return true;
        }
         return false;
    }


}

7-4 学投资 (20分)

小白学习了一些复利投资知识,想比较一下复利能多赚多少钱(所谓复利投资,是指每年投资的本金是上一年的本金加收益。而非复利投资是指每年投资金额不包含上一年的收益,即固定投资额)。假设他每年固定投资M元(整数),每年的年收益达到P(0<P<1,double),那么经过N(整数)年后,复利投资比非复利投资多收入多赚多少钱呢?计算过程使用双精度浮点数,最后结果四舍五入输出整数(Math的round函数)。

输入格式:
M P N

输出格式:
复利收入(含本金),非复利收入(含本金),复利比非复利收入多的部分(全部取整,四舍五入)

输入样例:

10000 0.2 3

输出样例:

17280 16000 1280

简单计算问题

ans:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int M=s.nextInt();
        double P=s.nextDouble();
        int N=s.nextInt();
        long income1=Math.round(M*Math.pow((1+P),N));//复利收入
        long income2=Math.round(M+N*M*P);//非复利收入
        long c=Math.round(income1-income2);
        System.out.print(income1+" ");
        System.out.print(income2+" ");
        System.out.print(c);
    }

}

7-5 打印所有的水仙花数 (20分)

编写程序打印出所有的水仙花数。所谓"水仙花数"是指一个三位数,其各位数字立方和等于该本身。例如:153是一个水仙花数,因为153=1^3+ 5^3 +3^3。 输出的数之间用“,”(英文半角的逗号)分割。

输入格式:

输出格式:
153,370,371,407

输入样例:
在这里给出一组输入。例如:

输出样例:
在这里给出相应的输出。例如:

153,370,371,407

ans:


public class Main
{
    public static void main(String[] args) {
        int array[] = new int[10];
        int i = 0,j=1,a,b,c,d,e,f;
        for(int num=100;num<1000;num++)
        {
            a = num / 100;//百位
            b = num % 100/10;//十位
            c = num%100%10;//个位
            d = a*a*a;
            e = b*b*b;
            f = c*c*c;
            if (num==d + e + f ) {
                array[++i] = num;
                //System.out.println(num);
              }
        }
        if (j == 1) System.out.print(array[1]);
        for (j = 2; j <=i; j++) {
            System.out.print("," + array[j]);
        }
    }
}

7-6 逆序输出整数 (20分)

编写程序将整数逆序输出。如输入为9876输出为6789 Main函数中读入n个整数,输出n个整数的逆序数

输入格式:
整数个数n n个整数

输出格式:
n个整数的逆序数

输入样例:
在这里给出一组输入。例如:

3
1234
2323
1112

输出样例:
在这里给出相应的输出。例如:

4321
3232
2111

除10取余再将每一位存入到数组中输出即可

ans:

import org.omg.Messaging.SYNC_WITH_TRANSPORT;

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
            Scanner s=new Scanner(System.in);
            int a=s.nextInt();
            for(int i=0;i<a;i++) {
                int n = s.nextInt();
                myreverse(n);
            }
           // System.out.print(m);
    }
    public static void myreverse(int n)
    {
        int a[]=new int[10000];
        int i=0;
        while(n!=0)
        {
            a[++i]=n%10;
            n/=10;
        }
        for(int j=1;j<=i;j++) {
            System.out.print(a[j]);
        }
        System.out.println();
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值