Java上机题汇总

2021.3.3

知识

读入/输出

  • 输出:System.out.println(“Hello, Java!”);
  • 读入:
    Scanner s = new Scanner(System.in);
    s.hasNextLine()
    String line = s.nextLine();
    b=i.nextInt();
    String s=in.next();
    s.close();

HELLO JAVA

Output a greeting phrase "Hello, Java! "
Input: None
Output: Hello, Java!
Hints:
Please output things according to the requirements. No extra output is allowed.

The class name MUST be Main. It’s case sensitive.

public class Main{
   
   
   public static void main(String[] args) {
   
   
	   System.out.println("Hello, Java!");
   }
}

console input/output

You are requested to output what you got from the system console.
Input: A line of string, until no inut is found! Example:
This is the first line.
This is the second line.

This is the end.
Output: What you get from the console(keyboard)
This is the first line.
This is the second line.

This is the end.
Hints:
On getting input from the console, you can refer to various articles on the web. For reading a line from the console, here is a example:
Scanner s = new Scanner(System.in);
if (s.hasNextLine()) {
String line = s.nextLine();

}

import java.util.*;
public class Main {
   
   
  public static void main(String[] args) {
   
   
	  Scanner s = new Scanner(System.in);
	  while(s.hasNextLine()) {
   
   
	  String line = s.nextLine();
	  System.out.println(line);
	  }
  }
}

A+B问题

1

You are requested to calculate the sum of two integral numbers a and b.
Input:
A pair of number seperated by the blank character. Example:
5 12
Output: the sum of the input pair
17
Hints:
On geting input from the console, you can refer to various articles on the web. Here is a example:
Scanner s = new Scanner(System.in);
int age = s.nextInt();
If you want to get the whole line, you can use:
String str = s.nextLine();

import java.util.*;
public class Main {
   
   
  public static void main(String[] args) {
   
   
	  Scanner i = new Scanner(System.in);
	  int a=i.nextInt();
	  int b=i.nextInt();
	  System.out.println(a+b);
  }
}

2

You are requested to calculate the sum of two integers a and b. This time we go further. There are multiple pair of a and b in a testing group. So you need to calculate the sum several times for each testing group. Note that there may be huge number of pairs to calculate. So don’t make an assumption of how many test cases.
Input:
Test case count T following by each test case. Example:
3
5 12
1 1
3 -5
Output: the sum of each input pair
17
2
-2

import java.util.*;
import java.util.ArrayList;
public class Main {
   
   
  public static void main(String[] args) {
   
   
	  Scanner in = new Scanner(System.in);
	  ArrayList al = new ArrayList();
	  int i=in.nextInt();
	  for(int j=0;j<i;j++){
   
   
	  int a=in.nextInt();
	  int b=in.nextInt();
	  al.add(a+b);
	  }
	  for(int j=0;j<i;j++)System.out.println(al.get(j));
  }
}

3

Description
Your task is to Calculate a + b.
Too easy?! Of course! I specially designed the problem for acm beginners.
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.
Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Sample Input
1 5
10 20
Sample Output
6
30

import java.util.*;
import java.util.ArrayList;
public class Main {
   
   
  public static void main(String[] args) {
   
   
	  Scanner in = new Scanner(System.in);
	  ArrayList al = new ArrayList();
	  int i=0;
	  int a=in.nextInt();
	  int b=in.nextInt();
	  al.add(a+b);
	  i++;
	  if(in.hasNextLine()){
   
   
	  a=in.nextInt();
	  b=in.nextInt();
	  al.add(a+b);
	  i++;
	  }
	  for(int j=0;j<i;j++)System.out.println(al.get(j));
  }
}

Approximate PI

PI can be computed using the following formula:
PI = 4 * (1-1/3+1/5-1/7+1/9-…+1/n).
For a given n, write a program that displays the result of PI
Input:
A single line containing a odd number n. Example:
9
Output: the result of calculated PI, keep 6 digit after the decimal point.
3.339683

import java.util.*;
import java.util.ArrayList;
public class Main {
   
   
  public static void main(String[] args) {
   
   
	  Scanner in = new Scanner(System.in);
	  int n=in.nextInt();
	  double sum=0,temp=0;
	  int k=1;
	  for(int j=1;j<=n;j=j+2) {
   
   		  
		  temp=1.0/(double)j;
		  temp=temp*k;
		  sum+=temp;
		  k=-1*k;
	  }
      double PI=4*sum;
      System.out.printf("%.6f\n",PI);
  }
}

