zoukankan      html  css  js  c++  java
  • Java算法求最大最小值,倒序,冒泡排序,斐波纳契数列,日历一些经典算法

    一,求最大,最小值

    int[] a={21,31,4,2,766,345,2,34};  
    //这里防止数组中有负数,所以初始化的时候给的数组中的第一个数。  
    int max=a[0];  
    int min=a[0];  
    for (int i = 0; i < a.length; i++) {  
         if(a[i]>max)  
                 max=a[i];  
         if(a[i]<min)  
                 min=a[i];  
    }  
    System.out.println("最大的数是:"+max);  
    System.out.println("最小的值是:"+min);

    二,1--100之间的质数(什么叫质数,就是只能被1和本身能除尽的数) 

    for (int i = 2; i <= 100; i++) {
        boolean flag = true;
        for (int j = 2; j < i/2+1; j++) {
             if(i%j==0){
                  flag = false;
                  break;  
             }
         }
         if(flag){
             System.out.print(i+" ");  
         }
    }

    三,输入一个字符串,倒着打印出来(如:abcdefg1256,打印出来是:6521gfedcba) 

    Scanner scanner = new Scanner(System.in);
    String ss = scanner.next();
    char[] a = ss.toCharArray();
    char tmp = 0;
    for(int i = 0; i<a.length/2;i++){
        tmp = a[i];
        a[i]=a[a.length-i-1];
        a[a.length-i-1]=tmp;
    }
    String s=new String(a); 
    System.out.println(s);

    四,斐波纳契数列(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181)

    int a=0;  
    int b=1;  
    for (int i = 1; i <= 10; i++) {  
          System.out.print(a+" ");  
          a=a+b;  
          System.out.print(b+" ");  
          b=a+b;  
    }

    五,冒泡排序

    int score[] = {167,69,75,87,89,90,99,100,54,62,87,201,89,14,25,57,53,19 };
    for (int i = 0; i < score.length; i++) {
         for (int j = i; j < score.length; j++) {
              if (score[i] > score[j]) {
                   int temp = score[i];
                   score[i] = score[j];
                   score[j] = temp;
              }
         }
    }
    System.out.print("最终排序结果:");
    for (int a = 0; a < score.length; a++) {
        System.out.print(score[a] + " ");
    }

    六,在控制台打印日历,平且标出今天

    package test;  
    import java.text.DateFormatSymbols;  
    import java.util.Calendar;  
    import java.util.GregorianCalendar;  
          
    public class HelloString {  
            public static void main(String[] args) {  
                // 构建D为当前日期  
                GregorianCalendar d = new GregorianCalendar();  
          
                int today = d.get(Calendar.DAY_OF_MONTH);  
                int month = d.get(Calendar.MONTH);  
          
                // D组为本月的开始日期  
                d.set(Calendar.DAY_OF_MONTH, 1);  
          
                int weekday = d.get(Calendar.DAY_OF_WEEK);  
          
                // 一周的第一天是星期日  
                int firstDayOfWeek = d.getFirstDayOfWeek();  
          
                int indent = 0;  
                while (weekday != firstDayOfWeek) {  
                    indent++;  
                    d.add(Calendar.DAY_OF_MONTH, -1);  
                    weekday = d.get(Calendar.DAY_OF_WEEK);  
                }  
          
                // 打印工作日的名称  
                String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();  
                do {  
                    System.out.printf("%4s", weekdayNames[weekday]);  
                    d.add(Calendar.DAY_OF_MONTH, 1);  
                    weekday = d.get(Calendar.DAY_OF_WEEK);  
                } while (weekday != firstDayOfWeek);  
                System.out.println();  
          
                for (int i = 1; i <= indent; i++)  
                    System.out.print("    ");  
          
                d.set(Calendar.DAY_OF_MONTH, 1);  
                do {  
                    // 打印的一天  
                    int day = d.get(Calendar.DAY_OF_MONTH);  
                    System.out.printf("%3d", day);  
          
                    // 带*标记当前日期  
                    if (day == today)  
                        System.out.print("*");  
                    else  
                        System.out.print(" ");  
          
                    // 先进的第二天  
                    d.add(Calendar.DAY_OF_MONTH, 1);  
                    weekday = d.get(Calendar.DAY_OF_WEEK);  
          
                    // 从本周开始新的一行  
                    if (weekday == firstDayOfWeek)  
                        System.out.println();  
                } while (d.get(Calendar.MONTH) == month);  
                // 循环退出时,D是下个月的1天  
          
                // 如果需要打印的最终行  
                if (weekday != firstDayOfWeek)  
                    System.out.println();  
            }  
    }  
  • 相关阅读:
    基于KNN的newsgroup 18828文本分类器的Python实现
    基于Bayes和KNN的newsgroup 18828文本分类器的Python实现
    C++笔试题
    一号店笔试题
    最长递增子序列
    雅虎2015校招--研究工程师
    百度2013校园招聘笔试题(答案整理) – 机器学习/数据挖掘工程师
    windows下Python shell代码自动补全
    windows下scrapy安装
    实验四
  • 原文地址:https://www.cnblogs.com/aqsunkai/p/6690624.html
Copyright © 2011-2022 走看看