102道java算法

1:奥运奖牌计数

   
   
  1. import java.util.Scanner;
  2. //输入n+1行,第1行是A国参与决赛项目的天数n,其后n行,每一行是该国某一天获得的金、银、铜牌数目,以一个空格分开
  3. //输出1行,包括4个整数,为A国所获得的金、银、铜牌总数及总奖牌数,以一个空格分开。
  4. public class 奥运奖牌计数 {
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. int x=sc.nextInt();
  8. int a=0; //金牌计数器
  9. int b=0; //银牌计数器
  10. int c=0; //铜牌计数器
  11. //天数控制语句
  12. for (int i = 0; i < x; i++) {
  13. a+=sc.nextInt();//金牌数
  14. b+=sc.nextInt();//银牌数
  15. c+=sc.nextInt();//铜牌数
  16. }
  17. System.out.print(a+" "+b+" "+c+" "+(a+b+c));
  18. }
  19. }

2:病历单加密

    
    
  1. import java.util.Scanner;
  2. /*
  3. * 小英是药学专业大三的学生,暑假期间获得了去医院药房实习的机会。
  4. 在药房实习期间,小英扎实的专业基础获得了医生的一致好评,得知小英在计算概论中取得过好成绩后,
  5. 主任又额外交给她一项任务,解密抗战时期被加密过的一些伤员的名单。
  6. 经过研究,小英发现了如下加密规律(括号中是一个“原文 -> 密文”的例子)
  7. 1. 原文中所有的字符都在字母表中被循环左移了三个位置(dec -> abz)
  8. 2. 逆序存储(abcd -> dcba )
  9. 3. 大小写反转(abXY -> ABxy)
  10. *
  11. * */
  12. public class 病历单加密 {
  13. public static void main(String[] args) {
  14. Scanner sc=new Scanner(System.in);
  15. String str=sc.nextLine();
  16. String str1=""; //定义字符串用于存放新的字符串
  17. char x=' '; //定义字符,用于单个字符操作
  18. //以字符串长度控制循环
  19. for (int i = 0; i < str.length(); i++) {
  20. if('W'<str.charAt(i)&&str.charAt(i)<='Z'){
  21. x=(char)('A'+str.charAt(i)+2-'Z'+32);//算出其字符对应的ASCII的值,将其转换为char类型,并将大小写互换
  22. }else if('A'<=str.charAt(i)&&str.charAt(i)<='W'){
  23. x=(char)(str.charAt(i)+3+32);
  24. }else if('w'<str.charAt(i)&&str.charAt(i)<='z'){
  25. x=(char)('a'+str.charAt(i)+2-'z'-32);
  26. }else{
  27. x=(char)(str.charAt(i)+3-32);
  28. }
  29. str1=x+str1; //循环相加得到新的倒叙的字符串
  30. }
  31. System.out.println(str1);
  32. }
  33. }

3:大象喝水

     
     
  1. /*
  2. *一只大象口渴了,要喝20升水才能解渴,但现在只有一个深h厘米,底面半径为r厘米的小圆桶(h和r都是整数)。问大象至少要喝多少桶水才会解渴。
  3. 输入
  4. 输入有一行:包行两个整数,以一个空格分开,分别表示小圆桶的深h和底面半径r,单位都是厘米。
  5. 输出
  6. 输出一行,包含一个整数,表示大象至少要喝水的桶数。
  7. 样例输入
  8. *
  9. * */
  10. import java.util.Scanner;
  11. public class 大象喝水 {
  12. public static void main(String[] args) {
  13. Scanner sc=new Scanner(System.in);
  14. int a=sc.nextInt();
  15. int b=sc.nextInt();
  16. double pi=3.14159;
  17. int num=0;
  18. //小圆桶的装水的多少和大象喝水的多少进行判断
  19. if(20*1000<pi*b*b*a){
  20. num=1;
  21. }
  22. else{
  23. num=(int) (20*1000/(pi*b*b*a)+1); //桶数,进行向上取整
  24. }
  25. System.out.println(num);
  26. }
  27. }

4:病人排队

      
      
  1. import java.util.Scanner;
  2. /*
  3. *病人登记看病,编写一个程序,将登记的病人按照以下原则排出看病的先后顺序:
  4. 1. 老年人(年龄 >= 60岁)比非老年人优先看病。
  5. 2. 老年人按年龄从大到小的顺序看病,年龄相同的按登记的先后顺序排序。
  6. 3. 非老年人按登记的先后顺序看病。
  7. 输入
  8. 第1行,输入一个小于100的正整数,表示病人的个数;
  9. 后面按照病人登记的先后顺序,每行输入一个病人的信息,包括:一个长度小于10的字符串表示病人的ID
  10. (每个病人的ID各不相同且只含数字和字母),一个整数表示病人的年龄,中间用单个空格隔开。
  11. 输出
  12. 按排好的看病顺序输出病人的ID,每行一个。
  13. 样例输入
  14. 5
  15. 021075 40
  16. 004003 15
  17. 010158 67
  18. 021033 75
  19. 102012 30
  20. 样例输出
  21. 021033
  22. 010158
  23. 021075
  24. 004003
  25. 102012
  26. *
  27. * */
  28. public class 病人排队 {
  29. public static void main(String[] args) {
  30. Scanner sc = new Scanner(System.in);
  31. int num = sc.nextInt();
  32. // 定义字符串数组用于存放年龄小于60的病人ID
  33. String[] str = new String[num];
  34. // 定义字符串数组用于存放年龄大于等于60的病人ID
  35. String[] str1 = new String[num];
  36. // 定义整型数组存放大于等于60的病人年龄
  37. int[] age = new int[num];
  38. // 定义年龄大于等于60以及小于60的计数器
  39. int count = 0;
  40. int sum = 0;
  41. for (int i = 0; i < num; i++) {
  42. // 定义变量用于临时存放ID和年龄
  43. String s = sc.next();
  44. int x = sc.nextInt();
  45. if (x >= 60) {
  46. age[count] = x;
  47. str1[count] = s;
  48. count++;
  49. } else {
  50. str[sum] = s;
  51. sum++;
  52. }
  53. }
  54. // 利用冒泡排序法根据年龄降序对病人ID进行对应存储
  55. for (int i = 0; i < count; i++) {
  56. for (int j = i + 1; j <= count - 1; j++) {
  57. if (age[i] < age[j]) {
  58. int temp=age[i];
  59. age[i]=age[j];
  60. age[j]=temp;
  61. String z = str1[i];
  62. str1[i] = str1[j];
  63. str1[j] = z;
  64. }
  65. }
  66. }
  67. // 输出看病顺序病人ID
  68. for (int i = 0; i < count; i++) {
  69. System.out.println(str1[i]);
  70. }
  71. for (int i = 0; i < sum; i++) {
  72. System.out.println(str[i]);
  73. }
  74. }
  75. }

5:不高兴的津津

       
       
  1. import java.util.Scanner;
  2. /*
  3. *津津上初中了。妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班。
  4. *另外每周妈妈还会送她去学习朗诵、舞蹈和钢琴。但是津津如果一天上课超过八个小时就会不高兴,而且上得越久就会越不高兴。
  5. *假设津津不会因为其它事不高兴,并且她的不高兴不会持续到第二天。请你帮忙检查一下津津下周的日程安排,看看下周她会不会不高兴;
  6. *如果会的话,哪天最不高兴。
  7. 输入
  8. 包括七行数据,分别表示周一到周日的日程安排。每行包括两个小于10的非负整数,用空格隔开,
  9. 分别表示津津在学校上课的时间和妈妈安排她上课的时间。
  10. 输出
  11. 包括一行,这一行只包含一个数字。如果不会不高兴则输出0,如果会则输出最不高兴的是周几(用1, 2, 3, 4, 5, 6, 7
  12. 分别表示周一,周二,周三,周四,周五,周六,周日)。如果有两天或两天以上不高兴的程度相当,则输出时间最靠前的一天。
  13. 样例输入
  14. 5 3
  15. 6 2
  16. 7 2
  17. 5 3
  18. 5 4
  19. 0 4
  20. 0 6
  21. 样例输出
  22. 3
  23. * */
  24. public class 不高兴的津津 {
  25. public static void main(String[] args) {
  26. Scanner sc=new Scanner(System.in);
  27. int[] arr=new int[7];
  28. int max=0;
  29. //定义计数器
  30. int count=1;
  31. for (int i = 0; i < arr.length; i++) {
  32. //将津津上课的时间相加
  33. for (int j = 0; j < 2; j++) {
  34. arr[i]+=sc.nextInt();
  35. }
  36. }
  37. //将星期一上课的时间相加
  38. max=arr[0];
  39. //找出津津上课时间的最大值
  40. for (int i = 1; i < arr.length; i++) {
  41. if(max<arr[i]){
  42. max=arr[i];
  43. count=i+1;
  44. }
  45. }
  46. //判断津津是否高兴
  47. if(max>8)
  48. System.out.println(count);
  49. else
  50. System.out.println(0);
  51. }
  52. }

6:不与最大数相同的数字之和

        
        
  1. package 算法;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. /*
  5. *输出一个整数数列中不与最大数相同的数字之和。
  6. 输入
  7. 输入分为两行:
  8. 第一行为N(N为接下来数的个数,N <= 100);
  9. 第二行为N个整数,数与数之间以一个空格分开,每个整数的范围是-1000,000到1000,000。
  10. 输出
  11. 输出为N个数中除去最大数其余数字之和。
  12. 样例输入
  13. 3
  14. 1 2 3
  15. 样例输出
  16. 3
  17. *
  18. * */
  19. public class 不与最大数相同的数字之和 {
  20. public static void main(String[] args) {
  21. Scanner sc = new Scanner(System.in);
  22. int num = sc.nextInt();
  23. // 定义数组用于存放整数
  24. int[] arr = new int[num];
  25. // 定义数组和的变量
  26. int sum = 0;
  27. for (int i = 0; i < arr.length; i++) {
  28. arr[i] = sc.nextInt();
  29. }
  30. // 利用工具类对数组进行排序
  31. Arrays.sort(arr);
  32. if (num == 1||arr[0]==arr[arr.length-1])
  33. System.out.println(0);
  34. else {
  35. // 对除去最大数的数进行加法运算
  36. for (int i = 0; i < arr.length - 1; i++) {
  37. //判断,如果数组中的数不等于最大值则进行加法运算
  38. if(arr[arr.length-1]!=arr[i])
  39. sum += arr[i];
  40. }
  41. System.out.println(sum);
  42. }
  43. }
  44. }

7:财务管理

        
        
  1. package 算法;
  2. import java.util.Scanner;
  3. public class 财务管理 {
  4. public static void main(String[] args) {
  5. Scanner sc=new Scanner(System.in);
  6. double[] moneys=new double[12];//钱数存储容器
  7. double sum=0;//钱数总和
  8. //将容器中的钱数相加
  9. for(int i=0;i<moneys.length;i++){
  10. sum+=sc.nextDouble();
  11. }
  12. //输出平均钱数,保留两位小数并在其前面加"$"
  13. System.out.printf("$"+"%.2f",sum/12);
  14. }
  15. }

8:成绩排序

         
         
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *给出班里某门课程的成绩单,请你按成绩从高到低对成绩单排序输出,如果有相同分数则名字字典序小的在前。
  5. 输入
  6. 第一行为n (0 < n < 20),表示班里的学生数目;
  7. 接下来的n行,每行为每个学生的名字和他的成绩, 中间用单个空格隔开。名字只包含字母且长度不超过20,成绩为一个不大于100的非负整数。
  8. 输出
  9. 把成绩单按分数从高到低的顺序进行排序并输出,每行包含名字和分数两项,之间有一个空格。
  10. 样例输入
  11. 4
  12. Kitty 80
  13. Hanmeimei 90
  14. Joey 92
  15. Tim 28
  16. 样例输出
  17. Joey 92
  18. Hanmeimei 90
  19. Kitty 80
  20. Tim 28
  21. *
  22. * */
  23. public class 成绩排序 {
  24. public static void main(String[] args) {
  25. Scanner sc=new Scanner(System.in);
  26. int num=sc.nextInt();
  27. //定义字符串数组用于存放姓名
  28. String[] str=new String[num];
  29. //定义整型数组用于存放成绩
  30. int[] arr=new int[num];
  31. for (int i = 0; i < num; i++) {
  32. str[i]=sc.next();
  33. arr[i]=sc.nextInt();
  34. }
  35. //利用冒泡排序法对成绩高低进行排序,并对应姓名
  36. for (int i = 0; i < arr.length; i++) {
  37. for (int j = i+1; j <=arr.length-1; j++) {
  38. //按成绩由小到大的顺序排序
  39. if(arr[i]<arr[j]){
  40. int temp=arr[i];
  41. arr[i]=arr[j];
  42. arr[j]=temp;
  43. String name=str[i];
  44. str[i]=str[j];
  45. str[j]=name;
  46. }
  47. //当成绩相同时,按名字的字典顺序从小到大进行排序
  48. if(arr[i]==arr[j]){
  49. int m=str[i].compareTo(str[j]);
  50. if(m>0){
  51. String name=str[i];
  52. str[i]=str[j];
  53. str[j]=name;
  54. }
  55. }
  56. }
  57. }
  58. for (int i = 0; i < arr.length; i++) {
  59. System.out.print(str[i]+" "+arr[i]+"\r\n");
  60. }
  61. }
  62. }

9:乘方问题

          
          
  1. package 算法;
  2. import java.util.Scanner;
  3. //一行,包含两个整数a和n。-1000000 <= a <= 1000000,1 <= n <= 10000。
  4. //一个整数,即乘方结果。题目保证最终结果的绝对值不超过1000000。
  5. public class 乘方问题 {
  6. public static void main(String[] args) {
  7. Scanner sc=new Scanner(System.in);
  8. int a=sc.nextInt();
  9. int n=sc.nextInt();
  10. int prodect=1; //初始数据
  11. //控制乘a的个数
  12. for (int i = 0; i < n; i++) {
  13. prodect*=a;
  14. }
  15. System.out.println(prodect);
  16. }
  17. }

10:虫子和苹果2

           
           
  1. package 算法;
  2. /*
  3. *你买了一箱n个苹果,很不幸的是买完时箱子里混进了一条虫子。虫子每x小时能吃掉一个苹果,假设虫子在吃完一个苹果之前不会吃另一个,那么经过y小时你还有多少个完整的苹果?
  4. 输入
  5. 输入仅一行,包括n,x和y(均为整数)。
  6. 输出
  7. 输出也仅一行,剩下的苹果个数
  8. *
  9. * */
  10. import java.util.Scanner;
  11. public class 虫子和苹果2 {
  12. public static void main(String[] args) {
  13. Scanner sc = new Scanner(System.in);
  14. int y = sc.nextInt();
  15. int apple = 0; //苹果的剩余个数
  16. double x = sc.nextDouble();
  17. int z = sc.nextInt();
  18. //总苹果树减去被虫子吃过的苹果数(向上取整)
  19. apple = y - (int) (Math.ceil(z / x));
  20. //对剩余苹果数进行判断输出
  21. if (apple >= 0)
  22. System.out.println(apple);
  23. else
  24. System.out.println(0);
  25. }
  26. }

