考研题目汇总

编程入门 行业动态 更新时间:2024-10-19 15:41:49

考研<a href=https://www.elefans.com/category/jswz/34/1769227.html style=题目汇总"/>

考研题目汇总

1 反序输出

输入任意4个字符(如:abcd), 并按反序输出(如 :dcba)

输入描述:

题目可能包含多组用例,每组用例占一行,包含4个任意的字符。

输出描述:

对于每组输入,请输出一行反序后的字符串。
具体可见样例。
示例

输入

Upin
cvYj
WJpw
cXOA

输出

nipU
jYvc
wpJW
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;int main()
{char arr[5];while(scanf("%s",arr)!=EOF) //字符串与数组之间的联系
    {for(int i=3;i>=0;i--){printf("%c",arr[i]);}}return 0;
}
View Code

2 Problem Description

The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.

For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.    Input The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero Output For each integer in the input, output its digital root on a separate line of the output.    Sample Input 24 39 0 Sample Output 6 3
 1 package Test;
 2 
 3 import java.util.Scanner;
 4 /*这种方法更简单
 5  * public class Main {
 6     public static void main(String[] argsStrings){       
 7              Scanner scanner=new Scanner(System.in);
 8                    int num = scanner.nextInt();
 9                   while (num>9) {
10                           int count=0;
11                           while (num>0) {
12                                    count+=num%10;
13                                    num=num/10;
14                           }
15                           num=count;
16                   }                                
17                   System.out.print(num);       
18     }
19 }
20  */
21 
22 public class Main{
23     
24     public static void main(String[] args) {
25         Scanner sc=new Scanner(System.in);
26         while(sc.hasNext()){
27             int n=sc.nextInt();
28             System.out.println(f(n));
29         }
30     }
31     public static int f(int n){
32         int[] arr=new int[30];
33         int sum=0;
34         int k=0;
35         while(n!=0){
36             arr[k]=n%10;
37             k++;
38             n/=10;
39         }
40         for(int i=0;i<k;i++){
41             sum+=arr[i];    
42         }
43         if(sum>=10){
44             return f(sum);//这里是一个递归调用
45         }
46         else return sum;
47 /*一开始我是这样写的,用59做测试一直输出14,因为在第二步计算的5是函数内部的局部变量,不可能替换掉
48  * 第一步计算的14,所以后一步计算的结果没有办法传递到上一步,但是用return f(sum);就不一样了,这里不涉及
49  * 值的替换,一直计算到最后满足条件时才向上传值
50 * if(sum>=10){
51     f(sum);
52 }
53  return sum;
54 */
55     }
56 }
View Code

 3 成绩排序

题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩都按先录入排列在前的规则处理。例示:jack      70peter     96Tom       70smith     67 从高到低 成绩 peter 96 jack 70 Tom 70 smith 67 从低到高 smith 67 Tom 70 jack 70 peter 96 输入描述: 输入多行,先输入要排序的人的个数,然后分别输入他们的名字和成绩,以一个空格隔开 输出描述: 按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开

输入例子:
3