Repeat

Repeat 1

Given a non negative number n and a string s, you are expected to repeatedly output the string s n times.
Input:
4
aabb
output:
aabbaabbaabbaabb

import java.util.*;
import java.util.ArrayList;
public class Main {
   
   
  public static void main(String[] args) {
   
   
	  Scanner in = new Scanner(System.in);
	  int n=in.nextInt();
	  String s=in.next();
	  for(int i=0;i<n-1;i++)
		  System.out.printf(s+" ");
	  System.out.println(s);
  }
}

Repeat 2

Given a non negative number n and a string s, you are expected to repeatedly output the string s in n lines.
Input:
4
aabb
output:
aabb
aabb
aabb
aabb

import java.util.*;
import java.util.ArrayList;
public class Main {
   
   
  public static void main(String[] args) {
   
   
	  Scanner in = new Scanner(System.in);
	  int n=in.nextInt();
	  String s=in.nextLine();
	  if(in.hasNextLine()){
   
   
	  s=in.nextLine();
	  }
	  for(int j=0;j<n;j++)System.out.println(s);
  }
}

2021.3.17

知识

integer转string

String t=Integer.toString(m);

string转数组

String num = s.next();
char[] arr = num.toCharArray();

string取空格

String c=m.replaceAll(" ", “”);

public类

class Vehicle{
double load;
double maxLoad;
public double getLoad()
{
	return load;
}
public double getMaxLoad()
{
	return maxLoad;
}
public Vehicle(double maxload)
{
	maxLoad=maxload;
}
}

private 类

class Vehicle{
private double load;
private double maxLoad;
public double getLoad()
{
	return load;
}
public double getMaxLoad()
{
	return maxLoad;
}
public Vehicle(double maxload)
{
	maxLoad=maxload;
}
public boolean addBox(double weight)
{
	if (load+weight>getMaxLoad())return false;
	else 
		{
		load+=weight;
		return true;
		}
}
}

Digit Count

Given a number n, you are required to output how many digits are there.
There are several test cases for each test group.
Input: Test case count T following by each test case. Example:
2
12
120
Output:
2
3

import java.util.*;
import java.util.ArrayList;
public class Main {
   
   
  public static void main(String[] args) {
   
   
	  int[] l=new int[100];
	  Scanner in = new Scanner(System.in);
	  int n=in.nextInt();
	  for(int j=0;j<n;j++) {
   
   
		int m=in.nextInt();
		String t=Integer.toString(m);
		l[j]=t.length();
	  }
	  for(int j=0;j<n;j++) {
   
   
			System.out.println(l[j]);
		  }
  }
}

Unique Digits

Given a number n, you are required to output its unique digits in appearing order. There are several test cases for each test group.
Input: Test case count T following by each test case, preceding zeros may exist. Example:
5
1212
120
0121
0000
-23
Output:
12
120
012
0
23

import java.util.*;
public class Main
{
   
   
public static void main(String[] args)
{
   
   
Scanner s = new Scanner(System.in);
int number = s.nextInt();
for(int i = 0; i < number; i++)
{
   
   
String num = s.next();
char[] arr = num.toCharArray();
int j = 0;
if(arr[0]=='-') j = 1;
outer:for(; j<arr.length; j++)
{
   
   
for(int m = 0;m<j;m++)
if(arr[m]==arr[j])
continue outer;
System.out.print(arr[j]);
}
System.out.print("\n");
}
s.close();
}
}

Sum of digits

Given a number n, you are required to output the sum of its digits. There are several test cases for each test group.
Input: Test case count T following by each test case. Example:
5
1211
1234
012
1111
-23

Output:
5
10
3
4
5

import java.util.*;  
import java.util.ArrayList;
public class Main {
   
   
  public static void main(String[] args) {
   
   
   int[] sum=new int[10];
   for(int i=0;i<10;i++)sum[i]=0;
   Scanner in = new Scanner(System.in);
   int n=in.nextInt();
   for(int j=0;j<n;j++) {
   
   
  int m=in.nextInt();
  int num=m;
  String t=Integer.toString(m);
  int l=t.length();
  if(num>=0)
  {
   
   
   char[]strarray=t.toCharArray();
   for(int i=0;i<l;i++)
   {
   
   
    sum[j]=strarray[i]-'0'+sum[j];
  } 
  }
  else {
   
   
  char[]strarray=t.toCharArray();
  for(int i=1;i<l;i++)
  {
   
   
   sum[j]=strarray[i]-'0'+sum[j];
 }
  }
   }
   for(int j=0;j<n;j++) {
   
   
   System.out.println(sum[j]);
    }
  }
}