11:串的简单处理

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. /*1. 把每个单词的首字母变为大写。
  6. 2. 把数字与字母之间用下划线字符(_)分开,使得更清晰
  7. 3. 把单词中间有多个空格的调整为1个空格。
  8. 例如:
  9. 用户输入:
  10. you and me what cpp2005program
  11. 则程序输出:
  12. You And Me What Cpp_2005_program*/
  13. @SuppressWarnings("all")
  14. public class 串的简单处理 {
  15. public static void main(String[] args) {
  16. Scanner sc = new Scanner(System.in);
  17. System.out.print("请输入你要进行操作的字符串:");
  18. String str = sc.nextLine();
  19. String regex = "\\s+"; // 匹配空格正则
  20. String regex1 = "(\\d+)";// 数字匹配字符串
  21. String regex2 = "[a-zA-Z]+";
  22. String str1 = str.replaceAll(regex, " ");// 将每次出现的空格用一个空格代替
  23. String[] str2 = str1.split(" "); // 利用空格将其拆分为字符数组
  24. String s = "";
  25. String s2 = "";
  26. for (int i = 0; i < str2.length; i++) {
  27. String s1 = str2[i].substring(0, 1).toUpperCase().concat(str2[i].substring(1).toLowerCase());
  28. if (!s1.startsWith(regex1)) {
  29. s2 = s1.replaceAll(regex1, "$1_");
  30. }
  31. else {
  32. s2 = s1.replaceAll(regex1, "_$1_");
  33. }
  34. System.out.print(s2 + " ");
  35. // s += s1 + " ";
  36. }
  37. // System.out.println(s);
  38. }
  39. }

