1 反序输出
输入任意4个字符(如:abcd),
并按反序输出(如
:dcba)
输入描述:
题目可能包含多组用例,每组用例占一行,包含4个任意的字符。
输出描述:
对于每组输入,请输出一行反序后的字符串。 具体可见样例。
示例
输入
Upin cvYj WJpw cXOA
输出
nipU jYvc wpJW
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#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; }
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.
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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 }
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
0
fang 90
yang 50
ning 70
输出例子:
fang 90
ning 70
yang 50
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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