java程序设计(第一次作业)

一系列编程基础题目,包括简单的加法、输出第二个整数、数字统计、递归求和、判断C语言标识符合法性、奇数求和、十六进制转换、数字统计、阶乘计算以及找出最高分数的方法。涵盖了基本输入输出、字符串处理、循环、递归等核心概念。
摘要由CSDN通过智能技术生成

目录

01:A+B Problem

02:输出第二个整数

05:数字统计

06:递归求和

07:判断是否为C语言的合法标识符

08:奇数求和

09:十六进制转十进制

10:数字统计

11:求10000以内n的阶乘

12:最高的分数

01:A+B Problem

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int a = cin.nextInt();
		int b = cin.nextInt();
		System.out.println(a+b);

	}

}

02:输出第二个整数

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int a = cin.nextInt();
		int b = cin.nextInt();
		int c = cin.nextInt();
		System.out.println(b);

	}

}

05:数字统计

import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner n = new Scanner(System.in);
		int a = n.nextInt();
		int i=0;
		n.nextLine();
		while (i<a){
			String s = n.nextLine();
			int sum = 0;
			int m = 0;
			while(m<s.length()) {
				if (s.charAt(m) >='0' && s.charAt(m) <='9') {
					sum=sum+1;
				}
				m=m+1;
			}
			System.out.println(sum);
			i=i+1;
		}
	}
}

06:递归求和

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		Scanner n = new Scanner(System.in);
		int a=n.nextInt();
		int sum;
		sum=0;
		for(int i=1;i<=a;i++) {
			sum+=i*i;
				}
	    System.out.println(sum);
	}

}

07:判断是否为C语言的合法标识符

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        for (int i = 0; i < n; i++) {
        	String a = sc.nextLine();
        	int m = 0;
        	int sum = 1;
        	if(a.charAt(0)>='0' && a.charAt(0)<='9') {
    			sum = 0;
    		}
        	while(m<a.length()){
        		if(a.charAt(m) == ' '){
        			sum = 0;
        		}
        		m=m+1;
        	}
        	System.out.println(sum);
        }
    }
}

08:奇数求和

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner a = new Scanner(System.in);
		
		int n= a.nextInt();
		int m= a.nextInt();
        int s=0;
			for (int i = n; i <= m; i++) {
			 if(i%2!=0) {
				s+=i;
			}
			
		}
			System.out.println(s);
			
    }
}

09:十六进制转十进制

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
        	String a = sc.next();
            System.out.println(Integer.parseInt(a,16));
        }
    }
}

10:数字统计

import java.util.Scanner;
public class Main {
	  public static void main(String[] args) {
		  Scanner a = new Scanner(System.in);
	        int n = a.nextInt();
	        int[] b = new int[n];
	        int[] d = new int[1000000];
	        for (int i = 0; i < n; i++) 
	        {
	            b[i] = a.nextInt();
	            d[b[i]]++;
	        }
	        int sum = 1;
	        for (int i = 1;i<n-1;i++) {
	        	for (int j = i+1;j<n;j++) {
	        		if(b[i]==b[j]) {
	        			b[j]=b[0];
	        		}
	        	}
	        }
	        int s = b[0];
	        int m = d[s];
	        for (int i=0;i<n;i++) {
	        	if(b[0]!=b[i]) {
	        		sum=sum+1;
	        	}
	        }
	        System.out.println(sum);
	        System.out.println(s+" "+m);
	        for (int i=1;i<n;i++) {
	        	if(b[i]!=b[0]) {
	        		System.out.println(b[i]+" "+d[b[i]]);
	        	}
	        }		
	}
}

11:求10000以内n的阶乘

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        BigInteger p=new BigInteger("1");
        for (int i = 1; i < a + 1; i++) {
            p=p.multiply(new BigInteger(i+""));
        }
        System.out.println(p);
    }
}
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        List<Integer> result = new ArrayList<Integer>();//List list = new ArrayList(),调用 List接口里面的内置函数,add,get等方法
       //List< Integer>,List是一个接口<>表示了List里面放的对象是什么类型的,这样写就表示了,你List里面放的必须是Integer类型的。
        int carry = 0;
        int digit = 1;
        int temp = 1;
        int i;
        int j;
        result.add(1);
        for(i = 2;i<=n;i++){
            for(j = 0,carry = 0;j<digit;j++){
                temp = result.get(j)*i+carry;
                result.set(j, temp%10000);
                carry  = temp/10000;
            }
            while(carry != 0){
                result.add(digit++, carry%10000);
                carry/=10000;
            }
        }
        int count = 1;
        System.out.print(result.get(digit-1));
        for(i = digit-1;i>=1;i--){
            count++;
            if(result.get(i-1)<10){
                System.out.print("000");
            }else if(result.get(i-1)<100){
                System.out.print("00");
            }else if(result.get(i-1)<1000){
                System.out.print("0");
            }
            System.out.print(result.get(i-1));
            
        }
    }
}        


12:最高的分数

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {

	  Scanner a=new Scanner(System.in);
	  int n=a.nextInt();
	  
	  int max=0;
		for(int i=0;i<n;i++)
		{
                        int c=a.nextInt();			
                        if(c>max)
			{
				max=c;
			}
		}
		System.out.println(max);
  }
}

import java.util.Scanner;
public class Main {
	  public static void main(String[] args) {
		  Scanner a = new Scanner(System.in);
	      int n = a.nextInt();
	      int[] b = new int[n];
	      for (int i=0;i<n;i++) {
	    	  b[i] = a.nextInt();
	      }
	      int max = b[0];
	      int m ;
		  for(int i=0;i<n-1;i++) {
			  m = i+1;
			  if(max<b[m] && b[i]<b[m]) {
				  max = b[m];
			  }
		  }
		  System.out.println(max);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值