12:串排序

            
            
  1. package 算法;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. /*
  6. * 将01串首先按长度排序,长度相同按1的个数排序,1的个数相同按字典顺序排序。
  7. 输入
  8. 输入包含一个整数n,代表要录入的字符串个数n,接下来输入n个字符串
  9. 输出
  10. 输出排序后的字符串,每行输出一个。
  11. 样例输入
  12. 5
  13. 10010010
  14. 11100010
  15. 100101
  16. 1000111
  17. 11100001
  18. 样例输出
  19. 100101
  20. 1000111
  21. 10010010
  22. 11100001
  23. 11100010
  24. *
  25. * */
  26. @SuppressWarnings("unchecked")
  27. public class 串排序 {
  28. public static void main(String[] args) {
  29. Scanner sc=new Scanner(System.in);
  30. int num=Integer.parseInt(sc.nextLine());
  31. List list=new ArrayList<>();
  32. for (int i = 0; i < num; i++) {
  33. list.add(sc.nextLine());
  34. }
  35. bijiao(list);
  36. for (Object str : list) {
  37. System.out.println(str);
  38. }
  39. }
  40. private static void change(List list,int i,int j) {
  41. String temp=(String) list.get(i);
  42. list.set(i, list.get(j));
  43. list.set(j, temp);
  44. }
  45. private static void bijiao(List list) {
  46. for (int i = 0; i < list.size(); i++) {
  47. for (int j = i+1; j <= list.size()-1; j++) {
  48. if(list.get(i).toString().length()>list.get(j).toString().length()){
  49. change(list, i, j);
  50. }else if(list.get(i).toString().length()==list.get(j).toString().length()){
  51. if(list.get(i).toString().replaceAll("0+", "").length()>list.get(j).toString().replaceAll("0+", "").length()){
  52. change(list, i, j);;
  53. }
  54. if(list.get(i).toString().replaceAll("0+", "").length()==list.get(j).toString().replaceAll("0+", "").length()){
  55. int x=list.get(i).toString().compareTo(list.get(j).toString());
  56. if(x>0){
  57. change(list, i, j);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }

13:行程长度编码

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *在数据压缩中,一个常用的途径是行程长度压缩。对于一个待压缩的字符串而言,我们可以依次记录每个字符及重复的次数。
  5. *这种压缩,对于相邻数据重复较多的情况比较有效。 例如,如果待压缩串为"AAABBBBCBB",则压缩的结果是(A,3)(B,4)(C,1)(B,2)。
  6. *当然,如果相邻字符重复情况较少,则压缩效率就较低。
  7. 现要求根据输入的字符串,得到大小写不敏感压缩后的结果(即所有小写字母均视为相应的大写字母)。
  8. 输入
  9. 一个字符串,长度大于0,且不超过1000,全部由大写或小写字母组成。
  10. 输出
  11. 输出为一行,表示压缩结果,形式为:
  12. (A,3)(B,4)(C,1)(B,2)
  13. 即每对括号内部分别为字符(都为大写)及重复出现的次数,不含任何空格。
  14. 样例输入
  15. aAABBbBCCCaaaaa
  16. 样例输出
  17. (A,3)(B,4)(C,3)(A,5)
  18. *
  19. * */
  20. public class 行程长度编码 {
  21. public static void main(String[] args) {
  22. Scanner sc=new Scanner(System.in);
  23. //输入字符串将其全部转换为大写,并在字符串尾部加一个小写的字母a,用来进行循环控制
  24. String str=sc.nextLine().toUpperCase()+"a";
  25. //定义每个不同字母的计数器
  26. int count=1;
  27. //定义一个长为字符串长度的数组
  28. String[] arr=new String[str.length()];
  29. //定义需要用到的字符串数组长度
  30. int x=0;
  31. //根据数组长度控制循环次数
  32. for (int i = 0; i < str.length()-1; i++) {
  33. //如果前一个字符等于后一个字符则计数器加1
  34. if(str.charAt(i)==str.charAt(i+1)){
  35. count++;
  36. }
  37. else{
  38. //将相等的字符以及出现的次数存入字符串数组中
  39. arr[x]="("+str.charAt(i)+","+count+")";
  40. //每存储一次计数器归1
  41. count=1;
  42. //每存储一次,所用到的字符串数组加1
  43. x++;
  44. }
  45. }
  46. //遍历数组
  47. for (int i = 0; i < x; i++) {
  48. System.out.print(arr[i]);
  49. }
  50. }
  51. }

14:大小写字母互换

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. public class 大小写字母互换 {
  4. public static void main(String[] args) {
  5. Scanner sc=new Scanner(System.in);
  6. String str=sc.nextLine();
  7. //字符存放容器
  8. char[] ch=new char[str.length()];
  9. //根据字符串的长度控制循环语句
  10. for (int i = 0; i < str.length(); i++) {
  11. //对单个字符所在的区间进行判断,将大小写相互转换存放进字符容器
  12. if(str.charAt(i)>='a'&&str.charAt(i)<='z')
  13. ch[i]=(char)(str.charAt(i)-32); //小写转大写减32
  14. else if(str.charAt(i)>='A'&&str.charAt(i)<='Z')
  15. ch[i]=(char)(str.charAt(i)+32); //大写转小写加32
  16. else
  17. ch[i]=str.charAt(i);
  18. }
  19. //将字符数组转换为字符串输出
  20. System.out.println(String.valueOf(ch));
  21. }
  22. }

15:大整数加法

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. public class 大小写字母互换 {
  4. public static void main(String[] args) {
  5. Scanner sc=new Scanner(System.in);
  6. String str=sc.nextLine();
  7. //字符存放容器
  8. char[] ch=new char[str.length()];
  9. //根据字符串的长度控制循环语句
  10. for (int i = 0; i < str.length(); i++) {
  11. //对单个字符所在的区间进行判断,将大小写相互转换存放进字符容器
  12. if(str.charAt(i)>='a'&&str.charAt(i)<='z')
  13. ch[i]=(char)(str.charAt(i)-32); //小写转大写减32
  14. else if(str.charAt(i)>='A'&&str.charAt(i)<='Z')
  15. ch[i]=(char)(str.charAt(i)+32); //大写转小写加32
  16. else
  17. ch[i]=str.charAt(i);
  18. }
  19. //将字符数组转换为字符串输出
  20. System.out.println(String.valueOf(ch));
  21. }
  22. }

16:大整数减法

            
            
  1. package 算法;
  2. import java.math.BigInteger;
  3. import java.util.Scanner;
  4. public class 大整数减法 {
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. String x=sc.nextLine();
  8. String y=sc.nextLine();
  9. //定义BigInteger变量用于存储大整数
  10. BigInteger x1=new BigInteger(x);
  11. BigInteger y1=new BigInteger(y);
  12. //大整数相减
  13. x1=x1.add(y1.negate());
  14. //将大整数转换为字符串输出
  15. System.out.println(x1.toString());
  16. }
  17. }

17:单词排序

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *编写程序,读入一行英文(只包含字母和空格,单词间以单个空格分隔),将所有单词的顺序倒排并输出,依然以单个空格分隔。
  5. 输入
  6. 输入为一个字符串(字符串长度至多为100)。
  7. 输出
  8. 输出为按要求排序后的字符串。
  9. 样例输入
  10. I am a student
  11. 样例输出
  12. student a am I
  13. *
  14. * */
  15. public class 单词倒排 {
  16. public static void main(String[] args) {
  17. Scanner sc=new Scanner(System.in);
  18. String str=sc.nextLine();
  19. String[] arr=str.split(" ");
  20. String str1="";
  21. for (int i = 0; i < arr.length; i++) {
  22. str1+=arr[arr.length-i-1]+" ";
  23. }
  24. System.out.println(str1);
  25. }
  26. }

18:单词的长度

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *输入一行单词序列,相邻单词之间由1个或多个空格间隔,请对应地计算各个单词的长度。
  5. 注意,如果有标点符号(如连字符,逗号),标点符号算作与之相连的词的一部分。没有被空格间开的符号串,都算作单词。
  6. 输入
  7. 一行单词序列,最少1个单词,最多300个单词,单词之间用至少1个空格间隔。单词序列总长度不超过1000。
  8. 输出
  9. 依次输出对应单词的长度,之间以逗号间隔。
  10. 样例输入
  11. She was born in 1990-01-02 and from Beijing city.
  12. 样例输出
  13. 3,3,4,2,10,3,4,7,5
  14. *
  15. * */
  16. public class 单词的长度 {
  17. public static void main(String[] args) {
  18. Scanner sc = new Scanner(System.in);
  19. String str = sc.nextLine().replaceAll(" +", " ");
  20. String[] str1 = str.split(" ");
  21. for (int i = 0; i < str1.length; i++) {
  22. if (i < str1.length - 1)
  23. System.out.print(str1[i].length() + ",");
  24. else
  25. System.out.println(str1[i].length());
  26. }
  27. }
  28. }

19:单词翻转

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *输入一个句子(一行),将句子中的每一个单词翻转后输出。
  5. 输入
  6. 只有一行,为一个字符串,不超过500个字符。单词之间以空格隔开。
  7. 输出
  8. 翻转每一个单词后的字符串,单词之间的空格需与原文一致。
  9. 样例输入
  10. hello world
  11. 样例输出
  12. olleh dlrow
  13. *
  14. * */
  15. public class 单词翻转 {
  16. public static void main(String[] args) {
  17. Scanner sc = new Scanner(System.in);
  18. //给每个输入的字符串后面加一个空格
  19. String str = sc.nextLine() + " ";
  20. //定义 空字符串,用于存储新字符串
  21. String str1 = "";
  22. //定义字符串,用于对每个单词进行操作
  23. String str2 = "";
  24. //根据单词的长度控制循环语句
  25. for (int i = 0; i < str.length(); i++) {
  26. //将每个单词翻转
  27. if (str.charAt(i) != ' ') {
  28. str2 = str.charAt(i) + str2;
  29. } else {
  30. str1 += str2; //将每个单词逐个加起来
  31. str2 = ""; //将存放单词的字符串赋值为空字符串
  32. }
  33. //如果是空格则直接相加
  34. if (str.charAt(i) == ' ')
  35. str1 += str.charAt(i);
  36. }
  37. //由于开=开始给每个字符串尾部加了个空格,因此截取到其字符串长度减1的位置
  38. System.out.println(str1.substring(0, str1.length()-1));
  39. }
  40. }

20:单词替换

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *输入一个字符串,以回车结束(字符串长度<=100)。该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写。
  5. *现需要将其中的某个单词替换成另一个单词,并输出替换之后的字符串。
  6. 输入
  7. 输入包括3行,
  8. 第1行是包含多个单词的字符串 s;
  9. 第2行是待替换的单词a(长度 <= 100);
  10. 第3行是a将被替换的单词b(长度 <= 100).
  11. *
  12. *
  13. * */
  14. public class 单词替换 {
  15. public static void main(String[] args) {
  16. Scanner sc=new Scanner(System.in);
  17. //输入字符串,并将其以空格分割开
  18. String[] str=sc.nextLine().split(" ");
  19. String a=sc.nextLine();
  20. String b=sc.nextLine();
  21. for (int i = 0; i < str.length; i++) {
  22. if(str[i].equals(a))
  23. str[i]=b;
  24. }
  25. for (int i = 0; i < str.length; i++) {
  26. System.out.print(str[i]+" ");
  27. }
  28. }
  29. }

21:点和正方形的关系

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 有一个正方形,四个角的坐标(x,y)分别是(1,-1),(1,1),(-1,-1),(-1,1),
  5. * x是横轴,y是纵轴。写一个程序,判断一个给定的点是否在这个正方形内(包括正方形边界)。
  6. * 输入一行,包括两个整数x、y,以一个空格分开,表示坐标(x,y)。
  7. * 输出一行,如果点在正方形内,则输出yes,否则输出no。
  8. * */
  9. public class 点和正方形的关系 {
  10. public static void main(String[] args) {
  11. Scanner sc=new Scanner(System.in);
  12. int x=sc.nextInt();
  13. int y=sc.nextInt();
  14. //判断坐标点是否在正方形的范围
  15. if(x>=-1&&x<=1&&y>=-1&&y<=1)
  16. System.out.println("yes");
  17. else
  18. System.out.println("no");
  19. }
  20. }

22:方便记忆的电话号码

            
            
  1. package 算法;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. /*
  5. *英文字母(除Q和Z外)和电话号码存在着对应关系,如下所示:
  6. A,B,C -> 2
  7. D,E,F -> 3
  8. G,H,I -> 4
  9. J,K,L -> 5
  10. M,N,O -> 6
  11. P,R,S -> 7
  12. T,U,V -> 8
  13. W,X,Y -> 9
  14. 标准的电话号码格式是xxx-xxxx,其中x表示0-9中的一个数字。有时为了方便记忆电话号码,我们会将电话号码的数字转变为英文字母,
  15. 如把263-7422记成America。有时,我们还加上“-”作为分隔符,如把449-6753记成Hi-World。当然,我们未必要将所有的数字都转变为字母,
  16. 比如474-6635可以记成iPhone-5。
  17. 总之,一个方便记忆的电话号码由数字和除Q、Z外的英文字母组成,并且可以在任意位置插入任意多的“-”符号。
  18. 现在 ,我们有一个列表,记录着许多方便记忆的电话号码。不同的方便记忆的电话号码可能对应相同的标准号码,你的任务就是找出它们。
  19. 输入
  20. 第一行是一个正整数n(n <= 100000),表示列表中的电话号码数。
  21. 其后n行,每行是一个方便记忆的电话号码,它由数字和除Q、Z外的英文字母、“-”符号组成,其中数字和字母的总数一定为7,字符串总长度不超过200。
  22. 输出
  23. 输出包括若干行,每行包括一个标准电话号码(xxx-xxxx)以及它重复出现的次数k(k >= 2),中间用空格分隔。输出的标准电话号码需按照升序排序。
  24. 如果没有重复出现的标准电话号码,则输出一行“No duplicates.”。
  25. 样例输入
  26. 12
  27. 4873279
  28. ITS-EASY
  29. 888-4567
  30. 3-10-10-10
  31. 888-GLOP
  32. TUT-GLOP
  33. 967-11-11
  34. 310-GINO
  35. F101010
  36. 888-1200
  37. -4-8-7-3-2-7-9-
  38. 487-3279
  39. 样例输出
  40. 310-1010 2
  41. 487-3279 4
  42. 888-4567 3
  43. * */
  44. public class 方便记忆的电话号码 {
  45. public static void main(String[] args) {
  46. Scanner sc=new Scanner(System.in);
  47. int num=sc.nextInt();
  48. //定义一个整型数组用于存放转换后的电话号码
  49. int[] call=new int[num];
  50. //定义相同电话号码计数器
  51. int count=1;
  52. int y=1;
  53. //根据输入的个数控制循环
  54. for (int i = 0; i < num; i++) {
  55. //定义一个字符串用于临时存储电话号码
  56. String phone=sc.next();
  57. //根据字符串的长度判断字符串中是否包含"-",如果包含则去除"-"
  58. phone=phone.replaceAll("-+", "");
  59. //定义一个空字符串,用于连接每个字符
  60. String tel="";
  61. //定义一个空字符,用于临时存放其字母对应的数字
  62. char x=' ';
  63. //对字符串进行判断如果包含字母则将其转换为对应的数字
  64. for (int j = 0; j < phone.length(); j++) {
  65. if('A'<=phone.charAt(j)&&phone.charAt(j)<='C')
  66. x='2';
  67. else if('D'<=phone.charAt(j)&&phone.charAt(j)<='F')
  68. x='3';
  69. else if('G'<=phone.charAt(j)&&phone.charAt(j)<='I')
  70. x='4';
  71. else if('J'<=phone.charAt(j)&&phone.charAt(j)<='L')
  72. x='5';
  73. else if('M'<=phone.charAt(j)&&phone.charAt(j)<='O')
  74. x='6';
  75. else if('P'==phone.charAt(j)||phone.charAt(j)=='R'||phone.charAt(j)=='S')
  76. x='7';
  77. else if('T'<=phone.charAt(j)&&phone.charAt(j)<='V')
  78. x='8';
  79. else if('W'<=phone.charAt(j)&&phone.charAt(j)<='Y')
  80. x='9';
  81. else
  82. x=phone.charAt(j);
  83. tel+=x;
  84. }
  85. //将电话号码转换为整型存放进数组,用于排序
  86. call[i]=Integer.parseInt(tel);
  87. }
  88. //对电话号码进行升序排序
  89. Arrays.sort(call);
  90. //判断电话号码出现的次数
  91. for (int i = 0; i < call.length-1; i++) {
  92. if(call[i]==call[i+1]){
  93. count++;
  94. }else{
  95. if(count>1)
  96. System.out.print(String.valueOf(call[i]).substring(0,3)+"-"+String.valueOf(call[i]).substring(3)+" "+count+"\r\n");
  97. count=1;
  98. y++;
  99. }
  100. if(call[i]==call[i+1]&&i==call.length-2)
  101. System.out.print(String.valueOf(call[i]).substring(0,3)+"-"+String.valueOf(call[i]).substring(3)+" "+count+"\r\n");
  102. }
  103. if(num==1||y==num)
  104. System.out.println("No duplicates.");
  105. }
  106. }

23:斐波那切数列

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. public class 菲波那切数列 {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int x = sc.nextInt();
  7. //对于斐波那契额数列第一第二的数都为1
  8. if (x == 1||x==2) {
  9. System.out.println(1);
  10. } else {
  11. //定义数组,用于存放数字
  12. int[] fib = new int[x];
  13. //令第一第二个数为1
  14. fib[0] = 1;
  15. fib[1] = 1;
  16. //循环控制语句,且后一个数等于前两个数之和
  17. for (int i = 2; i < fib.length; i++) {
  18. fib[i] = fib[i - 1] + fib[i - 2];
  19. }
  20. System.out.println(fib[fib.length-1]);
  21. }
  22. }
  23. }

24:费式数列

            
            
  1. package 算法;
  2. /*Fibonacci为1200年代的欧洲数学家,在他的著作中曾经提到:“若有一只免子每个月生一只小免子,
  3. * 一个月后小免子也开始生产。起初只有一只免子,一个月后就有两只免子,二个月后有三只免子,三个月后有五只免子(小免子投入生产)......”。
  4. 如果不太理解这个例子的话,举个图就知道了,注意新生的小免子需一个月成长期才会投入生产,类似的道理也可以用于植物的生长,
  5. 这就是Fibonacci数列,一般习惯称之为费氏数列,例如以下:
  6. 1、1 、2、3、5、8、13、21、34、55、89...... */
  7. public class 费式数列 {
  8. public static void main(String[] args) {
  9. int[] fib=new int[2];
  10. fib[0]=0;
  11. fib[1]=1;
  12. for(int i=2;i<fib.length;i++){
  13. fib[i]=fib[i-1]+fib[i-2];
  14. }
  15. for(int i=0;i<fib.length;i++){
  16. System.out.print(fib[i]+" ");
  17. }
  18. System.out.println();
  19. }
  20. }

25:分离整数的各个位数

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //给定一个整数,要求从个位开始分离出它的每一位数字
  4. //从个位开始按照从低位到高位的顺序依次输出每一位数字。数字之间以一个空格分开。
  5. public class 分离整数的各个位数 {
  6. public static void main(String[] args) {
  7. Scanner sc=new Scanner(System.in);
  8. int num=sc.nextInt();
  9. //将整数转换为字符串,并计算其长度
  10. int lon=String.valueOf(num).length();
  11. //定义存放单个数字的容器
  12. int[] s=new int[String.valueOf(num).length()];
  13. if(num<10){
  14. System.out.println(num);
  15. }
  16. else{
  17. for(int i=0;i<lon;i++){
  18. //依次取出数字的低位和高位并存放进容器
  19. s[i]=num%10;
  20. num=num/10;
  21. }
  22. for(int i=0;i<lon;i++){
  23. System.out.print(s[i]+" ");
  24. }
  25. }
  26. }
  27. }

26:根据公式计算

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 对某些带电感L和电阻R的电路,其自然衰减频率由等式:频率=给定L、R,希望研究频率随电容C的波动情况。
  5. * 请编程,用于计算从0.01到0.1、步长为0.01的不同C值时的频率(保留3位小数)。
  6. * 输入一行,包含两个单精度浮点数,表示L 和 R的值。数与数之间以空格分开。
  7. (L > 0, R >= 0, L * R2 <= 0.04)
  8. 输出10行,每行一个数,对应C值从小到大计算所得的频率,每个数保留3位小数。
  9. * */
  10. public class 根据公式计算 {
  11. public static void main(String[] args) {
  12. Scanner sc = new Scanner(System.in);
  13. float l = sc.nextFloat();
  14. float r = sc.nextFloat();
  15. float c = 0;
  16. for (int i = 0; i < 10; i++) {
  17. c = (float) (c + 0.01);
  18. System.out.printf("%.3f", Math.sqrt(1 / (l * c) - r * r / (4 * c * c)));
  19. System.out.println();
  20. }
  21. }
  22. }

27:过滤多余的空格

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. import org.omg.Messaging.SyncScopeHelper;
  4. /*
  5. *一个句子中也许有多个连续空格,过滤掉多余的空格,只留下一个空格。
  6. 输入
  7. 一行,一个字符串(长度不超过200),句子的头和尾都没有空格。
  8. 输出
  9. 过滤之后的句子。
  10. 样例输入
  11. Hello world.This is c language.
  12. 样例输出
  13. Hello world.This is c language
  14. *
  15. * */
  16. public class 过滤多余的空格 {
  17. public static void main(String[] args) {
  18. Scanner sc=new Scanner(System.in);
  19. String str=sc.nextLine().replaceAll(" +", " ");
  20. System.out.println(str);
  21. }
  22. }

28:紧急措施

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *近日,一些热门网站遭受黑客入侵,这些网站的账号、密码及email的数据惨遭泄露。你在这些网站上注册若干账号(使用的用户名不一定相同),但是注册时使用了相同的email。你此时拿到了那份泄露的数据,希望尽快将自己的密码更改。策略如下:根据email找到你的用户名和密码,然后更改密码。更改的规则为:小写和大写交换,非字母字符保持不变。
  5. 输入
  6. 第一行为你的email地址,长度不超过50个字符且只包含字母、数字和‘@’符号。
  7. 第二行为账号数N,N(0 < N < 10000)。
  8. 接下来N行,每行表示一个账号,格式为:
  9. 用户名 密码 email
  10. 它们之间用单个空格分开。用户名、密码、email均不含空格,且长度不超过50个字符。
  11. 输出
  12. 有若干行,每行为你的一个账号,包括:你的账号,修改后的密码(之间用单个空格分隔)。
  13. 如果没有你的账号,则输出empty。
  14. *
  15. * */
  16. public class 紧急措施 {
  17. public static void main(String[] args) {
  18. Scanner sc=new Scanner(System.in);
  19. //email地址
  20. String str=sc.nextLine();
  21. //账号数
  22. int x=Integer.parseInt(sc.nextLine());
  23. String[] str1=new String[x];
  24. //非自己email账号计数器
  25. int count=0;
  26. //根据账号数控制循环语句
  27. for (int i = 0; i < x; i++) {
  28. //输入用户名,密码,email并将其根据空格截取放入字符串数组中
  29. String[] str2=sc.nextLine().split(" ");
  30. //判断email账号是否为自己的
  31. if(str2[2].equals(str)){
  32. String str3=str2[0]+" ";
  33. //将自己的原来密码大小写互换得到新的密码,非字母字符保持不变
  34. for (int j = 0; j < str2[1].length(); j++) {
  35. if('a'<=str2[1].charAt(j)&&str2[1].charAt(j)<='z')
  36. str3+=(char)(str2[1].charAt(j)-32);
  37. else if('A'<=str2[1].charAt(j)&&str2[1].charAt(j)<='Z')
  38. str3+=(char)(str2[1].charAt(j)+32);
  39. else
  40. str3+=str2[1].charAt(j);
  41. }
  42. System.out.println(str3);
  43. }
  44. else
  45. count++;
  46. }
  47. if(count==x)
  48. System.out.println("empty");
  49. }
  50. }

29:合影效果

            
            
  1. package 算法;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. /*
  5. *小云和朋友们去爬香山,为美丽的景色所陶醉,想合影留念。如果他们站成一排,男生全部在左(从拍照者的角度),
  6. *并按照从矮到高的顺序从左到右排,女生全部在右,并按照从高到矮的顺序从左到右排,请问他们合影的效果是什么样的(所有人的身高都不同)?
  7. 输入
  8. 第一行是人数n(2 <= n <= 40,且至少有1个男生和1个女生)。
  9. 后面紧跟n行,每行输入一个人的性别(男male或女female)和身高(浮点数,单位米),两个数据之间以空格分隔。
  10. 输出
  11. n个浮点数,模拟站好队后,拍照者眼中从左到右每个人的身高。每个浮点数需保留到小数点后2位,相邻两个数之间用单个空格隔开。
  12. 样例输入
  13. 6
  14. male 1.72
  15. male 1.78
  16. female 1.61
  17. male 1.65
  18. female 1.70
  19. female 1.56
  20. 样例输出
  21. 1.65 1.72 1.78 1.70 1.61 1.56
  22. *
  23. * */
  24. public class 合影效果 {
  25. public static void main(String[] args) {
  26. Scanner sc=new Scanner(System.in);
  27. int num=sc.nextInt();
  28. //定义存放男女生身高的数组
  29. double[] male=new double[num];
  30. double[] female=new double[num];
  31. //定义男生人数计数器
  32. int count=0;
  33. for (int i = 0; i < num; i++) {
  34. //输入性别和年龄,若为男生则将计数器加1,将身高存入male数组,反之存入female数组
  35. String sex=sc.next();
  36. double high=sc.nextDouble();
  37. if(sex.equals("male")){
  38. count++;
  39. male[i]=high;
  40. }else
  41. female[i]=high;
  42. }
  43. //利用数组工具类对男女身高进行排序输出
  44. Arrays.sort(male);
  45. Arrays.sort(female);
  46. for (int i = male.length-count; i < male.length; i++) {
  47. System.out.printf("%.2f"+"%s",male[i]," ");
  48. }
  49. for (int i = female.length-1; i>=count ; i--) {
  50. System.out.printf("%.2f"+"%s",female[i]," ");
  51. }
  52. }
  53. }

30:黑马歌手大赛

            
            
  1. package 算法;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. /*
  5. *某年某日,为了庆祝黑马程序员成立一百周年,西安黑马中心进行了一次青年歌手大奖赛,每位评委会给参赛选手打分。
  6. 选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程实现,输出平均分,保留到小数点后两位。
  7. 输入
  8. 第一个数是n(2 < n < 100),表示评委的人数,然后是n个评委的打分,打分分数均为小于或等于100的正整数。
  9. 输出
  10. 输出平均得分,保留到小数点后两位
  11. *
  12. *
  13. * */
  14. public class 黑马歌手大赛 {
  15. public static void main(String[] args) {
  16. Scanner sc=new Scanner(System.in);
  17. int num=sc.nextInt();
  18. int[] arr=new int[num];
  19. double sum=0;
  20. for(int i=0;i<arr.length;i++){
  21. arr[i]=sc.nextInt();
  22. }
  23. Arrays.sort(arr);
  24. for(int i=1;i<arr.length-1;i++){
  25. sum+=arr[i];
  26. }
  27. System.out.printf("%.2f",sum/(num-2));
  28. }
  29. }

31:黑马歌手大赛1

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *某年某日,为了庆祝黑马程序员成立一百周年,西安黑马中心进行了一次青年歌手大奖赛,每位评委会给参赛选手打分。
  5. 选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程实现,输出平均分,保留到小数点后两位。
  6. 输入
  7. 第一个数是n(2 < n < 100),表示评委的人数,然后是n个评委的打分,打分分数均为小于或等于100的正整数。
  8. 输出
  9. 输出平均得分,保留到小数点后两位
  10. 样例输入
  11. 10
  12. 1 2 3 4 5 6 7 8 9 10
  13. 样例输出
  14. 5.50
  15. *
  16. * */
  17. public class 黑马歌手大赛1 {
  18. public static void main(String[] args) {
  19. Scanner sc=new Scanner(System.in);
  20. int num=sc.nextInt();
  21. int max=1;
  22. int min = 100;
  23. double sum=0;
  24. for (int i = 0; i < num; i++) {
  25. int x=sc.nextInt();
  26. if(max<x)
  27. max=x;
  28. if(min>x)
  29. min=x;
  30. sum+=x;
  31. }
  32. System.out.printf("%.2f",(sum-min-max)/(num-2));
  33. }
  34. }

32:猴子约瑟夫问题

            
            
  1. package 算法;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. /*
  5. * 约瑟夫问题:有n只猴子,按顺时针方向围成一圈选大王(编号从1到n),从第1号开始报数,
  6. * 一直数到m,数到m的猴子退出圈外,剩下的猴子再接着从1开始报数。就这样,直到圈内只剩下
  7. * 一只猴子时,这个猴子就是猴王,编程求输入n,m后,输出最后猴王的编号。
  8. * */
  9. import java.util.Scanner;
  10. public class 猴子约瑟夫问题 {
  11. public static void main(String[] args) {
  12. List list = new ArrayList();
  13. while (true) {
  14. Scanner sc = new Scanner(System.in);
  15. int num = sc.nextInt();
  16. int y = sc.nextInt();
  17. if (num == 0 && y == 0)
  18. break;
  19. if (y == 1) {
  20. list.add(num);
  21. }
  22. int[] people = new int[num];
  23. int x = 0;
  24. for (int i = 0; i < people.length; i++) {
  25. people[i] = 1;
  26. }
  27. if (y > 1) {
  28. while (true) {
  29. for (int i = 0; i < people.length; i++) {
  30. if (people[i] == 1) {
  31. x++;
  32. if (x % y == 0) {
  33. people[i] = 0;
  34. num--;
  35. }
  36. }
  37. }
  38. if (num == 1) {
  39. break;
  40. }
  41. }
  42. }
  43. for (int i = 0; i < people.length; i++) {
  44. if (people[i] == 1&&y!=1) {
  45. list.add(i + 1);
  46. }
  47. }
  48. }
  49. for (int i = 0; i < list.size(); i++) {
  50. System.out.println(list.get(i));
  51. }
  52. }
  53. }

33:鸡兔同笼

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *一个笼子里面关了鸡和兔子(鸡有2只脚,兔子有4只脚,没有例外)。已经知道了笼子里面脚的总数a,问笼子里面至少有多少只动物,至多有多少只动物。
  5. 输入
  6. 一行,一个正整数a (a < 32768)。
  7. 输出
  8. 一行,包含两个正整数,第一个是最少的动物数,第二个是最多的动物数,两个正整数用一个空格分开。
  9. 如果没有满足要求的答案,则输出两个0,中间用一个空格分开。
  10. * */
  11. public class 鸡兔同笼 {
  12. public static void main(String[] args) {
  13. Scanner sc = new Scanner(System.in);
  14. double x = sc.nextDouble();
  15. int y = (int) Math.ceil(x / 4);
  16. int z = (int) Math.ceil(x / 2);
  17. //注意没有一只脚和三只脚的动物
  18. if (x % 4 == 2 && x % 2 == 0)
  19. System.out.println(y + " " + z);
  20. else
  21. System.out.println(0 + " " + 0);
  22. }
  23. }

34:计算2的N次方

            
            
  1. package 算法;
  2. import java.math.BigInteger;
  3. import java.util.Scanner;
  4. public class 计算2N次方 {
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. int x=sc.nextInt();
  8. BigInteger bi=new BigInteger("2");
  9. BigInteger sum=new BigInteger("1");
  10. for (int i = 0; i < x; i++) {
  11. sum=sum.multiply(bi);
  12. }
  13. System.out.println(sum.toString());
  14. }
  15. }

35:计算邮资

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 根据邮件的重量和用户是否选择加急计算邮费。计算规则:重量在1000克以内(包括1000克),
  5. * 基本费8元。超过1000克的部分,每500克加收超重费4元,不足500克部分按500克计算;如果用户选择加急,多收5元。
  6. * 输入一行,包含整数和一个字符,以一个空格分开,分别表示重量(单位为克)和是否加急。如果字符是y,
  7. * 说明选择加急;如果字符是n,说明不加急。
  8. * */
  9. public class 计算邮资 {
  10. public static void main(String[] args) {
  11. Scanner sc=new Scanner(System.in);
  12. int x=sc.nextInt();
  13. double money=0;
  14. char m=sc.next().charAt(0);
  15. //对重量进行判断
  16. if(x>=1000)
  17. //对于不满500的进行向上取整
  18. money=8+Math.ceil((x-1000)/500.0)*4;
  19. else
  20. money=8;
  21. //对是否加急进行判断,并转换为整型输出
  22. if(m=='y')
  23. System.out.println((int)(money+5));
  24. else
  25. System.out.println((int)money);
  26. }
  27. }

36:简单计算器

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //输入只有一行,共有三个参数,其中第1、2个参数为整数,第3个参数为操作符(+,-,*,/)。
  4. //输出只有一行,一个整数,为运算结果。然而:
  5. //1. 如果出现除数为0的情况,则输出:Divided by zero!
  6. //2. 如果出现无效的操作符(即不为 +, -, *, / 之一),则输出:Invalid operator!
  7. public class 简单计算器 {
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10. int a = sc.nextInt();
  11. int b = sc.nextInt();
  12. char x = sc.next().charAt(0);
  13. if (b == 0 && x == '/')
  14. System.out.println("Divided by zero!");
  15. else {
  16. switch (x) {
  17. case '+':
  18. System.out.println(a + b);
  19. break;
  20. case '-':
  21. System.out.println(a - b);
  22. break;
  23. case '*':
  24. System.out.println(a * b);
  25. break;
  26. case '/':
  27. System.out.println(a / b);
  28. break;
  29. default:
  30. System.out.println("Invalid operator!");
  31. break;
  32. }
  33. }
  34. }
  35. }

37:简单密码

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. Julius Caesar曾经使用过一种很简单的密码。对于明文中的每个字符,将它用它字母表中后5位对应的字符来代替,
  5. 这样就得到了密文。比如字符A用F来代替。如下是密文和明文中字符的对应关系。
  6. 密文
  7. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
  8. 明文
  9. V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
  10. 你的任务是对给定的密文进行解密得到明文。
  11. 你需要注意的是,密文中出现的字母都是大写字母。密文中也包括非字母的字符,对这些字符不用进行解码
  12. */
  13. public class 简单密码 {
  14. public static void main(String[] args) {
  15. Scanner sc=new Scanner(System.in);
  16. String str=sc.nextLine();
  17. String str1="";
  18. for (int i = 0; i < str.length(); i++) {
  19. if('A'<=str.charAt(i)&&str.charAt(i)<'F'){
  20. str1+=(char)(91-('A'-str.charAt(i)+5));
  21. }
  22. else if('F'<=str.charAt(i)&&str.charAt(i)<='Z'){
  23. str1+=(char)(str.charAt(i)-5);
  24. }
  25. else
  26. str1+=str.charAt(i);
  27. }
  28. System.out.println(str1);
  29. }
  30. }

38:将字符串中的小写字母转换成大写字母

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. public class 将字符串中的小写字母转换成大写字母 {
  4. public static void main(String[] args) {
  5. Scanner sc=new Scanner(System.in);
  6. String str=sc.nextLine();
  7. char[] strr=new char[str.length()];
  8. for (int i = 0; i < str.length(); i++) {
  9. if(str.charAt(i)>='a'&&str.charAt(i)<='z'){
  10. strr[i]=(char)(str.charAt(i)-32);
  11. }
  12. else
  13. strr[i]=str.charAt(i);
  14. }
  15. System.out.println(String.valueOf(strr));
  16. }
  17. }

39:角谷猜想

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 所谓角谷猜想,是指对于任意一个正整数,如果是奇数,则乘3加1,如果是偶数,则除以2,
  5. * 得到的结果再按照上述规则重复处理,最终总能够得到1。如,假定初始整数为5,计算过程分别为
  6. * 16、8、4、2、1。程序要求输入一个整数,将经过处理得到1的过程输出来。
  7. *
  8. * */
  9. public class 角谷猜想 {
  10. public static void main(String[] args) {
  11. Scanner sc=new Scanner(System.in);
  12. int x=sc.nextInt();
  13. while(x!=1){
  14. if(x%2==1){
  15. System.out.println(x+"*"+3+"+"+1+"="+(3*x+1));
  16. x=3*x+1;
  17. }
  18. else{
  19. System.out.println(x+"/"+2+"="+(x/2));
  20. x=x/2;
  21. }
  22. }
  23. System.out.println("End");
  24. }
  25. }

40:阶乘求和

            
            
  1. package 算法;
  2. import java.math.BigInteger;
  3. import java.util.Scanner;
  4. public class 阶乘求和 {
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. int x=sc.nextInt();
  8. BigInteger sum=new BigInteger("0");
  9. for (int i = 1; i <= x; i++) {
  10. BigInteger rid=new BigInteger("1");
  11. for (int j = 1; j <= i; j++) {
  12. BigInteger k=BigInteger.valueOf(j);
  13. rid=rid.multiply(k);
  14. }
  15. sum=sum.add(rid);
  16. }
  17. System.out.println(sum.toString());
  18. }
  19. }

41:金币

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 国王将金币作为工资,发放给忠诚的骑士。第一天,骑士收到一枚金币;
  5. * 之后两天(第二天和第三天)里,每天收到两枚金币;之后三天(第四、
  6. * 五、六天)里,每天收到三枚金币;之后四天(第七、八、九、十天)
  7. * 里,每天收到四枚金币……这种工资发放模式会一直这样延续下去:当连
  8. * 续N天每天收到N枚金币后,骑士会在之后的连续N+1天里,每天收到
  9. * N+1枚金币(N为任意正整数)。
  10. 你需要编写一个程序,确定从第一天开始的给定天数内,骑士一共获得了多少金币
  11. *
  12. * */
  13. public class 金币 {
  14. public static void main(String[] args) {
  15. Scanner sc=new Scanner(System.in);
  16. int time=sc.nextInt(); //输入的是天数
  17. int count=1; //表示第一天,之后两天,之后三天,之后四天......中的天数也代表每天收到的金币数
  18. int sum=0; //虚拟总天数(判断time发工资的区间)
  19. int money=0; //得到金币数
  20. //当虚拟天数大于输入的天数时循环结束
  21. while(true){
  22. sum=sum+count;
  23. if(sum<time)
  24. count++;
  25. else
  26. break;
  27. }
  28. //计算前count天数的金币数(不包含count)1 2*2 3*3 4*4
  29. for(int i=1;i<count;i++){
  30. money=money+i*i;
  31. }
  32. //计算总金币数,即前count天的金币数+最后一天获得的金币数(由于最后一个count的天数不一定干满,所以)
  33. money=money+(count-(sum-time))*count;
  34. System.out.println(money);
  35. }
  36. }

42:奇数单增序列

            
            
  1. package 算法;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. /*
  5. *给定一个长度为N(不大于500)的正整数序列,请将其中的所有奇数取出,并按升序输出。
  6. 输入
  7. 共2行:
  8. 第1行为 N;
  9. 第2行为 N 个正整数,其间用空格间隔。
  10. 输出
  11. 增序输出的奇数序列,数据之间以逗号间隔。数据保证至少有一个奇数。
  12. 样例输入
  13. 10
  14. 1 3 2 6 5 4 9 8 7 10
  15. 样例输出
  16. 1,3,5,7,9
  17. *
  18. * */
  19. public class 奇数单增序列 {
  20. public static void main(String[] args) {
  21. Scanner sc = new Scanner(System.in);
  22. int num = sc.nextInt();
  23. // 定义整型数组用存放数字
  24. int[] arr = new int[num];
  25. // 定义奇数的个数
  26. int y = 0;
  27. // 循环输入整数
  28. for (int i = 0; i < num; i++) {
  29. int x = sc.nextInt();
  30. // 判断输入的整数是否为奇数,如果为奇数则将其存放进数组,并将计数器加1
  31. if (x % 2 == 1) {
  32. arr[i] = x;
  33. y++;
  34. }
  35. }
  36. // 利用数组工具类对数组中的数字进行排序
  37. Arrays.sort(arr);
  38. // 输出数组中的奇数
  39. for (int i = arr.length - y; i < arr.length; i++) {
  40. // 判断加","的条件
  41. if (i != arr.length - 1)
  42. System.out.print(arr[i] + ",");
  43. else
  44. System.out.print(arr[i]);
  45. }
  46. }
  47. }

43:晶晶赴约会

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //晶晶的朋友贝贝约晶晶下周一起去看展览,但晶晶每周的1、3、5有课必须上课,
  4. //请帮晶晶判断她能否接受贝贝的邀请,如果能输出YES;如果不能则输出NO。
  5. //输入有一行,贝贝邀请晶晶去看展览的日期,用数字1到7表示从星期一到星期日。
  6. //输出有一行,如果晶晶可以接受贝贝的邀请,输出YES,否则,输出NO。注意YES和NO都是大写字母!
  7. public class 晶晶赴约会 {
  8. public static void main(String[] args) {
  9. Scanner sc=new Scanner(System.in);
  10. int x=sc.nextInt();
  11. if(x==1||x==3||x==5)
  12. System.out.println("NO");
  13. else
  14. System.out.println("YES");
  15. }
  16. }

44:九九乘法表

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. public class 九九乘法表 {
  4. public static void main(String[] args) {
  5. Scanner sc=new Scanner(System.in);
  6. System.out.print("请输入你要打印的乘法表数:");
  7. int x=sc.nextInt();
  8. for (int i = 1; i <=x; i++) {
  9. for (int j = 1; j <=i; j++) {
  10. System.out.print(i+"*"+j+"="+i*j+" ");
  11. }
  12. System.out.println();
  13. }
  14. }
  15. }

45:救援

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 救生船从大本营出发,营救若干屋顶上的人回到大本营,屋顶数目以及每个屋顶的坐标和人数都将由输入决定,
  5. * 求出所有人都到达大本营并登陆所用的时间。在直角坐标系的原点是大本营,救生船每次从大本营出发,
  6. * 救了人之后将人送回大本营。坐标系中的点代表屋顶,每个屋顶由其位置坐标和其上的人数表 示。救生船每次从大
  7. * 本营出发,以速度50 米/分钟驶向下一个屋顶,达到一个屋顶后,救下其上的所有人,每人上船1 分钟,船原路
  8. * 返回,达到大本营,每人下船0.5 分钟。假设原点与任意一个屋顶的连线不穿过其它屋顶。
  9. *
  10. * 第一行,一个整数,表示屋顶数n。接下来依次有n 行输入,每一行上包含两个表示屋顶相对于大本营的平面坐标位
  11. * 置的实数(单位是米)、一个表示人数的整数,数之间以一个空格分开。
  12. * 一行,救援需要的总时间,精确到分钟 (向上取整)。
  13. * */
  14. public class 救援 {
  15. public static void main(String[] args) {
  16. Scanner sc = new Scanner(System.in);
  17. int x = sc.nextInt();
  18. double[] num = new double[3 * x];
  19. double times = 0;
  20. for (int j = 0; j < x; j++) {
  21. for (int i = 0; i < 3; i++) {
  22. num[i] = sc.nextDouble();
  23. }
  24. times += 2 * Math.sqrt(num[0] * num[0] + num[1] * num[1]) / 50 + num[2] * 1.5;
  25. }
  26. System.out.println((int) Math.ceil(times));
  27. }
  28. }

46:买房子

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 某程序员开始工作,年薪N万,他希望在中关村公馆买一套60平米的房子,现在价格是200万,
  5. * 假设房子价格以每年百分之K增长,并且该程序员未来年薪不变,且不吃不喝,不用交税,每年
  6. * 所得N万全都积攒起来,问第几年能够买下这套房子?(第一年年薪N万,房价200万)
  7. * 一行,包含两个正整数N(10 <= N <= 50), K(1 <= K <= 20),中间用单个空格隔开。
  8. * 如果在第20年或者之前就能买下这套房子,则输出一个整数M,表示最早需要在第M年能买下,否则输出Impossible。
  9. * */
  10. public class 买房子 {
  11. public static void main(String[] args) {
  12. Scanner sc = new Scanner(System.in);
  13. int n = sc.nextInt();
  14. int k = sc.nextInt();
  15. double sum = 200;
  16. int money = n;
  17. int time = 1;
  18. while (sum>money) {
  19. time++;
  20. money += n;
  21. sum+=sum * k / 100;
  22. if (time > 20) {
  23. System.out.println("Impossible");
  24. return;
  25. }
  26. }
  27. System.out.println(time);
  28. }
  29. }

47:密码翻译

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 在情报传递过程中,为了防止情报被截获,往往需要对情报用一定的方式加密,简单的加密算法虽然不足以完全避免情报被破译,
  5. * 但仍然能防止情报被轻易的识别。我们给出一种最简的的加密方法,对给定的一个字符串,把其中从a-y,A-Y的字母用其后继字母替代,
  6. * 把z和Z用a和A替代,其他非字母字符不变,则可得到一个简单的加密字符串。
  7. * */
  8. public class 密码翻译 {
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11. String str = sc.nextLine();
  12. String strr = "";
  13. char a = ' ';
  14. for (int i = 0; i < str.length(); i++) {
  15. if (str.charAt(i) >= 'a' && str.charAt(i) <= 'y' || str.charAt(i) >= 'A' && str.charAt(i) <= 'Y')
  16. a = (char) (str.charAt(i) + 1);
  17. else if(str.charAt(i)=='z')
  18. a='a';
  19. else if(str.charAt(i)=='Z')
  20. a='A';
  21. else
  22. a=str.charAt(i);
  23. strr = strr + a;
  24. }
  25. System.out.println(strr);
  26. }
  27. }

