• 每天10道编程题-第四天


    【程序31】   题目:将一个数组逆序输出。

     1 package com.daliu.suanfa4;
     2 
     3 public class Exp31 {
     4 
     5     /**
     6      * 【程序31】   题目:将一个数组逆序输出。 
     7      */
     8     public static void main(String[] args) {
     9         int myarr[]={1,6,9,8,5,3,7,6};
    10          for(int k=myarr.length-1;k>=0;k--)
    11               System.out.print(myarr[k]+",");
    12     }
    13 
    14 }

    【程序32】   题目:取一个整数a从右端开始的4~7位。  

     1 package com.daliu.suanfa4;
     2 
     3 public class Exp32 {
     4     public static void main(String[] args) {
     5         int a = 0;//用于存放得到的数
     6         long b = 18745678;//测试数据
     7         a = (int) Math.floor(b % Math.pow(10, 7) / Math.pow(10, 3));
     8         System.out.println(a);
     9     }
    10 }

    【程序33】  
    题目:打印出杨辉三角形(要求打印出10行如下图)  
     
    1  
    1   1  
    1   2   1  
    1   3   3   1  
    1   4   6   4   1  
    1   5   10   10   5   1  

     1 package com.daliu.suanfa4;
     2 public class Exp33 {
     3     public static void main(String args[]){
     4            int i,j;
     5            int a[][];
     6            a=new int[8][8];
     7           for(i=0;i<8;i++){
     8              a[i][i]=1;
     9              a[i][0]=1; 
    10             }
    11           for(i=2;i<8;i++){
    12            for(j=1;j<=i-1;j++){
    13           a[i][j]=a[i-1][j-1]+a[i-1][j]; 
    14            }
    15           }  
    16           for(i=0;i<8;i++){
    17           for(j=0;j<i;j++){  
    18            System.out.printf("  "+a[i][j]);
    19            }
    20           System.out.println();
    21           }
    22          }
    23 }


    【程序34】   题目:输入3个数a,b,c,按大小顺序输出。  

     1 package com.daliu.suanfa4;
     2 public class Exp34 {
     3     public static void main(String[] args) 
     4     { 
     5     int []arrays = {800,56,500}; 
     6     for(int i=arrays.length;--i>=0;) 
     7     { 
     8     for(int j=0;j<i;j++) 
     9     { 
    10     if(arrays[j]>arrays[j+1]) 
    11     { 
    12     int temp=arrays[j]; 
    13     arrays[j]=arrays[j+1]; 
    14     arrays[j+1]=temp; 
    15     } 
    16     } 
    17     } 
    18     for(int n=0;n<arrays.length;n++) 
    19     System.out.print(arrays[n]+" "); 
    20     } 
    21 
    22 }


    【程序35】   题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。  

     1 package com.daliu.suanfa4;
     2 
     3 import java.util.*;
     4 
     5 public class Exp35 {
     6     public static void main(String[] args) {
     7         int i, min, max, n, temp1, temp2;
     8         int a[];
     9         System.out.println("输入数组的长度:");
    10         Scanner keyboard = new Scanner(System.in);
    11         n = keyboard.nextInt();
    12         a = new int[n];
    13         for (i = 0; i < n; i++) {
    14             System.out.print("输入第" + (i + 1) + "个数据");
    15             a[i] = keyboard.nextInt();
    16         }
    17         // 以上是输入整个数组
    18         max = 0;
    19         min = 0;
    20         // 设置两个标志,开始都指向第一个数
    21         for (i = 1; i < n; i++) {
    22             if (a[i] > a[max])
    23                 max = i; // 遍历数组,如果大于a[max],就把他的数组下标赋给max
    24             if (a[i] < a[min])
    25                 min = i; // 同上,如果小于a[min],就把他的数组下标赋给min
    26         }
    27         // 以上for循环找到最大值和最小值,max是最大值的下标,min是最小值的下标
    28         temp1 = a[0];
    29         temp2 = a[min]; // 这两个temp只是为了在交换时使用
    30 
    31         a[0] = a[max];
    32         a[max] = temp1; // 首先交换a[0]和最大值a[max]
    33 
    34         if (min != 0) { // 如果最小值不是a[0],执行下面
    35             a[min] = a[n - 1];
    36             a[n - 1] = temp2; // 交换a[min]和a[n-1]
    37         } else { // 如果最小值是a[0],执行下面
    38             a[max] = a[n - 1];
    39             a[n - 1] = temp1;
    40         }
    41 
    42         for (i = 0; i < n; i++) { // 输出数组
    43             System.out.print(a[i] + " ");
    44         }
    45     }
    46 }

    【程序36】   题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数  


    【程序37】   题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。 

     1 package com.daliu.suanfa4;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Exp37 {
     6     public static void main(String[] args) {
     7         Scanner s = new Scanner(System.in);
     8         int n = s.nextInt();
     9         boolean[] arr = new boolean[n];
    10         for (int i = 0; i < arr.length; i++) {
    11             arr[i] = true;// 下标为TRUE时说明还在圈里
    12         }
    13         int leftCount = n;
    14         int countNum = 0;
    15         int index = 0;
    16         while (leftCount > 1) {
    17             if (arr[index] == true) {// 当在圈里时
    18                 countNum++; // 报数递加
    19                 if (countNum == 3) {// 报道3时
    20                     countNum = 0;// 从零开始继续报数
    21                     arr[index] = false;// 此人退出圈子
    22                     leftCount--;// 剩余人数减一
    23                 }
    24             }
    25             index++;// 每报一次数,下标加一
    26             if (index == n) {// 是循环数数,当下标大于n时,说明已经数了一圈,
    27                 index = 0;// 将下标设为零重新开始。
    28             }
    29         }
    30         for (int i = 0; i < n; i++) {
    31             if (arr[i] == true) {
    32                 System.out.println(i);
    33             }
    34         }
    35     }
    36 }

     

    【程序38】   题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

     1 package com.daliu.suanfa4;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Exp38 {
     6     public static void main(String[] args) {
     7         Scanner s = new Scanner(System.in);
     8         System.out.println("请输入一个字符串");
     9         String mys = s.next();
    10         System.out.println(str_len(mys));
    11     }
    12 
    13     public static int str_len(String x) {
    14         return x.length();
    15     }
    16 }

    【程序39】  题目:字符串排序。  

     1 package com.daliu.suanfa4;
     2 
     3 import java.util.*;
     4 
     5 public class Exp39 {
     6     public static void main(String[] args) {
     7         ArrayList<String> list = new ArrayList<String>();
     8         list.add("010101");
     9         list.add("010003");
    10         list.add("010201");
    11         Collections.sort(list);
    12         for (int i = 0; i < list.size(); i++) {
    13             System.out.println(list.get(i));
    14         }
    15     }
    16 }


    【程序40】  
    题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?  

     1 package com.daliu.suanfa4;
     2 
     3 public class Exp40 {
     4     static int ts = 0;// 桃子总数
     5     int fs = 1;// 记录分的次数
     6     static int hs = 5;// 猴子数...
     7     int tsscope = 5000;// 桃子数的取值范围.太大容易溢出.
     8 
     9     public int fT(int t) {
    10         if (t == tsscope) {
    11             // 当桃子数到了最大的取值范围时取消递归
    12             System.out.println("结束");
    13             return 0;
    14         } else {
    15             if ((t - 1) % hs == 0 && fs <= hs) {
    16                 if (fs == hs) {
    17                     System.out.println("桃子数 = " + ts + " 时满足分桃条件");
    18                 }
    19                 fs += 1;
    20                 return fT((t - 1) / 5 * 4);// 返回猴子拿走一份后的剩下的总数
    21             } else {
    22                 // 没满足条件
    23                 fs = 1;// 分的次数重置为1
    24                 return fT(ts += 1);// 桃子数加+1
    25             }
    26         }
    27     }
    28 
    29     public static void main(String[] args) {
    30         new Exp40().fT(0);
    31     }
    32 
    33 }
  • 相关阅读:
    Microsoft Azure 微软云平台系列新品发布
    python学习笔记(三)
    python学习笔记(一)
    python GUI编程(tkinter)
    python Tkinter之Button
    tkinter比较常用的组件
    tkinter窗口系列之一——列表框
    tkinter模块中常用的参数
    python的字符串操作函数之一览
    python之闭包
  • 原文地址:https://www.cnblogs.com/liuhongfeng/p/4143947.html
Copyright © 2020-2023  润新知