欢迎光临
我们一直在努力

sdut-Java面向对象-04基础语法3-数组应用(11-22)

7-11 sdut-array2-3 二维方阵变变变

有4行,4列的数据组成的矩阵。

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

它经过顺时针90度旋转后的效果为:(旋转角度值设为 90 )

13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4

顺时针180度旋转后的效果为:(旋转角度值设为 180 )

16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1

逆时针90度旋转后的结果为:(旋转角度值设为 -90 )

4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13

输入格式:

首行包括:(1)二维矩阵的行(列)数n,(2)矩阵旋转的角度,从90、180、-90中取一个; 接下来是n行n列的整数,数据之间以空格分隔,组成一个方阵。

输出格式:

矩阵按指定角度旋转后的结果。输出时按行、列进行组织。每行的数据之间有一个空格;行末尾数据后没有空格。

输入样例1:

4 90
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

输出样例1:

13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4

输入样例2:

4 180
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

输出样例2:

16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1

输入样例3:

4 -90
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

输出样例3:

4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13

实现代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int angel = sc.nextInt();
int[][] a = new int[n + 3][n + 3];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
a[i][j] = sc.nextInt();
}
}
if (angel == 90) {
for (int i = 1; i <= n; i++) {
for (int j = n; j >= 1; j–) {
System.out.printf("%d", a[j][i]);
if(j>1) System.out.printf(" ");
}
System.out.println();
}
}
if(angel==-90) {
for(int i = n;i>=1;i–){
for(int j=1;j<=n;j++){
System.out.printf("%d",a[j][i]);
if(j<n) System.out.printf(" ");
}
System.out.println();
}
}
if(angel == 180) {
for(int i=n;i>=1;i–){
for(int j=n;j>=1;j–){
System.out.printf("%d",a[i][j]);
if(j>1) System.out.printf(" ");
}
System.out.println();
}

}
}
}

7-12 sdut-array1-2 RDMP音乐播放器(III)(一维数组)

注意: 这是在使用数组的情况下完成的。

RDMP音乐播放器将存储m首歌曲,它们的名称将是以字母'A'开始的歌曲。(m<=26)
例如:
M=3,则歌曲名称将是:'A','B','C' 。
M=7,则歌曲名称将是:'A','B','C', D','E','F','G' 。

RDMP有3个按钮,用户可以按下这些按钮来重新排列播放列表并播放歌曲。
举例:
如果,最初RDMP播放列表是“A, B, C, D, E”。3个控制按钮做以下工作:
按钮1: 将播放列表的第一首歌曲移动到播放列表的末尾。例如:“A, B, C, D, E”会变成“B, C, D, E, A”。
按钮2: 将播放列表的最后一首歌移动到播放列表的开始。例如,“A, B, C, D, E”会变成“E, A, B, C, D”。
按钮3: 交换播放列表的前两首歌。例如,“A, B, C, D, E”会变成“B, A, C, D, E”。

你需要编写一个程序来模拟一个可以按下n次按钮的RD音乐播放器。(n>0)

输入格式:

输入有多行。
首行是两个整数,分别为歌曲的数量m和按下播放按钮的次数n。
后续有n行整数,表示按下按扭的不同的操作,均为1到3之间的数字,代表要采取的行动。

输出格式:

输出最终播放列表的正确顺序。
歌曲名称中间用逗号作分隔。最后一首歌名称之后没有逗号。

输入样例1:

5 2
1
2

输出样例1:

A,B,C,D,E

输入样例2:

5 2
3
3

输出样例2:

A,B,C,D,E

输入样例3:

10 5
1
2
3
3
1

输出样例3:

B,C,D,E,F,G,H,I,J,A

实现代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
char[] a = new char[n];
for(int i = 0;i<n;i++)
{
a[i] = (char)('A'+i);
}
int m = sc.nextInt();
while(m–>0) {
int num = sc.nextInt();
if(num == 1) method1(a);
else if(num == 2) method2(a);
else if(num == 3) method3(a);
}
for (int i = 0; i < a.length; i++) {
System.out.printf("%c",a[i]);
if(i<a.length-1) System.out.printf(",");
}
}
public static void method1(char a[]) {
char temp = a[0];
for(int i = 1;i<a.length;i++)
a[i-1]=a[i];
a[a.length-1] = temp;
}
public static void method2(char a[]) {
char temp = a[a.length-1];
for(int i = a.length-2;i>=0;i–) {
a[i+1] = a[i];
}
a[0] = temp;
}
public static void method3(char a[]) {
char temp = a[0];
a[0] = a[1];
a[1] = temp;
}
}

7-13 从键盘输入n个数存入一维数组中,编程实现输出下标为奇数的元素。

从键盘输入n(<=100)个数存入一维数组中,编程实现输出下标为奇数的元素。

输入格式:

第一行输入n的值,第二行输入n个用空格隔开的整数。

输出格式:

输出数据之间用逗号,隔开,最后一个数据后换行。

输入样例:

10
1 2 3 4 5 6 7 8 9 0

输出样例:

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

2,4,6,8,0