48:年龄与疾病

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 某医院想统计一下某项疾病的获得与否与年龄是否有关,需要对以前的诊断记录进行整理,
  5. * 按照0-18、19-35、36-60、61以上(含61)四个年龄段统计的患病人数占总患病人数的比例。
  6. * 共2行,第一行为过往病人的数目n(0 < n <= 100),第二行为每个病人患病时的年龄。
  7. * 按照0-18、19-35、36-60、61以上(含61)四个年龄段输出该段患病人数占总患病人数的比例,
  8. * 以百分比的形式输出,精确到小数点后两位。每个年龄段占一行,共四行。
  9. *
  10. * */
  11. public class 年龄与疾病 {
  12. public static void main(String[] args) {
  13. Scanner sc=new Scanner(System.in);
  14. int x=sc.nextInt();
  15. int y=0;
  16. int a=0;
  17. int b=0;
  18. int c=0;
  19. int d=0;
  20. for(int i=0;i<x;i++){
  21. y=sc.nextInt();
  22. if(y>0&&y<=18)
  23. a++;
  24. if(y>=19&&y<=35)
  25. b++;
  26. if(y>=36&&y<=60)
  27. c++;
  28. if(y>=61)
  29. d++;
  30. }
  31. System.out.printf("%.2f"+"%s"+"%.2f"+"%s"+"%.2f"+"%s"+"%.2f"+"%s",a*1.0*100/x,"%"+"\n",b*1.0*100/x,"%"+"\n",c*1.0*100/x,"%"+"\n",d*1.0*100/x,"%"+"\n");
  32. }
  33. }