fang 90
yang 50
ning 70
输出例子:
fang 90
ning 70
yang 50

 

 1 package Test;
 2 
 3 import java.util.Scanner;
 4 /*
 5  * 在nextInt(),next(),nextDouble(),nextFloat()方法与nextLine()连用并放在nextLine()前面时,就会出现如下错误:
 6  * nextLine()并未读取任何输入,直接转到了下一行。
 7   问题分析:
 8      nextLine()会把nextInt(),next(),nextDouble(),nextFloat()的结束换行符作为字符串读入,
 9             进而不需要从键盘输入字符串nextLine便已经转向了下一条语句执行。
10    解决办法:
11      在每一个nextInt(),next(),nextDouble(),nextFloat()后都加一个nextLine()语句,将被next()去掉的Enter过滤掉。
12  * 
13 */
14 
15 public class Main{
16     public static void main(String[] args) {    
17         
18         Scanner sc=new Scanner(System.in);
19         while(sc.hasNext()){
20             //int n=sc.nextInt();出错
21             int n=Integer.parseInt(sc.nextLine()); 
22             int flag=sc.nextInt(); 
23             String s=sc.nextLine();//解决方案
24             student[] sts=new student[n];
25             for(int i=0;i<n;i++){
26                 String str[]=sc.nextLine().split(" ");//以空格为分割条件,分割为数组
27                 int score=Integer.parseInt(str[1]);//将字符串转换为数组
28                 String name=str[0];
29                 sts[i]=new student(name,score);//创建学生对象
30             }
31             InsertSort sor=new InsertSort();
32             if(flag==1){
33                 sts=sor.sort(sts);
34             }else{
35                 sts=sor.sort2(sts);
36             }
37             for(int i=0;i<n;i++){
38                 System.out.println(sts[i].getName()+" "+sts[i].getScore());
39             }
40         }
41     }
42 }
43 
44 
45 class student{
46     private String name;
47     private int score;
48     
49     //构造方法
50     public student(String name,int score){
51         this.name=name;
52         this.score=score;
53     }
54     
55     public String getName() {
56         return name;
57     }
58     public void setName(String name) {
59         this.name = name;
60     }
61     public int getScore() {
62         return score;
63     }
64     public void setScore(int score) {
65         this.score = score;
66     }
67 }
68 
69 //定义了一个插入排序类,里面有两种方法升序和降序
70 class InsertSort{
71     public student[] sort(student[] arr){
72         for(int i=1;i<arr.length;i++){
73             student tmp=arr[i];
74             int j=i-1;
75             while(j>-1&&tmp.getScore()<arr[j].getScore()){
76                 arr[j+1]=arr[j];
77                 j--;
78             }
79             arr[j+1]=tmp;
80         }
81         return arr;
82     }
83     
84     public student[] sort2(student[] arr){
85            for( int j = 1; j<arr.length;j++){
86                 student temp = arr[j];
87                 int i = j-1;
88                 while(i>-1&&temp.getScore()>arr[i].getScore()){
89                     arr[i+1] = arr[i];
90                     i--;
91                 }
92                 arr[i+1] = temp;
93             }
94             return arr;
95         }
96 }
View Code

 4 约数个数

输入n个整数,依次输出每个数的约数的个数
输入描述:
输入的第一行为N,即数组的个数(N<=1000)
接下来的1行包括N个整数,其中每个数的范围为(1<=Num<=1000000000)
当N=0时输入结束。
输出描述:
可能有多组输入数据,对于每组输入数据,
输出N行,其中每一行对应上面的一个数的约数的个数。
示例1
输入

5
1 3 4 6 12
输出

1
2
3
4
6

 1 package Test;
 2 /*
 3  * 对于要计算的数太大会导致计算时间延长,我一开始想着就是循环遍历找能整除的数发现直接超时了。
 4  * 待测数一直都在int范围内。
 5  * 对于输出格式,发现你把他们存数组输出和输入一个数就直接输出没区别。在测试时这个要求不严格
 6  * 约数定理:将m开方得n,判断n之前属于m的约数个数num。若n为整数,则m约数个数为2*num+1,否则为2*num  
 7  */
 8 import java.util.Scanner;
 9 
10 public class Main{
11     public static void main(String[] args) {
12         Scanner sc=new Scanner(System.in);
13         while(sc.hasNext()){
14             int n=sc.nextInt();
15             if(n==0) break;
16             //输入一个数后直接计算
17             while(n-->0){
18                 fun(sc.nextInt());
19             }
20         }
21     }
22     
23     public static void fun(int m){
24         int sum=0;
25         int root=(int)Math.sqrt(m);
26         for(int i=1;i<=root;i++){
27             if(m%i==0){
28                 sum+=2;
29             }
30         }
31         //利用相乘判断,可以提高精度,如果能开放则会多算一次
32         if(root*root==m){
33             sum=sum-1;
34         }
35         System.out.println(sum);
36     }
37 }
View Code

 5 判断三角形

题目描述:

给定三角形的三条边,a,b,c。判断该三角形类型。

输入:

测试数据有多组,每组输入三角形的三条边。

输出:

对于每组输入,输出直角三角形、锐角三角形、或是钝角三角形。

样例输入:
3 4 5
样例输出:
直角三角形
package Test;import java.util.Scanner;
//这个代码最重要的找最大数,中间数最小数的思路
public class Main{public static void main(String[] args) {Scanner sc=new Scanner(System.in);while(sc.hasNext()){int a=sc.nextInt();int b=sc.nextInt();int c=sc.nextInt();int x=Math.max(Math.max(a, b), c);//找到最大的数int y=Math.min(Math.min(a, b), c);//找到最小的数int z=a+b+c-x-y;//找到中间的数int res=z*z+y*y-x*x;if(res==0) System.out.println("直角三角形");else if(res>0)System.out.println("锐角三角形");else System.out.println("钝角三角形");}}
}
View Code 

