目录
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);
}
}