49:排序查找一起练

            
            
  1. package 算法;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. /*
  5. *对一个指定大小为n的整数序列,先进行排序(从小到大),然后通过二分查找来查找一个元素m,要去打印出排序后数组,
  6. *元素在序列里的位置,并统计处查找的次数。输出格式请参考示例输出
  7. 输入
  8. 输入包括两行:
  9. 第一行输入整数n(n<500)和需要查找的数值m
  10. 第二行依次输入n个整数
  11. 输出
  12. 输出包括两行:
  13. 第一行输出 排序后的序列
  14. 第二行输出m在序列中位置和查找的次数,如果没有找到,请输出-1,两个数字间用空格隔开。
  15. 样例输入
  16. 10 5
  17. 11 12 91 38 20 5 23 99 12 4
  18. 样例输出
  19. [4,5,11,12,12,20,23,38,91,99]
  20. 1 2
  21. *
  22. * */
  23. public class 排序查找一起练 {
  24. public static void main(String[] args) {
  25. Scanner sc=new Scanner(System.in);
  26. int num=sc.nextInt();
  27. int x=sc.nextInt();
  28. int[] nums=new int[num];
  29. int min = 0;
  30. int max = nums.length - 1;
  31. int mid = (min + max) / 2;
  32. int count=1;
  33. StringBuilder sb=new StringBuilder("[");
  34. for (int i = 0; i < num; i++) {
  35. nums[i]=sc.nextInt();
  36. }
  37. Arrays.sort(nums);
  38. for (int i = 0; i < nums.length; i++) {
  39. if(i==nums.length-1){
  40. sb.append(nums[i]).append("]");
  41. }else{
  42. sb.append(nums[i]).append(",");
  43. }
  44. }
  45. System.out.println(sb.toString());
  46. while(nums[mid] != x) {
  47. if(nums[mid] < x) {
  48. min = mid + 1;
  49. }else if (nums[mid] > x){
  50. max = mid - 1;
  51. }
  52. mid = (min + max) / 2;
  53. count++;
  54. if(min > max) {
  55. System.out.println(-1+" "+(count-1));
  56. return;
  57. }
  58. }
  59. System.out.println(mid+" "+count);
  60. }
  61. }

50:判断闰年

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. public class 判断闰年 {
  4. public static void main(String[] args) {
  5. Scanner sc=new Scanner(System.in);
  6. int year=sc.nextInt();
  7. if(year%4==0&&year%100!=0||year%400==0){
  8. System.out.println("Y");
  9. }
  10. else
  11. System.out.println("N");
  12. }
  13. }

51:判断正负

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //给定一个整数N,判断其正负。
  4. //如果N > 0, 输出positive;
  5. //如果N = 0, 输出zero;
  6. //如果N < 0, 输出negative
  7. public class 判断数正负 {
  8. public static void main(String[] args) {
  9. Scanner sc=new Scanner(System.in);
  10. int x=sc.nextInt();
  11. if(x>0)
  12. System.out.println("positive");
  13. if(x==0)
  14. System.out.println("zero");
  15. if(x<0)
  16. System.out.println("negative");
  17. }
  18. }

52:判断一个数能否同时被3和5整除

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //输入一行,包含一个整数n。( -1,000,000 < n < 1,000,000)
  4. //输出一行,如果能同时被3和5整除输出YES,否则输出NO
  5. public class 判断一个数能否同时被35整除 {
  6. public static void main(String[] args) {
  7. Scanner sc=new Scanner(System.in);
  8. int x=sc.nextInt();
  9. if(x%3==0&&x%5==0)
  10. System.out.println("YES");
  11. else
  12. System.out.println("NO");
  13. }
  14. }

53:判断字符串是否为回文

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. 描述
  5. 输入一个字符串,输出该字符串是否回文。回文是指顺读和倒读都一样的字符串。
  6. 输入
  7. 输入为一行字符串(字符串中没有空白字符,字符串长度不超过100)。
  8. 输出
  9. 如果字符串是回文,输出yes;否则,输出no。
  10. 样例输入
  11. abcdedcba
  12. 样例输出
  13. yes
  14. *
  15. * */
  16. public class 判断字符串是否为回文 {
  17. public static void main(String[] args) {
  18. Scanner sc = new Scanner(System.in);
  19. String str = sc.nextLine();
  20. StringBuffer str1 = new StringBuffer(str);
  21. String str2 = str1.reverse().toString();
  22. if (str.equals(str2)) {
  23. System.out.println("yes");
  24. } else {
  25. System.out.println("no");
  26. }
  27. }
  28. }

54:奇偶ASCII值判断

           
           
  1. package 算法;
  2. import java.util.Scanner;
  3. //任意输入一个字符,判断其ASCII是否是奇数,若是,输出YES,否则,输出NO
  4. //例如,字符A的ASCII值是65,则输出YES,若输入字符B(ASCII值是66),则输出NO
  5. //如果其ASCII值为奇数,则输出YES,否则,输出NO
  6. public class 奇偶ASCII值判断 {
  7. public static void main(String[] args) {
  8. Scanner sc=new Scanner(System.in);
  9. String y=sc.nextLine();
  10. byte x=(byte)y.charAt(0);
  11. if(x%2==1){
  12. System.out.println("YES");
  13. }else{
  14. System.out.println("NO");
  15. }
  16. }
  17. }

55:奇偶数判断

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //给定一个整数,判断该数是奇数还是偶数
  4. public class 奇偶数判断 {
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. int x=sc.nextInt();
  8. if(x%2==0){
  9. System.out.println("even");
  10. }else{
  11. System.out.println("odd");
  12. }
  13. }
  14. }

56:输出绝对值

           
           
  1. package 算法;
  2. import java.util.Scanner;
  3. //输入一个浮点数,输出这个浮点数的绝对值。
  4. //输出这个浮点数的绝对值,保留到小数点后两位。
  5. public class 输出绝对值 {
  6. public static void main(String[] args) {
  7. Scanner sc=new Scanner(System.in);
  8. System.out.printf("%.2f",Math.abs(sc.nextDouble()));
  9. }
  10. }

57:奇数求和

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //两个数 m 和 n,两个数以一个空格分开,其中 0 <= m <= n <= 300 。
  4. //输出一行,包含一个整数,表示m 到 n(包括m 和 n )之间的所有奇数的和
  5. public class 奇数求和 {
  6. public static void main(String[] args) {
  7. Scanner sc=new Scanner(System.in);
  8. int m=sc.nextInt();
  9. int n=sc.nextInt();
  10. int sum=0;
  11. for(int i=m;i<=n;i++){
  12. if(i%2==1){
  13. sum+=i;
  14. }
  15. }
  16. System.out.println(sum);
  17. }
  18. }

58:骑车与走路

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 在北大校园里,没有自行车,上课办事会很不方便.但实际上,并非去办任何事情都是骑车快,
  5. * 因为骑车总要找车、开锁、停车、锁车等,这要耽误一些时间.假设找到自行车,开锁并车上自
  6. * 行车的时间为27秒;停车锁车的时间为23秒;步行每秒行走1.2米,骑车每秒行走3.0米。
  7. * 请判断走不同的距离去办事,是骑车快还是走路快。
  8. * 输出一行,如果骑车快,输出一行"Bike";如果走路快,输出一行"Walk";如果一样快,输出一行"All"。
  9. * */
  10. public class 骑车与走路 {
  11. public static void main(String[] args) {
  12. Scanner sc=new Scanner(System.in);
  13. double x=sc.nextDouble();
  14. double walk=x/1.2;
  15. double bike=27+23+x/3.0;
  16. if(bike==walk)
  17. System.out.println("All");
  18. else if(bike>walk)
  19. System.out.println("Walk");
  20. else
  21. System.out.println("Bike");
  22. }
  23. }

59:求10000以内n的阶乘

            
            
  1. package 算法;
  2. import java.math.BigInteger;
  3. import java.util.Scanner;
  4. public class 10000以内n的阶乘 {
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. int n=sc.nextInt();
  8. BigInteger ride=new BigInteger("1");
  9. for (int i = 1; i <=n; i++) {
  10. BigInteger j=BigInteger.valueOf(i);
  11. ride=ride.multiply(j);
  12. }
  13. System.out.println(ride);
  14. }
  15. }  

60:求和

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 求Sn = a + aa + aaa + … + aa…a 的值(最后一个数中 a 的个数为 n ),其中 a 是一个1~9的数字,例如:
  5. 2 + 22 + 222 + 2222 + 22222 (此时 a=2 n=5 )
  6. 输入
  7. 一行,包括两个整数,第一个为a,第2个为n(1 ≤ a, n ≤ 9),以空格分隔。
  8. *
  9. * */
  10. public class 求和 {
  11. public static void main(String[] args) {
  12. Scanner sc = new Scanner(System.in);
  13. int num = sc.nextInt();
  14. int a = sc.nextInt();
  15. int sum = 0;
  16. int sn = 0;
  17. for(int j = 0;j < a ; j++){
  18. sn = sn * 10 + num; //循环累加
  19. sum=sum+sn;
  20. }
  21. System.out.println(sum);
  22. }
  23. }

61:求阶乘的和

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //给定正整数n,求不大于n的正整数的阶乘的和(即求1!+2!+3!+...+n!)
  4. public class 求阶乘的和 {
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. int n=sc.nextInt();
  8. int sum=0;
  9. for(int i=1;i<=n;i++){
  10. int a=1;
  11. for(int j=1;j<=i;j++){
  12. a*=j;
  13. }
  14. sum+=a;
  15. }
  16. System.out.println(sum);
  17. }
  18. }

62:求整数的和与均值

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //输入第一行是一个整数n,表示有n个整数。
  4. //第2~n+1行每行包含1个整数。每个整数的绝对值均不超过10000。
  5. public class 求整数的和与均值 {
  6. public static void main(String[] args) {
  7. Scanner sc=new Scanner(System.in);
  8. int num=sc.nextInt();
  9. int sum=0;
  10. for(int i=0;i<num;i++){
  11. sum+=sc.nextInt();
  12. }
  13. System.out.printf(sum+" "+"%.5f",(double)sum/num);
  14. }
  15. }

63:求最大最小值

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. public class 求最大最小值 {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int[] num=new int[3];
  7. for(int i=0;i<3;i++){
  8. num[i]=sc.nextInt();
  9. }
  10. int min=num[0];
  11. for(int i=0;i<num.length;i++){
  12. if(min<num[i]){
  13. min=num[i];
  14. }
  15. }
  16. System.out.println(min);
  17. }
  18. }

64:球弹跳高度的计算

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 一球从某一高度落下(整数,单位米),每次落地后反跳回原来高度的一半,再落下。
  5. 编程计算气球在第10次落地时,共经过多少米? 第10次反弹多高?
  6. 输入一个整数h,表示球的初始高度。
  7. *
  8. * 输出包含两行:
  9. 第1行:到球第10次落地时,一共经过的米数。
  10. 第2行:第10次弹跳的高度。
  11. 注意:结果可能是实数,结果用double类型保存。
  12. 提示:输出时不需要对精度特殊控制,用cout << ANSWER,或者printf("%g", ANSWER)即可。
  13. *
  14. */
  15. public class 球弹跳高度的计算 {
  16. public static void main(String[] args) {
  17. Scanner sc = new Scanner(System.in);
  18. int x = sc.nextInt();
  19. double high = x;
  20. double h = x;
  21. for (int i = 0; i < 10; i++) {
  22. h *= 0.5;
  23. if (i < 9) {
  24. high += 2 * h;
  25. }
  26. }
  27. System.out.printf("%g"+"\n"+"%g", high,h);
  28. }
  29. }