 6 整数相加

题目描述:

给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。

输入:

输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。

输出:

请计算A+B的结果,并以正常形式输出,每组数据占一行。

样例输入:

-234,567,890   123,456,789

1,234   2,345,678

样例输出:

-111111101

2346912

 1 package Test;
 2 
 3 import java.util.Scanner;
 4 //对于Java各个类下的方法要熟悉
 5 public class Main{
 6     public static void main(String[] args) {
 7         Scanner sc=new Scanner(System.in);
 8         while(sc.hasNext()){
 9             String str1=sc.next();
10             String str2=sc.next();
11             str1=str1.replaceAll(",","");//把字符串中的逗号替换
12             str2=str2.replaceAll(",","");
13             //把字符串化为整数并相加
14             System.out.println(Integer.parseInt(str1)+Integer.parseInt(str2));
15         }
16     }
17 }
View Code

7 单词统计

题目描述:

编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。
(凡是以一个或多个空格隔开的部分就为一个单词)

输入:

输入包括1行字符串,以“.”结束,字符串中包含多个单词,单词之间以一个或多个空格隔开。

输出:

可能有多组测试数据,对于每组数据,
输出字符串中每个单词包含的字母的个数。

样例输入:
hello how are you.
样例输出:
5 3 3 3
 1 package Test;
 2 
 3 import java.util.Scanner;
 4 //对于Java各个类下的方法要熟悉
 5 public class Main{
 6     public static void main(String[] args) {
 7         Scanner sc=new Scanner(System.in);
 8         while(sc.hasNext()){
 9             String[] s=sc.nextLine().split(" ");//以空格为标志对输入分割
10             for(int i=0;i<s.length;i++){
11                 if(i==(s.length-1)){
12                     System.out.println(s[i].length()-1);//在输入的时候有一个点,不算做字符,要减去
13                 }else{
14                     System.out.print(s[i].length()+" ");//对输出格式调整
15                 }
16             }
17         }
18     }
19 }
View Code

 8 N的阶乘

输入描述  :正整数N(0<=N<=1000)
输出描述:   输入可能包括多组数据,对于每一组输入数据,输出N的阶乘
输入例子:
4
5
15
输出例子:
24
120
1307674368000
 1 package Test;
 2 //N的范围已经到1000了普通的long long 已经无法满足需要了
 3 import java.math.BigInteger;
 4 import java.util.Scanner;
 5 //对于Java各个类下的方法要熟悉
 6 public class Main{
 7     public static void main(String[] args) {
 8         Scanner sc=new Scanner(System.in);
 9         while(sc.hasNext()){
10          BigInteger res=BigInteger.valueOf(1);
11         //新建一个值为1的BigInteger对象,相当于直接把新建对象赋值一起了
12          int n=sc.nextInt();
13          for(int i=n;i>0;i--){
14              //i为普通整数先换为BigInteger对象,再用大数乘法计算
15              res=res.multiply(BigInteger.valueOf(i));
16          }
17          System.out.println(res);
18         }
19     }
20 }
View Code

 9 回文字符串

题目描述:

给出一个长度不超过1000的字符串,判断它是不是回文(顺读,逆读均相同)的。

输入:

输入包括一行字符串,其长度不超过1000。

输出:

可能有多组测试数据,对于每组数据,如果是回文字符串则输出"Yes!”,否则输出"No!"。

样例输入:
hellolleh
helloworld
样例输出
Yes!
No!
 1 package Test;
 2 /* String StringBuilder StringBuffer的区别
 3  * 1:在这方面运行速度快慢为:StringBuilder > StringBuffer > String
 4  * 原因:String为字符串常量,而StringBuilder和StringBuffer均为字符串变量,即String对象一旦创建之后该对象是不可更改的,但后两者的对象是变量,是可以更改的。
 5  * 2:在线程安全上,StringBuilder是线程不安全的,而StringBuffer是线程安全的
 6  * StringBuffer中很多方法可以带有synchronized关键字,所以可以保证线程是安全的。
 7  * 但StringBuilder的方法则没有该关键字,所以不能保证线程安全
 8  * 总结:
 9  * String:适用于少量的字符串操作的情况
10  * StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况
11  * StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况
12  * 
13  */
14 import java.util.Scanner;
15 
16 public class Main{
17     public static void main(String[] args) {
18         Scanner sc=new Scanner(System.in);
19         while(sc.hasNext()){
20             String str=sc.nextLine();
21             //用StringBuilder速度更快
22             StringBuilder sb=new StringBuilder(str);//把string转换为StringBuilder
23             sb.reverse();//字符串翻转
24             if(str.equals(sb.toString())){
25                 System.out.println("Yes!");
26             }else{
27                 System.out.println("No!");
28             }
29         }
30     }
31 }
View Code

 

