java作业一

1-1 一个整数各个位上的最大数字

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

输入格式:

输入一个整数N

输出格式:

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

输入样例:

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

59274 

输出样例:

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

9

import java.util.Scanner;

public class Main {
        public static void main(String[] args) {
            int n;
            Scanner scanner = new Scanner(System.in);
            n=scanner.nextInt();
            int max = 0;
            while(n!=0) {
                if(n%10 > max)
                
                  max=n%10; 
                  n/=10;
                
               
                
            }
            System.out.println(max);
        }
}

1-2 十进制转二进制

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

输入格式:

正整数

输出格式:

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

输入样例:

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

9

输出样例:

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

1001
import java.util.Scanner;

public class Main {
        public static void main(String[] args) {
        	int n,k=0;
    		int []a = new int [100000];
    		Scanner scanner = new Scanner(System.in);
    		n = scanner.nextInt();
    		if(n==0)
    			System.out.println(0);
    		else
    		{
    			while(n!=0)
    			{
    				a[k++] = n%2;
    				n/=2;
    			}
    			for(int i = k-1 ;i >= 0; i--)
    			{
    				System.out.print(a[i]);
    			}
    			System.out.println();
    		}
      	 
      	 
		}
}

1-3 判断回文

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

输入格式:

判定是否是回文的字符串

输出格式:

“Yes”或者“No”

输入样例:

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

TooooT

输出样例:

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

Yes
import java.util.*;
public class Main{
	
	public static void main(String argc[])
	{
		Scanner scan = new Scanner(System.in);
		String s = scan.nextLine();
		String revers = "";
		
		for(int i = s.length()-1; i >= 0 ; i--)
		{
			revers += s.charAt(i);
		}
		if(revers.equals(s))    //revers.equals(s) 表达式的返回值是一个布尔类型,如果两个对象的内容相等,则返回 true,否则返回 false。


		{
			System.out.println("Yes");
		}
		else 
		{
			System.out.println("No");
		}
	}
	
}

1-4学投资

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

输入格式:

M P N

输出格式:

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

输入样例:

10000 0.2 3

输出样例:

17280 16000 1280
import java.util.*;
public class Main{
	
	public static void main(String argc[])
	{
		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+M*N*P);//非复利收入
		long c=Math.round(income1-income2);
        System.out.print(income1+" ");
        System.out.print(income2+" ");
        System.out.print(c);
	}
}

1-5打印所有的水仙花数

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

输入格式:

输出格式:

153,370,371,407

输入样例:

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

输出样例:

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

153,370,371,407

public class Main{
	
	public static void main(String argc[])
	{
		int[] s=new int[10];
		int a,b,c,num,d,e,f;
		int i=0,j=0;
		for(num=100;num<1000;num++)
		{
			a=num%10;//ge
			b=num/10%10;//shi
			c=num/100;//bai
			d=a*a*a;
			e=b*b*b;
			f=c*c*c;
			if(num==d+e+f)
				s[i++]=num;
		}
		if (j == 0) System.out.print(s[0]);
        for (j = 1; j <i; j++) {
            System.out.print("," + s[j]);
	}
}
}

1-6 逆序输出整数

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

输入格式:

整数个数n
n个整数

输出格式:

n个整数的逆序数

输入样例:

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

3
1234
2323
1112

输出样例:

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

4321
3232
2111

import java.util.*;
public class Main{
	
	public static void main(String argc[])
	{
       Scanner s=new Scanner(System.in);
      int a = s.nextInt();
       for(int i=1;i<=a;i++)
       {
    	   int b=s.nextInt();
    	   exchange(b);
	}
}
	public static void exchange(int b)
	{
		int [] k=new int [100000];
		int i=0;
		int c,sum=0;
		while(b!=0)
		{
			c=b%10;
			sum=sum*10+c;
			b=b/10;
		}
		System.out.println(sum);
}
}


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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值