65:人口增长问题

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *我国现有x亿人口,按照每年0.1%的增长速度,n年后将有多少人?
  5. *一行,包含两个整数x和n,分别是人口基数和年数,以单个空格分隔。
  6. *输出最后的人口数,以亿为单位,保留到小数点后四位。1 <= x <= 100, 1 <= n <= 100。
  7. * */
  8. public class 人口增长问题 {
  9. public static void main(String[] args) {
  10. Scanner sc=new Scanner(System.in);
  11. int x=sc.nextInt();
  12. int n=sc.nextInt();
  13. double sum=x;
  14. for(int i=1;i<=n;i++){
  15. sum=sum+sum*0.1/100;
  16. }
  17. System.out.printf("%.4f",sum);
  18. }
  19. }

66:删除单词后缀

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //给定一个单词,如果该单词以er、ly或者ing后缀结尾, 则删除该后缀(题目保证删除后缀后的单词长度不为0), 否则不进行任何操作
  4. public class 删除单词后缀 {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. String str = sc.nextLine();
  8. if (str.endsWith("er") || str.endsWith("ly"))
  9. System.out.println(str.substring(0, str.length() - 2));
  10. else if (str.endsWith("ing"))
  11. System.out.println(str.substring(0, str.length() - 3));
  12. else
  13. System.out.println(str);
  14. }
  15. }

67:石头剪刀布

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*输入包含三行。
  4. 第一行包含三个整数:N,NA,NB,分别表示比了N轮,小A出拳的周期长度,小B出拳的周期长度。0 < N,NA,NB < 100。
  5. 第二行包含NA个整数,表示小A出拳的规律。
  6. 第三行包含NB个整数,表示小B出拳的规律。
  7. 其中,0表示“石头”,2表示“剪刀”,5表示“布”。相邻两个整数之间用单个空格隔开。
  8. 输出
  9. 输出一行,如果小A赢的轮数多,输出A;如果小B赢的轮数多,输出B;如果两人打平,输出draw。
  10. 对于测试数据,猜拳过程为:
  11. 样例输入
  12. 10 3 4
  13. 0 2 5
  14. 0 5 0 2
  15. A:0 2 5 0 2 5 0 2 5 0
  16. B:0 5 0 2 0 5 0 2 0 5
  17. */
  18. public class 石头剪刀布 {
  19. public static void main(String[] args) {
  20. Scanner sc = new Scanner(System.in);
  21. int a = sc.nextInt();
  22. int b = sc.nextInt();
  23. int c = sc.nextInt();
  24. int[] x = new int[b];
  25. int[] y = new int[c];
  26. int[] x1 = new int[a];
  27. int[] y1 = new int[a];
  28. int m = 0;
  29. int n = 0;
  30. int aa = 0;
  31. int bb = 0;
  32. for (int i = 0; i < b; i++) {
  33. x[i] = sc.nextInt();
  34. }
  35. for (int i = 0; i < c; i++) {
  36. y[i] = sc.nextInt();
  37. }
  38. if (a <= b && a <= c) {
  39. for (int i = 0; i < a; i++) {
  40. if (x[i] - y[i] == -2 || x[i] - y[i] == -3 || x[i] - y[i] == 5)
  41. aa++;
  42. if (x[i] - y[i] == -5 || x[i] - y[i] == 2 || x[i] - y[i] == 3)
  43. bb++;
  44. }
  45. } else {
  46. if (a > b) {
  47. for (int i = 0; i < a; i++) {
  48. if (i % b == 0)
  49. m = 0;
  50. x1[i] = x[m];
  51. m++;
  52. }
  53. }
  54. if (a > c) {
  55. for (int i = 0; i < a; i++) {
  56. if (i % c == 0)
  57. n = 0;
  58. y1[i] = y[n];
  59. n++;
  60. }
  61. }
  62. for (int i = 0; i < a; i++) {
  63. if(x1[i]-y1[i]==-2||x1[i]-y1[i]==-3||x1[i]-y1[i]==5)
  64. aa++;
  65. if(x1[i]-y1[i]==-5||x1[i]-y1[i]==2||x1[i]-y1[i]==3)
  66. bb++;
  67. }
  68. }
  69. if (aa > bb)
  70. System.out.println("A");
  71. else if (aa < bb)
  72. System.out.println("B");
  73. else
  74. System.out.println("draw");
  75. }
  76. }

68:完数

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如,6的因子为1、2、3,而6=1+2+3,
  5. * 因此6是"完数"。小明为了梦想,来到黑马程序员学习,点招老师为了测试他的基础水平,让他编程计算出N以内的所有的完数,
  6. * 并按下面格式输出其因子:6 its fastors are 1 2 3
  7. 输入
  8. 输入一个正整数N,测试数据保证N>6
  9. 输出
  10. N以内所有的完数及其因子
  11. *
  12. * */
  13. public class 完数 {
  14. public static void main(String[] args) {
  15. Scanner sc = new Scanner(System.in);
  16. int num = sc.nextInt();
  17. for (int j = 6; j <= num; j++) {
  18. int sum = 0;
  19. String str=" its fastors are";
  20. for (int i = 1; i <= j / 2; i++) {
  21. if (j % i == 0){
  22. sum += i;
  23. str+=" "+i;
  24. }
  25. }
  26. if (j == sum)
  27. System.out.println(j+str);
  28. }
  29. }
  30. }

69:输出最高分数的学生姓名

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *输入学生的人数,然后再输入每位学生的分数和姓名,求获得最高分数的学生的姓名。
  5. 输入
  6. 第一行输入一个正整数N(N <= 100),表示学生人数。接着输入N行,每行格式如下:
  7. 分数 姓名
  8. 分数是一个非负整数,且小于等于100;
  9. 姓名为一个连续的字符串,中间没有空格,长度不超过20。
  10. 数据保证最高分只有一位同学。
  11. 输出
  12. 获得最高分数同学的姓名。
  13. 样例输入
  14. 5
  15. 87 lilei
  16. 99 hanmeimei
  17. 97 lily
  18. 96 lucy
  19. 77 jim
  20. 样例输出
  21. hanmeimei
  22. *
  23. * */
  24. public class 输出最高分数的学生姓名 {
  25. public static void main(String[] args) {
  26. Scanner sc=new Scanner(System.in);
  27. int num=sc.nextInt();
  28. //定义存放成绩和姓名的数组
  29. int[] grade=new int[num];
  30. String[] name=new String[num];
  31. int max=grade[0];
  32. int count=0;
  33. for (int i = 0; i < num; i++) {
  34. grade[i]=sc.nextInt();
  35. name[i]=sc.nextLine();
  36. }
  37. for (int i = 0; i < grade.length; i++) {
  38. if(max<grade[i]){
  39. max=grade[i];
  40. count=i;
  41. }
  42. }
  43. System.out.println(name[count].substring(1));
  44. }
  45. }

70:数1的个数

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 给定一个十进制正整数n,写下从1到n的所有整数,然后数一下其中出现的数字“1”的个数。
  5. * 例如当n=2时,写下1,2。这样只出现了1个“1”;当n=12时,
  6. * 写下1,2,3,4,5,6,7,8,9,10,11,12。这样出现了5个“1”。
  7. * */
  8. public class 1的个数 {
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11. int num = sc.nextInt();
  12. int count = 0;
  13. String str="";
  14. String min="1";
  15. for (int i = 1; i <= num; i++) {
  16. str=str+i;
  17. }
  18. String now=str.replace(min, "");
  19. System.out.println(str.length()-now.length());
  20. }
  21. }

71:数字模式的识别

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *数字的模式是指在一堆给定数字中出现次数最多的数值,如5,5,5,3,3,2,6,4,它的模式就是5。现在你的任务,就是从数字中找到它的模式.
  5. 输入
  6. 第一行为整数N.从第二行开始为N个整数。对于输入的每个数,有( 0<= input_number <= 2000000 ).
  7. 输出
  8. 输出这些数字的模式,如果模式个数不为1,选择它们之中较小的
  9. 样例输入
  10. 8
  11. 5 5 5 3 3 2 6 4
  12. 样例输出
  13. 5
  14. *
  15. * */
  16. public class 数字模式的识别 {
  17. public static void main(String[] args) {
  18. Scanner sc = new Scanner(System.in);
  19. int num = sc.nextInt();
  20. //定义整型数组用于存放数字
  21. int[] arr = new int[num];
  22. //循环控制数字存入数组
  23. for (int i = 0; i < arr.length; i++) {
  24. arr[i] = sc.nextInt();
  25. }
  26. //将x赋值为arr[0]
  27. int x = arr[0];
  28. //y表示相同数字出现的次数
  29. int y = 1;
  30. for (int i = 0; i < arr.length; i++) {
  31. //count表示每轮相同数字出现的次数
  32. int count = 1;
  33. for (int j = i + 1; j < arr.length; j++) {
  34. //出现相等的数字时count++
  35. if (arr[i] == arr[j]) {
  36. count++;
  37. }
  38. //如果后一个数字出现的次数大于前面相同数字的出现次数,则将后面数字出现的次数记录,并将数字记录
  39. if (count > y) {
  40. y = count;
  41. x=arr[i];
  42. }
  43. }
  44. //如果出现相同数字的次数相同则将数字小的记录
  45. if(count==y){
  46. if(x>arr[i])
  47. x=arr[i];
  48. }
  49. }
  50. System.out.println(x);
  51. }
  52. }

72:数字求和

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. public class 数字求和 {
  4. public static void main(String[] args) {
  5. Scanner sc=new Scanner(System.in);
  6. int x=sc.nextInt();
  7. int sum=0;
  8. int y=0;
  9. for (int i = 0; i < 5; i++) {
  10. y=sc.nextInt();
  11. if(y<x)
  12. sum+=y;
  13. }
  14. System.out.println(sum);
  15. }
  16. }

73:数字统计

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *请统计某个给定范围[L, R]的所有整数中,数字2出现的次数。比如给定范围[2, 22]
  5. *数字2在数2中出现了1次,在数12中出现1次,在数20中出现1次,在数21中出现1次,
  6. *在数22中出现2次,所以数字2在该范围内一共出现了6次。
  7. * */
  8. public class 数字统计 {
  9. public static void main(String[] args) {
  10. Scanner sc=new Scanner(System.in);
  11. int one=sc.nextInt();
  12. int two=sc.nextInt();
  13. String str="";
  14. String min="2";
  15. for(int i=one;i<=two;i++){
  16. str=str+i;
  17. }
  18. String now=str.replace(min, "");
  19. System.out.println(str.length()-now.length());
  20. }
  21. }

74:数组逆序重放

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //输入为两行:第一行数组中元素的个数n(1<n<100),第二行是n个整数,每两个整数之间用空格分隔。
  4. public class 数组逆序重放 {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. int x = sc.nextInt();
  8. int[] y = new int[x];
  9. for (int i = 0; i < x; i++) {
  10. y[i] = sc.nextInt();
  11. }
  12. for (int i = 0; i < x; i++) {
  13. System.out.print(y[x-i-1] + " ");
  14. }
  15. }
  16. }

75:数组逆序重放1

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *将一个数组中的值按逆序重新存放。例如,原来的顺序为8,6,5,4,1。要求改为1,4,5,6,8。
  5. 输入
  6. 输入为两行:第一行数组中元素的个数n(1<n<100),第二行是n个整数,每两个整数之间用空格分隔。
  7. 输出
  8. 输出为一行:输出逆序后数组的整数,每两个整数之间用空格分隔。
  9. 样例输入
  10. 5
  11. 8 6 5 4 1
  12. 样例输出
  13. 1 4 5 6 8
  14. *
  15. * */
  16. public class 数组逆序重放1 {
  17. public static void main(String[] args) {
  18. Scanner sc=new Scanner(System.in);
  19. int num=sc.nextInt();
  20. int[] arr=new int[num];
  21. int x=0;
  22. for (int i = 0; i < arr.length; i++) {
  23. arr[i]=sc.nextInt();
  24. }
  25. for (int i = 0; i < arr.length/2; i++) {
  26. x=arr[arr.length-1-i];
  27. arr[arr.length-1-i]=arr[i];
  28. arr[i]=x;
  29. }
  30. for (int i = 0; i < arr.length; i++) {
  31. System.out.print(arr[i]+" ");
  32. }
  33. }
  34. }

76:谁考了第k名

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *在一次考试中,每个学生的成绩都不相同,现知道了每个学生的学号和成绩,求考第k名学生的学号和成绩。
  5. 输入
  6. 第一行有两个整数,分别是学生的人数n(1≤n≤100),和求第k名学生的k(1≤k≤n)。
  7. 其后有n行数据,每行包括一个学号(整数)和一个成绩(浮点数),中间用一个空格分隔。
  8. 输出
  9. 输出第k名学生的学号和成绩,中间用空格分隔。(注:请用%g输出成绩)
  10. 样例输入
  11. 5 3
  12. 90788001 67.8
  13. 90788002 90.3
  14. 90788003 61
  15. 90788004 68.4
  16. 90788005 73.9
  17. 样例输出
  18. 90788004 68.4
  19. *
  20. * */
  21. public class 谁考了第k {
  22. public static void main(String[] args) {
  23. Scanner sc=new Scanner(System.in);
  24. int num=sc.nextInt();
  25. int k=sc.nextInt();
  26. //定义整型数组用于存放学号
  27. int[] id=new int[num];
  28. //定义double类型数组用于存放成绩
  29. double[] grade=new double[num];
  30. for (int i = 0; i <num; i++) {
  31. id[i]=sc.nextInt();
  32. grade[i]=sc.nextDouble();
  33. }
  34. //根据成绩的大小利用冒泡排序法对其进行排序
  35. for (int i = 0; i < grade.length; i++) {
  36. for (int j = i+1; j <= grade.length-1; j++) {
  37. if(grade[i]<grade[j]){
  38. //对成绩进行交换
  39. double temp=grade[i];
  40. grade[i]=grade[j];
  41. grade[j]=temp;
  42. //对姓名进行交换
  43. int st=id[i];
  44. id[i]=id[j];
  45. id[j]=st;
  46. }
  47. }
  48. }
  49. System.out.printf(id[k-1]+" "+"%g",grade[k-1]);
  50. }
  51. }

77:陶陶摘苹果

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *包括两行数据。第一行包含10个100到200之间(包括100和200)的整数(以厘米为单位)
  5. *分别表示10个苹果到地面的高度,两个相邻的整数之间用一个空格隔开。第二行只包括一个100
  6. *到120之间(包含100和120)的整数(以厘米为单位),表示陶陶把手伸直的时候能够达到的最大高度。
  7. * 包括一行,这一行只包含一个整数,表示陶陶能够摘到的苹果的数目。
  8. */
  9. public class 陶陶摘苹果 {
  10. public static void main(String[] args) {
  11. Scanner sc=new Scanner(System.in);
  12. int[] m=new int[10];
  13. int count=0;
  14. for(int i=0;i<10;i++){
  15. m[i]=sc.nextInt();
  16. }
  17. int n=sc.nextInt()+30;
  18. for(int i=0;i<10;i++){
  19. if(m[i]<=n)
  20. count++;
  21. }
  22. System.out.println(count);
  23. }
  24. }

78:统计数字字符个数

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //输入一行字符,统计出其中数字字符的个数。
  4. public class 统计数字字符个数 {
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. String str=sc.nextLine();
  8. int count=0;
  9. for (int i = 0; i < str.length(); i++) {
  10. if('0'<=str.charAt(i)&&str.charAt(i)<='9')
  11. count++;
  12. }
  13. System.out.println(count);
  14. }
  15. }

