考研题目汇总

这篇博客整理了一系列编程题目,涵盖了数据结构和算法的不同方面,包括反序输出、成绩排序、约数个数计算、判断三角形、整数相加、单词统计、矩阵转置、字母频率统计等。每个题目都有详细的输入输出描述和示例,适合编程初学者和进阶者练习。
摘要由CSDN通过智能技术生成

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      70
   peter     96
   Tom       70
   smith     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 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值