 10 矩阵转置

输入一个N*N的矩阵,将其转置后输出。要求:不得使用任何数组(就地逆置)。

输入描述: 输入的第一行包括一个整数N,(1<=N<=100),代表矩阵的维数。 接下来的N行每行有N个整数,分别代表矩阵的元素。

输出描述: 可能有多组测试数据,对于每组数据,将输入的矩阵转置后输出。

输入例子: 3

              1 2 3

              4 5 6

              7 8 9

 

输出例子: 1 4 7

              2 5 8

              3 6 9

 1 package Test;
 2 
 3 import java.util.Scanner;
 4 //矩阵的转置输出实现很简单,但是这个代码写的就非常好
 5 public class Main{
 6     public static void main(String[] args) {
 7         Scanner sc=new Scanner(System.in);
 8         while(sc.hasNext()){
 9             int n=Integer.parseInt(sc.nextLine());//sc.nextLine()读入的是字符串
10             int[][] arr=new int[n][n];
11             for(int i=0;i<n;i++){
12                 String s[]=sc.nextLine().split(" "); //直接读入一行,把一行里的数字分隔开,并存入数组
13                 for(int j=0;j<n;j++){
14                     arr[i][j]=Integer.parseInt(s[j]);//把数组里的数字保存为矩阵的形式
15                 }
16             }
17             for(int i=0;i<n;i++){
18                 for(int j=0;j<n-1;j++)
19                     System.out.print(arr[j][i]+" ");//需要换行输出的
20                 System.out.println(arr[n-1][i]);
21             }
22         }
23     }
24 }
View Code

 11 字母统计

题目描述:
输入一行字符串,计算其中A-Z大写字母出现的次数
输入:
案例可能有多组,每个案例输入为一行字符串。
输出:
对每个案例按A-Z的顺序输出其中大写字母出现的次数。
样例输入:
DFJEIWFNQLEF0395823048+_+JDLSFJDLSJFKK
样例输出:
A:0
B:0
C:0
D:3
E:2
F:5
G:0
H:0
I:1
J:4
K:2
L:3
M:0
N:1
O:0
P:0
Q:1
R:0
S:2
T:0
U:0
V:0
W:1
X:0
Y:0
Z:0 

 1 import java.util.Arrays;
 2 import java.util.Scanner;
 3  
 4 public class Main{
 5     public static void main(String[] args) {
 6         Scanner sc=new Scanner(System.in);
 7         while(sc.hasNext()){
 8              
 9             String str=sc.next();
10             int n=str.length();
11             int[] a=new int[26];
12              
13             for(int i=0;i<n;i++){
14                 char tmp=str.charAt(i);
15                 if(tmp>='A'&&tmp<='Z'){
16                     a[tmp-'A']++;
17                 }
18             }
19              
20             for(int i=0;i<26;i++){
21                 char tmp=(char)(i+'A');
22                 System.out.println(tmp+":"+a[i]);
23             }
24         }
25     }
26 }
View Code

 12 谁是你的朋友

题目描述:

 “臭味相投”——这是我们描述朋友时喜欢用的词汇。两个人是朋友通常意味着他们存在着许多共同的兴趣。然而作为一个宅男,你发现自己与他人相互了解的机会并不太多。幸运的是,你意外得到了一份北大图书馆的图书借阅记录,于是你挑灯熬夜地编程,想从中发现潜在的朋友。     首先你对借阅记录进行了一番整理,把N个读者依次编号为1,2,…,N,把M本书依次编号为1,2,…,M。同时,按照“臭味相投”的原则,和你喜欢读同一本书的人,就是你的潜在朋友。你现在的任务是从这份借阅记录中计算出每个人有几个潜在朋友。

输入描述:

  每个案例第一行两个整数N,M,2 <= N ,M<= 200。接下来有N行,第i(i = 1,2,…,N)行每一行有一个数,表示读者i-1最喜欢的图书的编号P(1<=P<=M)

输出描述:

   每个案例包括N行,每行一个数,第i行的数表示读者i有几个潜在朋友。如果i和任何人都没有共同喜欢的书,则输出“BeiJu”(即悲剧,^ ^)

样例输入:
4 5
2
3
2
1
样例输出:

1

BeiJu

1

BeiJu