79:统计水仙花数

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //计算1-100以内的水仙花个数,
  4. public class 统计水仙花数 {
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. int i=sc.nextInt();
  8. for(int count=100;count<=i;count++){
  9. int a=count/100;
  10. int b=count/10%10;
  11. int c=count%10;
  12. if(a*a*a+b*b*b+c*c*c==count)
  13. System.out.println(count);
  14. }
  15. }
  16. }

80:整数序列的元素最大跨度值

            
            
  1. package 算法;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. public class 整数序列的元素最大跨度值 {
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. int num=sc.nextInt();
  8. int[] x=new int[num];
  9. for(int i=0;i<num;i++){
  10. x[i]=sc.nextInt();
  11. }
  12. Arrays.sort(x);
  13. System.out.println(x[x.length-1]-x[0]);
  14. }
  15. }

81:校门外的树

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米。我们可以把马路看成一个数轴
  5. 马路的一端在数轴0的位置,另一端在L的位置;数轴上的每个整数点,即0,1,2,……,L,都种有一棵树。
  6. 由于马路上有一些区域要用来建地铁。这些区域用它们在数轴上的起始点和终止点表示。已知任一区域的起始点
  7. 和终止点的坐标都是整数,区域之间可能有重合的部分。现在要把这些区域中的树(包括区域端点处的两棵树)
  8. 移走。你的任务是计算将这些树都移走后,马路上还有多少棵树。
  9. 第一行有两个整数L(1 <= L <= 10000)和 M(1 <= M <= 100),
  10. L代表马路的长度,M代表区域的数目,L和M之间用一个空格隔开。接下来的M行每行包含两个不同的整数,
  11. 用一个空格隔开,表示一个区域的起始点和终止点的坐标。
  12. 对于20%的数据,区域之间没有重合的部分;
  13. 对于其它的数据,区域之间有重合的情况。
  14. 包括一行,这一行只包含一个整数,表示马路上剩余的树的数目。
  15. * */
  16. public class 校门外的树 {
  17. public static void main(String[] args) {
  18. Scanner sc = new Scanner(System.in);
  19. int x = sc.nextInt();
  20. int y = sc.nextInt();
  21. int[] tree = new int[x+1];
  22. int[] arr = new int[2 * y];
  23. int count=0;
  24. for (int i = 0; i < tree.length; i++) {
  25. tree[i] = 1;
  26. }
  27. for (int i = 0; i < arr.length; i++) {
  28. arr[i] = sc.nextInt();
  29. }
  30. for (int i = 0; i < tree.length; i++) {
  31. for (int j = 0; j < arr.length; j+=2) {
  32. if (arr[j] <= i && i <= arr[j + 1]) {
  33. tree[i] = 0;
  34. }
  35. }
  36. }
  37. for (int i = 0; i < tree.length; i++) {
  38. if(tree[i]==1)
  39. count++;
  40. }
  41. System.out.println(count);
  42. }
  43. }

82:心

            
            
  1. package 算法;
  2. public class {
  3. public static void main(String[] args) {
  4. System.out.println(" *** ***");
  5. System.out.println(" ********** **********");
  6. System.out.println("************ ************");
  7. System.out.println("************* *************");
  8. System.out.println("************** **************");
  9. System.out.println("*************** ***************");
  10. System.out.println(" ******* *** *** *******");
  11. System.out.println(" ****** ***** ******");
  12. System.out.println(" ****** *** ******");
  13. System.out.println(" ****** * ******");
  14. System.out.println(" ****** * ******");
  15. System.out.println(" ***********");
  16. System.out.println(" *********");
  17. System.out.println(" *******");
  18. System.out.println(" *****");
  19. System.out.println(" *");
  20. }
  21. }

83:星系炸弹

            
            
  1. package 算法;
  2. /*
  3. *
  4. 在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。
  5. 每个炸弹都可以设定多少天之后爆炸。
  6. 比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。
  7. 有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。
  8. 请填写该日期,格式为 yyyy-mm-dd 即4位年份2位月份2位日期。比如:2015-02-19
  9. *
  10. */
  11. import java.util.Scanner;
  12. public class 星系炸弹 {
  13. public static void main(String[] args) {
  14. System.out.print("请输入炸弹放置的时间(yyyy-mm-dd):");
  15. // 创建字符输入对象
  16. Scanner sc = new Scanner(System.in);
  17. // 接收用户输入的字符串
  18. String time = sc.nextLine();
  19. System.out.print("请定时(天):");
  20. int num = Integer.parseInt(sc.nextLine());
  21. // 以-为分割线将日期截取
  22. String[] arr = time.split("-");
  23. // 分别提取出年月日并将其转换为int类型
  24. int year = Integer.parseInt(arr[0]);
  25. int month = Integer.parseInt(arr[1]);
  26. int day = Integer.parseInt(arr[2]);
  27. num = num + day;
  28. day = 0;
  29. while (true) {
  30. // 判断年份是闰年
  31. if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
  32. if (month == 2) {
  33. if (num > 28) {
  34. month++;
  35. num = num - 28;
  36. }
  37. else{
  38. day=num;
  39. break;
  40. }
  41. }
  42. }
  43. //判断年份是平年
  44. if (!(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) {
  45. if (month == 2) {
  46. if (num > 27) {
  47. month++;
  48. num = num - 27;
  49. }
  50. else{
  51. day=num;
  52. break;
  53. }
  54. }
  55. }
  56. // 对月份为31天的进行操作
  57. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
  58. if (num > 31) {
  59. month++;
  60. num = num - 31;
  61. if (month == 13) {
  62. year++;
  63. month = 1;
  64. }
  65. }
  66. else{
  67. day=num;
  68. break;
  69. }
  70. }
  71. // 对月份天数为30天的进行操作
  72. if (month == 4 || month == 6 || month == 9 || month == 11) {
  73. if (num > 30) {
  74. month++;
  75. num = num - 30;
  76. }
  77. else{
  78. day=num;
  79. break;
  80. }
  81. }
  82. }
  83. System.out.println("爆炸的时间为:"+year+"-"+month+"-"+day);
  84. }
  85. }

84:熊怪吃核桃

           
           
  1. package 算法;
  2. /*
  3. *
  4. 森林里有一只熊怪,很爱吃核桃。不过它有个习惯,每次都把找到的核
  5. 桃分成相等的两份,吃掉一份,留一份。如果不能等分,熊怪就会扔掉
  6. 一个核桃再分。第二天再继续这个过程,直到最后剩一个核桃了,直接
  7. 丢掉。*/
  8. public class 熊怪吃核桃 {
  9. public static void main(String[] args) {
  10. int a = 1543;
  11. int b = 0;
  12. while (a!=0) {
  13. if(a%2==1){
  14. a--;
  15. b++;
  16. }
  17. else{
  18. a=a/2;
  19. }
  20. }
  21. System.out.println(b);
  22. }
  23. }

85:有趣的跳跃

           
           
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. * 一个长度为n(n>0)的序列中存在“有趣的跳跃”当前仅当相邻元素的差的绝对值经过排序后正好是从1到(n-1)。
  5. * 例如,1 4 2 3存在“有趣的跳跃”,因为差的绝对值分别为3,2,1。当然,任何只包含单个元素的序列一定存在“有趣的跳跃”。
  6. * 你需要写一个程序判定给定序列是否存在“有趣的跳跃”。
  7. * 一行,第一个数是n(0 < n < 3000),为序列长度,接下来有n个整数,依次为序列中各元素,各元素的绝对值均不超过1,000,000,000。
  8. * 一行,若该序列存在“有趣的跳跃”,输出"Jolly",否则输出"Not jolly"
  9. * */
  10. public class 有趣的跳跃 {
  11. public static void main(String[] args) {
  12. Scanner sc = new Scanner(System.in);
  13. int x = sc.nextInt();
  14. int[] jump = new int[x];
  15. int[] z = new int[x - 1];
  16. int count = 0;
  17. for (int i = 0; i < jump.length; i++) {
  18. jump[i] = sc.nextInt();
  19. }
  20. if (x < 3) {
  21. System.out.println("Not jolly");
  22. } else {
  23. for (int i = 1; i < jump.length - 1; i++) {
  24. if (jump[i] == jump[i - 1] && jump[i] == jump[i + 1]) {
  25. System.out.println("Jolly");
  26. System.exit(0);
  27. }
  28. if (Math.abs(jump[i] - jump[i - 1]) - Math.abs(jump[i] - jump[i + 1]) == 1) {
  29. z[i - 1] = Math.abs(jump[i] - jump[i - 1]);
  30. if (i == jump.length - 2)
  31. z[i] = Math.abs(jump[i] - jump[i + 1]);
  32. }
  33. }
  34. for (int i = 0; i < z.length; i++) {
  35. if (z[i] == x - 1 - i) {
  36. count++;
  37. }
  38. }
  39. if (count == x - 1)
  40. System.out.println("Jolly");
  41. else
  42. System.out.println("Not jolly");
  43. }
  44. }
  45. }

86:与指定数字相同的数的个数

           
           
  1. package 算法;
  2. import java.util.Scanner;
  3. //输入包含2行:
  4. //第1行为N和m,表示整数序列的长度(N <= 100)和指定的数字, 中间用一个空格分开;
  5. //第2行为N个整数,整数之间以一个空格分开。
  6. public class 与指定数字相同的数的个数 {
  7. public static void main(String[] args) {
  8. Scanner sc=new Scanner(System.in);
  9. int a=sc.nextInt();
  10. int[] x=new int[a];
  11. int count=0;
  12. for(int i=0;i<a;i++){
  13. x[i]=sc.nextInt();
  14. }
  15. int b=sc.nextInt();
  16. for (int i = 0; i < x.length; i++) {
  17. if(x[i]==b)
  18. count++;
  19. }
  20. System.out.println(count);
  21. }
  22. }

87:约瑟夫问题

           
           
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *
  5. * 据说著名犹太历史学家 Josephus有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的
  6. * 朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1
  7. * 个人开始报数,每报数到第3人该人就必须自杀,然后再由下一个重新报数,直到所有人都自杀身亡为止。
  8. * 问题:编号为那俩个数字的人存活
  9. * */
  10. public class 约瑟夫问题 {
  11. public static void main(String[] args) {
  12. System.out.print("请输入参与游戏的猴数:");
  13. Scanner sc = new Scanner(System.in);
  14. int num = sc.nextInt();
  15. System.out.print("请输入报数的大小:");
  16. int y=sc.nextInt();
  17. int[] people = new int[num];
  18. int x = 0;
  19. for (int i = 0; i < people.length; i++) {
  20. people[i] = 1;
  21. }
  22. while (true) {
  23. for (int i = 0; i < people.length; i++) {
  24. if (people[i] == 1) {
  25. x++;
  26. if (x % y == 0) {
  27. people[i] = 0;
  28. num--;
  29. }
  30. }
  31. }
  32. if (num == 1) {
  33. break;
  34. }
  35. }
  36. for (int i = 0; i < people.length; i++) {
  37. if (people[i] == 1) {
  38. System.out.print(i + 1);
  39. }
  40. }
  41. }
  42. }

88:找第一个只出现一次的字符

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //给定一个只包含小写字母的字符串,请你找到第一个仅出现一次的字符。如果没有,输出no。
  4. public class 找第一个只出现一次的字符 {
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. String str=sc.nextLine();
  8. for (int i = 0; i < str.length(); i++) {
  9. int count=0;
  10. for (int j = 0; j < str.length(); j++) {
  11. if(str.charAt(i)==str.charAt(j)){
  12. count++;
  13. }
  14. }
  15. if(count==1){
  16. System.out.println(str.charAt(i));
  17. break;
  18. }
  19. if(i==str.length()-1){
  20. System.out.println("no");
  21. break;
  22. }
  23. }
  24. }
  25. }

89:整理药名

           
           
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *医生在书写药品名的时候经常不注意大小写,格式比较混乱。现要求你写一个程序将医生书写混乱的药品名整理成
  5. *统一规范的格式,即药品名的第一个字符如果是字母要大写,其他字母小写。如将ASPIRIN、aspirin整理成Aspirin。
  6. 输入
  7. 第一行一个数字n,表示有n个药品名要整理,n不超过100。
  8. 接下来n行,每行一个单词,长度不超过20,表示医生手书的药品名。药品名由字母、数字和-组成。
  9. *
  10. * */
  11. public class 整理药名 {
  12. public static void main(String[] args) {
  13. Scanner sc = new Scanner(System.in);
  14. int x = Integer.parseInt(sc.nextLine());
  15. String[] arr = new String[x];
  16. String[] newarr = new String[x];
  17. String y = "";
  18. for (int i = 0; i < x; i++) {
  19. arr[i] = sc.nextLine();
  20. }
  21. for (int i = 0; i < arr.length; i++) {
  22. y = arr[i].toLowerCase();
  23. if (y.charAt(0) >= 'a' && y.charAt(0) <= 'z')
  24. newarr[i] = (char) (y.charAt(0) - 32) + y.substring(1);
  25. else
  26. newarr[i] = y;
  27. }
  28. for (int i = 0; i < newarr.length; i++) {
  29. System.out.println(newarr[i]);
  30. }
  31. }
  32. }

90:整数的个数

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. //输入有两行:第一行包含一个正整数k,第二行包含k个正整数,每两个正整数用一个空格分开。
  4. //输出有三行,第一行为1出现的次数,,第二行为5出现的次数,第三行为10出现的次数
  5. public class 整数的个数 {
  6. public static void main(String[] args) {
  7. Scanner sc=new Scanner(System.in);
  8. int x=sc.nextInt();
  9. int a=0;
  10. int b=0;
  11. int c=0;
  12. for(int i=0;i<x;i++){
  13. int y=sc.nextInt();
  14. if(y==1)
  15. a++;
  16. if(y==5)
  17. b++;
  18. if(y==10)
  19. c++;
  20. }
  21. System.out.print(a+"\n"+b+"\n"+c);
  22. }
  23. }

91:整数去重

             
             
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *给定含有n个整数的序列,要求对这个序列进行去重操作。所谓去重,是指对这个序列中每个重复出现的数,只保留该数第一次出现的位置,删除其余位置。
  5. 输入
  6. 输入包含两行:
  7. 第一行包含一个正整数n(1 <= n <= 20000),表示第二行序列中数字的个数;
  8. 第二行包含n个整数,整数之间以一个空格分开。每个整数大于等于10、小于等于100。
  9. 输出
  10. 输出只有一行,按照输入的顺序输出其中不重复的数字,整数之间用一个空格分开。
  11. 样例输入
  12. 5
  13. 10 12 93 12 75
  14. 样例输出
  15. 10 12 93 75
  16. *
  17. * */
  18. public class 整数去重 {
  19. public static void main(String[] args) {
  20. Scanner sc = new Scanner(System.in);
  21. int num = sc.nextInt();
  22. //定义整型数组用于存放不相等的整数
  23. int[] nums = new int[num];
  24. //定义所用到的数组长度计数器
  25. int count = 1;
  26. //先x为数组中第一个元素的值
  27. int x = sc.nextInt();
  28. nums[0] = x;
  29. //如果整数的数量为1则直接输出x
  30. if (num == 1)
  31. System.out.println(x);
  32. else {
  33. //以数组的长度减1控制循环
  34. for (int i = 1; i < nums.length; i++) {
  35. //z代表不相等的数的个数,每循环一次值变为0
  36. int z=0;
  37. int y=sc.nextInt();
  38. //输入的y值一次和存入数组的每一个数值比较
  39. for (int j = 0; j < count; j++) {
  40. //若果不相等则计数器加1
  41. if(y!=nums[j]){
  42. z++;
  43. }
  44. }
  45. //如果计数器的值等于数组的长度,则代表都不相等
  46. if(z==count){
  47. //将y的值存入数组中
  48. nums[count]=y;
  49. //所用到的数组长度加1
  50. count++;
  51. }
  52. }
  53. //循环输出数组中的元素
  54. for (int i = 0; i < count; i++) {
  55. System.out.print(nums[i]+" ");
  56. }
  57. }
  58. }
  59. }