实现代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int []a=new int[n+1];
for(int i=1;i<=n;i++)
{
a[i]=scan.nextInt();
}
for(int i=1;i<=n;i++)
{
if(i%2==0) System.out.print(a[i]);
if(n%2==0&&i<n&&i%2==0) System.out.print(',');
else if(n%2==1&&i<n-1&&i%2==0) System.out.print(',');
}
}
}

7-14 输出斐波那契数列的前n项

已知斐波那契数列定义:f(0) = 0; f(1) = 1; f(n) = f(n-1) + f(n-2); 请计算并输出数列的前n(n<=30)项。

输入格式:

输入一个正整数n(n<=30),n的值在程序中不用判断,输入时注意即可。

输出格式:

输出数列值,数据之间用逗号分隔,即每个数据后加一个逗号输出。

输入样例:

26

输出样例:

0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,41

实现代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int []f=new int[30];
f[0]=0;
f[1]=1;
for(int i=2;i<30;i++)
{
f[i]=f[i-1]+f[i-2];
}
int n=scan.nextInt();
for(int i=0;i<n;i++)
{
System.out.print(f[i]+",");
}
}
}

7-15 整理队伍

体育课上,教数学的体育老师怎么看这个队伍都不大对劲,于是对队伍进行了调整。张三,你和李四换一下位置……经过几次调整,队伍终于变得有条不紊了。

输入格式:

输入第一行包括一个整数n,表示班级里总共有n个人(1<=n<=100);第二行包括n个整数,表示同学们原来的顺序。第三行包括一个数m(m>=0),表示体育老师总共进行了m次调换;下面包括m行,每行包括两个数x,y,表示x位置的同学和y位置的同学调换位置。

输出格式:

输出调换之后的队伍,每两个同学的编号之间有一个空格。

输入样例:

5
1 2 3 4 5
3
1 2
1 5
3 4

输出样例:

5 1 4 3 2

实现代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int []a=new int[n+1];
for(int i=1;i<=n;i++)
{
a[i]=scan.nextInt();
}
int m=scan.nextInt();
for(int i=1;i<=m;i++)
{
int temp;
int x=scan.nextInt();
int y=scan.nextInt();
temp=a[x];
a[x]=a[y];
a[y]=temp;
}
for(int i=1;i<=n;i++)
{
if(i==n) System.out.println(a[i]);
else System.out.print(a[i]+" ");
}
}
}

7-16 JAVA-求整数序列中出现次数最多的数

要求统计一个整型序列中出现次数最多的整数及其出现次数。

输入格式:

在一行中给出序列中整数个数N(0<N≤1000),依次给出N个整数,每个整数占一行。

输出格式:

在一行中输出出现次数最多的整数及其出现次数,数字间以空格分隔。题目保证这样的数字是唯一的。

输入样例:

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

10
3
2
-1
5
3
4
3
0
3
2

输出样例:

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

3 4

实现代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n= sc.nextInt();
int[] a = new int[100000];
int[] b = new int[100000];
while(n–>0) {
int num = sc.nextInt();
if(num>=0) a[num]++;
else if(num<0) b[-num]++;
}
int maxa =0;
int maxb =0;
int maxai =0;
int maxbi =0;
for (int i = 0; i < a.length; i++) {
if(a[i]>maxa) {
maxa = a[i];
maxai = i;
}
if(b[i]>maxb) {
maxb = b[i];
maxbi = i;
}
}
if(maxa>=maxb) {
System.out.printf("%d %d",maxai,maxa);
}
else {
System.out.printf("%d %d",-maxbi,maxb);
}
}
}

7-17 兔子繁殖问题

已知有一对兔子,每个月可以生一对兔子,而小兔子一个月后又可以生一对小兔子(比如:2月份出生的小兔子4月份可以生育)。也就是说,兔子的对数为:第一个月1对,第二个月2对,第三个月3对,第四个月5对…..假设兔子的生育期为两年,且不死。那么问题来了,你能说出每个月的兔子数么?

输入格式:

输入一个数n,表示第n个月,1<=n<=24。

输出格式:

输出这个月兔子的数目。

输入样例:

4

输出样例:

5

实现代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n + 2];
a[1] = 1;
a[2] = 2;
for (int i = 3; i <= n; i++){
a[i] = a[i-1]+a[i-2];
}
System.out.printf("%d",a[n]);
}
}

7-18 About-face

It's time for the physical education class to begin, and before the class starts, it's essential to line up. At first, all the students are standing in a single row according to their height, but they are all facing away from the physical education teacher. At this moment, the teacher takes a deep breath and shouts loudly: "About-face!"

Xiao Zhou is a programmer. Seeing this scene, he came up with a question. Suppose that before the teacher gave the command, the monitor was facing the first student in the line and recorded the student numbers in order. After the teacher's command, what will be the order of the student numbers?

Input Format:

The first line contains a number n, indicating that there are n students in the class. The number of students is at least 1 and does not exceed 100. The second line contains n numbers, representing the student numbers of each student.

Output Format:

Please output the student numbers in order after the "about-face" command, separated by spaces.

Sample Input:

5
3 1 5 4 2

Sample Output:

2 4 5 1 3

实现代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=scan.nextInt();
}
for(int i=n-1;i>=0;i–)
{
if(i==0) System.out.println(a[i]);
else System.out.print(a[i]+" ");
}
}
}

7-19 摘苹果

陶陶家的院子里有一棵苹果树,每到秋天树上就会结出很多苹果。苹果成熟的时候,陶陶就会跑去摘苹果。陶陶有个30厘米高的板凳,当她不能直接用手摘到苹果的时候,就会踩到板凳上再试试。

现在已知每个苹果到地面的高度,以及陶陶把手伸直的时候能够达到的最大高度,请帮陶陶算一下她能够摘到的苹果的数目。假设她碰到苹果,苹果就会掉下来。

输入格式:

包括三行数据。 第一行是一个整数n,表示有n个苹果。苹果至少有一个,不超过1000个。 第二行包含n个100到200之间(包括100和200)的整数(以厘米为单位)分别表示这n个苹果到地面的高度,两个相邻的整数之间用一个空格隔开。 第三行是一个100到120之间(包含100和120)的整数(以厘米为单位),表示陶陶把手伸直的时候能够达到的最大高度。

输出格式:

一个整数,表示陶陶能够摘到的苹果的数目。

输入样例:

10
100 200 150 140 129 134 167 198 200 111
110

输出样例:

5

实现代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=scan.nextInt();
}
int m=scan.nextInt();
int num=0;
for(int i=0;i<n;i++)
{
if(m+30>=a[i]) num++;
}
System.out.println(num);
}
}

7-20 数组元素移动

完成数组元素的移动功能:假设数组有n个元素,输入一个数x,把数组的第x个位置的元素先保存起来,然后把x+1到n的元素,依次往前移一位,最后将原来的第x个位置的元素放在数组的最后。 重复若干次这样的移动,得到最后的结果。

输入格式:

第一行包括一个整数n(1<=n<=100),表示数组元素的个数。 第二行输入n个数组元素,均为整数,用空格隔开。 第三行输入一个数k(1<=k<=100),表示要进行k次移动。 接下来k行,每行一个数x,表示要移动第x个元素。

输出格式:

输出经过k次移动后的数组,每两个元素之间用空格隔开。

输入样例:

10
1 2 3 4 5 6 7 8 9 10
4
4
3
2
5

输出样例:

1 5 6 7 9 10 4 3 2 8

实现代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n+5];
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
}
int m = sc.nextInt();
while(m–>0){
int num = sc.nextInt();
int temp = a[num];
for(int i = num;i<=n;i++){
a[i] = a[i+1];
}
a[n] = temp;
}
for (int i = 1; i <=n; i++) {
System.out.printf("%d",a[i]);
if(i!=n) System.out.printf(" ");
}
}
}

7-21 数组元素的删除

完成数组元素的移动功能:假设数组有n个元素,输入一个数x,把数组的第x个位置的元素删除了,后面的元素依次前进一个位置。 重复若干次这样的删除,得到最后的结果。

输入格式:

第一行包括一个整数n(1<=n<=100),表示数组元素的个数。 第二行输入n个数组元素,均为整数,用空格隔开。 第三行输入一个数k(1<=k<=100),表示要进行k次删除。 接下来k行,每行一个数x,表示要删除第x个元素。

输出格式:

输出经过k次删除后的数组,每两个元素之间用空格隔开。

输入样例:

10
1 2 3 4 5 6 7 8 9 10
4
3
2
4
6

输出样例:

1 4 5 7 8 10

实现代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n+5];
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
}
int m = sc.nextInt();
int temp = m;
while(m–>0){
int num = sc.nextInt();
for(int i = num;i<=n;i++){
a[i] = a[i+1];
}
}
for (int i = 1; i <=(n-temp); i++) {
System.out.printf("%d",a[i]);
if(i!=(n-temp)) System.out.printf(" ");
}
}
}

7-22 Pair Up for the Autumn Outing

The students are so excited—they can finally go on an autumn outing! The teacher says that it's a bit dangerous to go alone, so let's pair up and travel together. The pairing rule is as follows: the first person in line pairs up with the last person, the second person pairs up with the second-to-last person, and so on, forming pairs of two. Now, let's quickly organize! As soon as the teacher finishes speaking, the students pair up swiftly, ready to set off!

Input Format:

The first line is an integer n, representing the number of students. (2<=n<=50,and n is even). The second line contains n integers representing the student numbers, separated by spaces.

Output Format:

There are n/2 lines, each containing two integers representing the paired student numbers, separated by a space.

Sample Input:

6
5 6 4 12 3 2

Sample Output:

5 2
6 3
4 12

实现代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=scan.nextInt();
}
for(int i=0;i<n/2;i++)
{
System.out.println(a[i]+" "+a[n-i-1]);
}
}
}

赞(0)
未经允许不得转载:171主机测评 » sdut-Java面向对象-04基础语法3-数组应用(11-22)
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址