 解法一:

 1 package Test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Main{
 6     public static void main(String[] args) {
 7         Scanner sc=new Scanner(System.in);
 8         while(sc.hasNext()){
 9             int n=sc.nextInt();
10             int m=sc.nextInt();//这个m本书就是来搞笑的,一点用都没有
11             int[] arr=new int[n];
12             int[] out=new int[n];
13             for(int i=0;i<n;i++){
14                 arr[i]=sc.nextInt();
15                 out[i]=0;
16             }
17             //第二层循环不能是j=i+1。题目相当于找数组中相同的数,每个数都要遍历数组所有的数
18             for(int i=0;i<n;i++){
19                 for(int j=0;j<n;j++){
20                     if(arr[i]==arr[j]){
21                         out[i]++;                        
22                     }                        
23                 }
24                 //不容易控制自身不遍历,所以遍历自身然后再减1
25                 if(out[i]>1) System.out.println(out[i]-1);//把输出的循环直接放在遍历比较,减少输出循环
26                 else System.out.println("BeiJu");
27             }
28             
29         }
30     }
31 }

解法二:

 1 package Test;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.util.ArrayList;
 5 import java.util.HashMap;
 6 import java.util.List;
 7 import java.util.Map;
 8 import java.util.Scanner;
 9  //书和人数相对应,可以认为是key和value的关系
10 public class Main {
11     public static void main(String args[]) {
12         Scanner scanner = new Scanner(new BufferedInputStream(System.in));
13         while (scanner.hasNext()) {
14             int n = scanner.nextInt();
15             int m = scanner.nextInt();
16             List<Integer> list = new ArrayList<>();
17             Map<Integer, Integer> map = new HashMap<>();
18             for (int i = 0; i < n; i++) {
19                 int x = scanner.nextInt();
20                 if (x <= m) {
21                     list.add(x);
22                     if (map.containsKey(x)) {
23                         map.put(x, map.get(x) + 1);
24                     } else {
25                         map.put(x, 1);
26                     }
27                 }
28             }
29             for (int i : list) {
30                 int x = map.get(i) - 1;
31                 if (x <= 0) {
32                     System.out.println("BeiJu");
33                 } else {
34                     System.out.println(x);
35                 }
36             }
37         }
38         scanner.close();
39     }
40 }
41 
42 }

 13 A+B for Matrix

题目描述
This time, you are supposed to find A+B where A and B are two matrices, and then count the number of zero rows and columns.
输入描述:
The input consists of several test cases, each starts with a pair of positive integers M and N (≤10) which are the number of rows and columns of the matrices, respectively. Then 2*M lines follow, each contains N integers in [-100, 100], separated by a space. The first M lines correspond to the elements of A and the second M lines to that of B.
The input is terminated by a zero M and that case must NOT be processed.
输出描述:
For each test case you should output in one line the total number of zero rows and columns of A+B.
示例1
输入
2 2
1 1
1 1
-1 -1
10 9
2 3
1 2 3
4 5 6
-1 -2 -3
-4 -5 -6
0
输出
1
5

 1 package Test;
 2 
 3 import java.util.Map;
 4 import java.util.Scanner;
 5 import java.util.TreeMap;
 6 
 7 public class Main{
 8     public static void main(String[] args) {
 9         Scanner sc= new Scanner(System.in);
10         while(sc.hasNext()){
11             int m=sc.nextInt();
12             if(m==0) return;
13             int n=sc.nextInt();
14             int[][] a=new int[m][n];
15             for(int i=0;i<m;i++){
16                 for(int j=0;j<n;j++)
17                     a[i][j]=sc.nextInt();
18             }
19             boolean[] row=new boolean[m];//默认为false
20             boolean[] column=new boolean[n];
21             for(int i=0;i<m;i++){
22                 for(int j=0;j<n;j++){
23                     int x=sc.nextInt();
24                     if(a[i][j]+x!=0){
25                         row[i]=true;
26                         column[j]=true;
27                     }
28                 }
29             }
30             
31             int count=0;
32             for(boolean r:row){
33                 if(!r)
34                     count++;
35             }
36             for(boolean c:column){
37                 if(!c){
38                     count++;
39                 }
40             }
41             System.out.println(count);
42         }
43     }
44 }

 

 

 

 

 

更多推荐

考研题目汇总

本文发布于:2024-02-17 03:27:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1692481.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:题目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!