92:最长最短单词

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *输入1行句子(不多于200个单词,每个单词长度不超过100),只包含字母、空格和逗号。
  5. *单词由至少一个连续的字母构成,空格和逗号都是单词间的间隔。
  6. 试输出第1个最长的单词和第1个最短单词。
  7. 输入
  8. 一行句子。
  9. 输出
  10. 两行输出:
  11. 第1行,第一个最长的单词。
  12. 第2行,第一个最短的单词。
  13. 样例输入
  14. I am studying Programming language C in Peking University
  15. 样例输出
  16. Programming
  17. I
  18. *
  19. * */
  20. public class 最长最短单词 {
  21. public static void main(String[] args) {
  22. Scanner sc = new Scanner(System.in);
  23. // 输入一个字符串,将","全部转换为空格便于分割字符串
  24. String str1 = sc.nextLine().replace(',', ' ');
  25. //将字符串相连的多个空格转换为一个空格
  26. String str=str1.replaceAll(" +", " ");
  27. // 以空格将字符串分割出来
  28. String[] arr = str.split(" ");
  29. // 定义最长最短单词的初始长度,以及存储变量
  30. int max = arr[0].length();
  31. int min = arr[0].length();
  32. String maxStr = arr[0];
  33. String minStr = arr[0];
  34. // 找出长度最长的单词
  35. for (int i = 1; i < arr.length; i++) {
  36. if (max < arr[i].length()) {
  37. max = arr[i].length();
  38. maxStr = arr[i];
  39. }
  40. }
  41. // 找出长度最短的单词
  42. for (int i = 1; i < arr.length; i++) {
  43. if (min > arr[i].length()) {
  44. min = arr[i].length();
  45. minStr = arr[i];
  46. }
  47. }
  48. System.out.print(maxStr+"\r\n"+minStr);
  49. }
  50. }

93:字符串处理

            
            
  1. package 算法;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Set;
  6. /*1、(黑马点招面试题):有类似这样的字符串:"1.2,3.4,5.6,7.8,5.56,44.55"请按照要求,依次完成以下试题。
  7. (1)以逗号作为分隔符,把已知的字符串分成一个String类型的数组,数组中的每一个元素类似于"1.2","3.4"这样的字符串
  8. (2)把数组中的每一个元素以"."作为分隔符,把"."左边的元素作为key,右边的元素作为value,封装到Map中,
  9. Map中的key和value都是Object类型。
  10. (3)把map中的key封装的Set中,并把Set中的元素输出。
  11. (4)把map中的value封装到Collection中,把Collection中的元素输出。*/
  12. public class 字符串处理 {
  13. public static void main(String[] args) {
  14. String str="1.2,3.4,5.6,7.8,5.56,44.55";
  15. //将字符串以","切割
  16. String[] arr=str.split(",");
  17. //创建Map集合
  18. Map<Object, Object> m=new HashMap<Object, Object>();
  19. for (int i = 0; i < arr.length; i++) {
  20. String[] num=arr[i].split("\\.");
  21. m.put(num[0], num[1]);
  22. }
  23. Set<Object> key = m.keySet();
  24. for (Object o : key) {
  25. System.out.print(o+" ");
  26. }
  27. System.out.println();
  28. Collection<Object> values = m.values();
  29. for (Object o : values) {
  30. System.out.print(o+" ");
  31. }
  32. }
  33. }

94:字符串判等

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. *判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等。
  5. 输出
  6. 若两个字符串相等,输出YES,否则输出NO。
  7. *
  8. * */
  9. public class 字符串判等 {
  10. public static void main(String[] args) {
  11. Scanner sc=new Scanner(System.in);
  12. String str=sc.nextLine().replace(" ", "");
  13. String strr=sc.nextLine().replace(" ", "");
  14. if(str.equalsIgnoreCase(strr))
  15. System.out.println("YES");
  16. else
  17. System.out.println("NO");
  18. }
  19. }

95:字符替换

            
            
  1. package 算法;
  2. import java.util.Scanner;
  3. /*
  4. 只有一行,由一个字符串和两个字符组成,中间用单个空格隔开。字符串是待替换的字符串,字符串长度小于等于30个字符,且不含空格等空白符;
  5. 接下来一个字符为需要被替换的特定字符;
  6. 接下来一个字符为用于替换的给定字符。
  7. */
  8. public class 字符替换 {
  9. public static void main(String[] args) {
  10. Scanner sc=new Scanner(System.in);
  11. String str=sc.next();
  12. char a=sc.next().charAt(0);
  13. char b=sc.next().charAt(0);
  14. System.out.println(str.replace(a, b));
  15. }
  16. }

96:组合数

             
             
  1. package 算法;
  2. import java.util.Scanner;
  3. /**
  4. * 从4个人中选2个人参加活动,一共有6种选法。
  5. * 从n个人中选m个人参加活动,一共有多少种选法?
  6. */
  7. public class 组合数 {
  8. public static void main(String[] args) {
  9. System.out.println("请输入两个整数,且前一个大于后一个!");
  10. Scanner sc=new Scanner(System.in);
  11. System.out.println(f(sc.nextInt(),sc.nextInt()));
  12. }
  13. public static int f(int m,int n){
  14. if(n>m) return 0;
  15. if(n==0) return 1;
  16. return f(m-1,n-1)+f(m-1,n);
  17. }
  18. }

97:最大值和最小值的差

            
            
  1. package 算法;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. /*
  5. *输出一个整数序列中最大的数和最小的数的差。
  6. 输入
  7. 第一行为M,表示整数个数,整数个数不会大于10000;
  8. 第二行为M个整数,以空格隔开,每个整数的绝对值不会大于10000。
  9. 输出
  10. 输出M个数中最大值和最小值的差。
  11. 样例输入
  12. 5
  13. 2 5 7 4 2
  14. 样例输出
  15. 5
  16. *
  17. * */
  18. public class 最大值和最小值的差 {
  19. public static void main(String[] args) {
  20. Scanner sc = new Scanner(System.in);
  21. int num = sc.nextInt();
  22. // 定义数组用于存放输入的整数
  23. int[] arr = new int[num];
  24. for (int i = 0; i < arr.length; i++) {
  25. arr[i] = sc.nextInt();
  26. }
  27. if (num == 1)
  28. System.out.println(0);
  29. else {
  30. Arrays.sort(arr);
  31. System.out.println(arr[arr.length - 1] - arr[0]);
  32. }
  33. }
  34. }

98:最高的分数

            
            
  1. package 算法;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. //输入两行,第一行为整数n(1 <= n < 100),表示参加这次考试的人数.第二行是这n个学生的成绩,
  5. //相邻两个数之间用单个空格隔开。所有成绩均为0到100之间的整数。
  6. public class 最高的分数 {
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9. int num = sc.nextInt();
  10. int[] great = new int[num];
  11. for (int i = 0; i < num; i++) {
  12. great[i] = sc.nextInt();
  13. }
  14. Arrays.sort(great);
  15. System.out.println(great[great.length - 1]);
  16. }
  17. }

99:用户提醒

             
             
  1. package 算法;
  2. import java.util.Scanner;
  3. /*给你写个需求:键盘录入一个数,判断各种可能性,是字符串提示用户输入的是字符串,
  4. 是负数提示用户输入的是负数,是小数提示用户为小数,最终把整数打印在控制台上
  5. 判断是字符串或者负数小数时,不仅提示用户信息,还要继续录入整数,知道录入整数为止*/
  6. public class String_demo {
  7. public static void main(String[] args) {
  8. // 创建键盘录入对象
  9. Scanner sc = new Scanner(System.in);
  10. System.out.println("请输入一个数:");
  11. while (true) {
  12. String str = sc.nextLine();
  13. // 字符串正则验证式
  14. String regex = "[a-zA-Z]+";
  15. // 整数正则验证式
  16. String regex1 = "[1-9]+[0-9]+";
  17. if (str.length() > 1) {
  18. if (!str.equals(str.replaceAll(regex, ""))) {
  19. if (!(str.charAt(0) > '1' && str.charAt(0) < '9')) {
  20. System.out.println("您录入的是字符串,请再次输入:");
  21. continue;
  22. }else {
  23. System.out.println("输入错误");
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }

100:忘记题目

              
              
  1. package 算法;
  2. import java.util.Scanner;
  3. public class ad {
  4. public static void main(String[] args) {
  5. //创建键盘输入对象
  6. Scanner sc = new Scanner(System.in);
  7. //用户提示
  8. System.out.println("请输入字符串:");
  9. //接收用户输入的字符串
  10. String line = sc.nextLine();
  11. //调用字符串格式化方法
  12. format(line);
  13. }
  14. public static void format(String line) {
  15. //先干掉空格,只留一个空格
  16. String regex = "\\s+"; //匹配空格正则
  17. line = line.replaceAll(regex, " ");
  18. line = line.replaceAll("(\\d+)", "_$1_"); //匹配数字,并进行替换
  19. String[] split = line.split(" "); //分割成字符串数组
  20. String s = ""; //定义一个空串
  21. for (int i = 0; i < split.length; i++) { //遍历字符串数组
  22. //字符串数组中的每一个元素首字母大写,其他小写,链式编程
  23. String s1 = split[i].substring(0,1).toUpperCase().concat(split[i].substring(1).toLowerCase());
  24. s += s1 + " "; //将转换后的字符串追加到s中,并加" "
  25. }
  26. System.out.println(s); //打印结果
  27. }
  28. }

101:过河问题

              
              
  1. package 算法;
  2. import java.util.Arrays;
  3. /*
  4. *在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边。如果不借助手电筒的话,大家是无论如何也不敢过桥去的。
  5. *不幸的是,N个人一共只带了一只手电筒,而桥窄得只够让两个人同时过。如果各自单独过桥的话,N人所需要的时间已知;
  6. *而如果两人同时过桥,所需要的时间就是走得比较慢的那个人单独行动时所需的时间。问题是,如何设计一个方案,让这N人尽快过桥。
  7. 输入
  8. 第一行是一个整数T(1<=T<=20)表示测试数据的组数
  9. 每组测试数据的第一行是一个整数N(1<=N<=1000)表示共有N个人要过河
  10. 每组测试数据的第二行是N个整数Si,表示此人过河所需要花时间。(0 输出
  11. 输出所有人都过河需要用的最少时间
  12. 样例输入
  13. 1
  14. 4
  15. 1 2 5 10
  16. 样例输出
  17. 17
  18. *
  19. * */
  20. import java.util.Scanner;
  21. public class Crossing_River {
  22. public static void main(String[] args) {
  23. Scanner sc = new Scanner(System.in);
  24. // num代表测试次数
  25. int num = sc.nextInt();
  26. //定义数组,用于存放每次测试的时间
  27. int[] sum=new int[num];
  28. for (int j = 0; j < num; j++) {
  29. // x代表人数
  30. int x = sc.nextInt();
  31. // 定义数组用于存放每个人过河所用的时间
  32. int[] time = new int[x];
  33. // 定义过河总时间
  34. int times = 0;
  35. for (int i = 0; i < x; i++) {
  36. time[i] = sc.nextInt();
  37. }
  38. // 利用数组工具类对数组进行升序排序
  39. Arrays.sort(time);
  40. // 判断人数,如果小于等于3则字节算出过河时间
  41. if (x == 1)
  42. times = time[0];
  43. else if (x == 2)
  44. times = time[1];
  45. else if (x == 3)
  46. times = time[0] + time[1] + time[2];
  47. else {
  48. // 判断人数是奇数还是偶数,偶数的话最后加1个时间;奇数的话最后加3个时间
  49. times = 0;
  50. int y = 0;
  51. // 用人数除以2减1控制循环次数
  52. for (int i = 0; i < x / 2 - 1; i++) {
  53. times += 2 * time[1] + time[0] + time[time.length - 1 - y];
  54. y += 2;
  55. }
  56. if (x % 2 == 0)
  57. times += time[1];
  58. else
  59. times += time[1] + time[0] + time[time.length - 1 - y];
  60. }
  61. sum[j]=times;
  62. }
  63. //输出时间
  64. for (int i = 0; i < num; i++) {
  65. System.out.println(sum[i]);
  66. }
  67. }
  68. }

102:第n个数能否被3整除

              
              
  1. package 算法;
  2. import java.util.Scanner;
  3. // F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
  4. //求第n个数能否被3整除,能的话输出yes;不能的话输出no
  5. public class Fibonacci {
  6. public static void main(String[] args) {
  7. Scanner sc=new Scanner(System.in);
  8. int x=sc.nextInt();
  9. if((x-2)%4==0)
  10. System.out.println("yes");
  11. else
  12. System.out.println("no");
  13. }
  14. }
















  • 11
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CSDN Java算法是指CSDN网站上针对Java编程语言的算法资源和教学内容。Java是一种广泛使用的编程语言,用于开发各种应用程序。在Java编程中,算法是非常重要的部分,因为它涉及到了如何解决问和优化代码性能。CSDN Java算法提供了一系列Java算法实现的教学、代码分享和讨论,可以帮助Java开发者提高算法能力和编程技能。 CSDN Java算法中包含了很多经典算法,比如排序算法、查找算法、图论算法、动态规划、贪心算法等。在这些算法的教学中,会详细介绍算法的思路、实现方式和相关应用场景,让读者能够深入理解算法的原理和基本思想。 在Java编程中,使用正确的算法可以大大提升代码性能和效率。CSDN Java算法不仅提供了算法的教学,还有一些实际应用程序的代码分享和讨论,可以帮助Java开发者更好地掌握算法的实践技巧,从而创造出高效的代码。 总的来说,CSDN Java算法提供了非常丰富和实用的Java算法学习资源,可以帮助Java开发者提高算法水平、提升代码质量和效率,是Java开发者不可错过的学习和分享平台。 ### 回答2: CSDN是一个集IT技术、软件学习和职业发展于一体的专业IT社区,其中开发技术板块中Java算法是非常核心的领域。Java算法在编写更高效、更稳定的代码时起着至关重要的作用。Java算法的使用可以使程序运行速度更快、节省内存空间、提高代码可读性和可维护性。CSDN Java算法板块收录了大量Java算法的相关文章和分享,其中包括常见的排序算法、查找算法、动态规划算法等等。在这里,开发者可以学习到Java算法的实现方法、应用场景和注意事项等知识。此外,还有一些经验丰富的Java开发者分享了自己在实际项目中应用Java算法的令人惊叹的案例和技巧。所有这些CSDN Java算法板块的文章和分享可以帮助每个Java开发者更好地理解和掌握Java算法。无论是初学者还是有经验的Java开发者,都可以在CSDN Java算法板块中找到适合自己的Java算法文章。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值