foo bar ba

You are given two numbers “from”(inclusive) and “to”(inclusive). And you are expected to create an application that loops from “from” to “to” and prints out each value on a separate line, except print out “foo” for every multiple of 3, “bar” for every multiple of 5, and “baz” for every multiple of 7.
If “From” is greater than “to”, then output a blank line.
For example: if from=1 and to=16, then it will give the following output:
1
2
3 foo
4
5 bar
6 foo
7 baz
8
9 foo
10 bar
11
12 foo
13
14 baz
15 foo bar
16

Input: Total test case count T following by T cases of test instances. Example:
3
4 5
6 10
3 -1
output:
4
5 bar
6 foo
7 baz
8
9 foo
10 bar

import java.util.*;
import java.util.ArrayList;
public class Main {
   
   
  public static void main(String[] args) {
   
   
   int[] sum=new int[10];
      for(int i=0;i<10;i++)sum[i]=0;
   Scanner in = new Scanner(System.in);
   int n=in.nextInt();
   for(int j=0;j<n;j++) {
   
   
  int p=in.nextInt();
     int q=in.nextInt();
  if(p>q)
	  {
   
   
	  if(p!=3)System.out.println();
	  else continue;
	  }
  else{
   
   
   for(int t=p;t<=q;t++)
   {
   
   
    if(t%5==0&&t%3!=0&&t%7!=0)System.out.println(t+" bar");
    else if(t%3==0&&t%5!=0&&t%7!=0)System.out.println(t+" foo");
    else if(t%7==0&&t%5!=0&&t%3!=0)System.out.println(t+" baz");
    else if(t%7==0&&t%5!=0&&t%3==0)System.out.println(t+" foo baz");
    else if(t%7==0&&t%5==0&&t%3!=0)System.out.println(t+" bar baz");
    else if(t%7!=0&&t%5==0&&t%3==0)System.out.println(t+"foo bar");
    else if(t%7==0&&t%5==0&&t%3!=0)System.out.println(t+"foo bar baz baz");
    else System.out.println(t);
   } 
  }
  }
}
}

Reverse it

Given a number n, you are required to output its reverse counterpart. that is, the most significant digit become the list significant digit and so on. There are several test cases for each test group.

Input: Test case count T following by each test case. Example:
5
12000
11111
012
5
-23

Output:
21
11111
21
5
-32

import java.util.*;
public class Main
{
   
   
public static void main(String[] args)
{
   
   
Scanner s = new Scanner(System.in);
int times = s.nextInt();
for(int i = 0; i < times; i++)
{
   
   
int number = s.nextInt();
if(number < 0)
{
   
   
number = Math.abs(number);
System.out.print("-");
}
while(number%10 == 0) number /= 10;
while(number>=1)
{
   
   
System.out.print(number%10);
number /= 10;
}
System.out.print("\n");
}
s.close();
}
}

count number

Give you a character array, you are required to Count the number of occurrences of each character and print it to the console.

Input: a character array.
Output: the number of occurrences of each character .
Example:
a f m f o b b s n

a–1
b–2
f–2
m–1
n–1
o–1
s–1

import java.util.*;
import java.util.ArrayList;
public class Main{
   
   
  public static void main(String[] args) {
   
   
	  Scanner in = new Scanner(System.in);
	  int count[]=new int[26];
	  for(int j=0;j<26;j++)count[j]=0;
	  String m=in.nextLine();
	  String c=m.replaceAll(" ", "");
	  int l=c.length();
	 char[] strarray=c.toCharArray();
	 for(int i=0;i<l;i++)
	 {
   
   
		 int t=strarray[i]-'a';
		 count[t]++;
	 }
	 for(int i=0;i<26;i++)
	 {
   
   
		 int t=i+97;
		 char s=(char)t;
		 if(count[i]!=0)System.out.println(s+"--"+count[i<
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值