JAVA编程小题

a1

// 连成环 数m退一
public class Test_a1 {
 public static void main(String args[]) {
  MyLink ml = new MyLink(3);
  ml.remove(3);
  
  for(Node temp: ml.myNode)
   System.out.print(temp.data + " ");
 }
}

class Node {
 int data;
 Node next;
 
 Node() {
  data = -1;
  next = null;
 }
 Node(int i) {
  data = i;
  next = null;
 }
}

class MyLink {
 Node head,current,flag;
 int size;
 Node[] myNode;
 
 void add(int a) {
  current.next = new Node(a);
  current = current.next;
 }
 
 MyLink(int k) {
  size = k;
  head = new Node();
  current = head;
  flag = head;
  
  for(int i=1; i<=k; i++)
   add(i);
  
  current.next = head.next;
  myNode = new Node[k];
 
 }
 
 void remove(int m) {
  int n = 0;
  while(size != 0) {
   for(int i=1; i<m; i++) {
    flag = flag.next;
   }
   myNode[n++] = flag.next;
   flag.next = flag.next.next;
   size --;
  }
 }
}

a2

import java.util.*;
//题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5.
//程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
//(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。 ?
//(2)如果n <> k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你,重复执行第一步.
//(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
public class Test_a2 {
 public static void main(String args[]) {
  Vector<Integer> v = new Vector<Integer>();
  Scanner sc = new Scanner(System.in);
  int n = sc.nextInt();
  int k = 2;
  int i = n;
  
  if (n <= 1) {System.out.println("参数错误"); System.exit(0);}
  while(k < i) {
   if(i % k == 0) {
    v.addElement(new Integer(k));
    i = i / k;
   } else {
    k = k + 1;
   }
  }
  v.addElement(new Integer(k));
  
  System.out.format("%d=", n);
  for(int j = 0; j<v.size()-1; j++)
   System.out.format("%d*", v.get(j));
  System.out.println(v.get(v.size()-1));
  
 }
}

a3

// 判断素数
public class Test_a3 {
 public static void main(String args[]) {
  
  for(int i = 101; i <= 200; i++) { 
   boolean b = true;
   for(int j = 2; j <= Math.sqrt(i); j++) {
    if (i % j == 0) {
     b = false;
    }
   }
   if(b == true)
    System.out.print(i + " ");
  }
  System.out.println();
  
  for(int j = 101; j<=200; j++)
   if(isPrimeNumber(j) == true)
    System.out.print(j+ " ");
  
 }

 
 static boolean isPrimeNumber(int k) {
  for (int i = 2; i<=Math.sqrt(k);i++)
   if(k % i == 0)
    return false;
  return true;
    
 }
}

a4

//"斐波那契数列(Fibonacci)"
//1、1、2、3、5、8、13、21、……
public class Test_a4 {
 public static void main(String args[]) {
  FibonacciPrint(10);
 }
 
 static int FibonacciSum(int n) {
  if (n == 1 || n == 2)
   return 1;
  return FibonacciSum(n-1) + FibonacciSum(n-2);
 }
 
 static void FibonacciPrint(int n) {
  for(int i = 1; i <= n; i++) {
   System.out.print(FibonacciSum(i)+ " ");
  }
  
 }
}
 

a5

//设两数为a、b(b<a),求它们最大公约数(a、b)的步骤如下:
//用b除a,得a=bq......r1(0≤r)。若r1=0,则(a,b)=b;
//若r1≠0,则再用r1除b,得b=r1q......r2 (0≤r2).
//若r2=0,则(a,b)=r1,若r2≠0,则继续用r2除r1,……
//如此下去,直到能整除为止。其最后一个非零余数即为(a,b)。

//最小公倍数等于两数之积除以最大公约数
public class Test_a5 {

 
 public static void main(String[] args) {
  System.out.println(greatestCommonDivisor(30,20));
  System.out.println(greatestCommonDivisor2(30,20));
  System.out.println(leastCommonMultiple(30,20));
  System.out.println(leastCommonMultiple2(30,20));
 }
 
 static int greatestCommonDivisor(int a,int b ) {  // 最大公约数
  int m,n;
  if(a >= b) {
   m = a; n = b;
  } else {
   m = b; n = a;
  }
  int k = m % n;
  if (k == 0)
   return n;
  return greatestCommonDivisor(n,k);
 }
 
 static int greatestCommonDivisor2(int a,int b ) {
  int m,n,r;
  if(a >= b) {
   m = a; n = b;
  } else {
   m = b; n = a;
  }
  
  while(n != 0) {
   r = m % n;
   m = n;
   n = r;
  }
  return m;
 }
 
 static int leastCommonMultiple(int m,int n) {   //最小公倍数
  return ((m * n)/greatestCommonDivisor(m,n));
 }
 
 static int leastCommonMultiple2(int m,int n) {
  return (m * n)/greatestCommonDivisor2(m,n);
 }

}
 

a6

import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;

//90/15=6.将6分解可为1,6.或2,3.
//两组,2*15=30,3*15=45,成立。
//其他给出最大公约数,最小公倍数,两数相除所得数,将其分解分别乘以最小公倍数即得答案(可能得到多组,一一尝试)。

public class Test_a6 {
 static List<Integer> l1 = new ArrayList<Integer>();
 static List<Integer> l2 = new ArrayList<Integer>();

 public static void main(String args[]) {
  Scanner sc1 = new Scanner(System.in);
  Scanner sc2 = new Scanner(System.in);
  int m = sc1.nextInt();
  int n = sc2.nextInt();
  resolve(m / n);
  System.out.println();
  for (int i = 0; i < l1.size(); i++) {
   System.out.println(l1.get(i) * n + " " + l2.get(i) * n);
  }
 }

 static void resolve(int n) {
  int k = (int) Math.sqrt(n);
  for (int i = 1; i <= k; i++) {
   if (n % i == 0) {
    l1.add(i);
    l2.add(n / i);
   }
  }
 }
}
 


a7

import java.util.Scanner;

//输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。  
//程序分析:利用while语句,条件为输入的字符不为 '/n '.  
public class Test_a7 {
 public static void main(String args[]) {
  System.out.println("请输入字符串:");
  Scanner scan = new Scanner(System.in);
  String str = scan.next();
  String E1 = "[/u4e00-/u9fa5]";
  String E2 = "[a-zA-Z]";
  int countH = 0;
  int countE = 0;
  char[] arrChar = str.toCharArray();
  String[] arrStr = new String[arrChar.length];
  for (int i = 0; i < arrChar.length; i++) {
   arrStr[i] = String.valueOf(arrChar[i]);
  }
  for (String i : arrStr) {
   if (i.matches(E1)) {
    countH++;
   }
   if (i.matches(E2)) {
    countE++;
   }
  }
  System.out.println("汉字的个数" + countH);
  System.out.println("字母的个数" + countE);
 }
}

a8

import java.io.*;

//求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字
public class Test_a8 {
  public static void main(String[] args) throws IOException
  {
   int s=0;
   int n;
   int t=0;
   BufferedReader stadin = new BufferedReader(new InputStreamReader(System.in));
   String input = stadin.readLine();
   n=Integer.parseInt(input);
  
   for(int i=1;i<n;i++){
    t=t*10+n;
    s=s+t;
    System.out.print(t +"+");
   }
   t=t*10+n;
   s=s+t;
   System.out.print(t);
   System.out.println("=" + s);
  }
}

a9

//完数,即完美数,一个数如果恰好等于除它本身外的因子之和,这个数就称为完数。
//例如6=1+2+3.(6的因子是1,2,3)

public class Test_a9 {

 public static void main(String[] args) {
  int sum = 0;
  
  for (int i = 2; i <= 10000; i++) {
   for(int j = 1; j < i; j++) {
    if(i % j == 0) {
     sum = sum + j;
    }
   }
   if (sum == i)
    System.out.print(i+ " ");
   sum = 0;
  }

 }

}
 

b1

//一个整数,它加上100后是一个完全平方数,加上168又是一个完全平方数,请问该数是多少?  
//1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,
//如果开方后的结果满足如下条件,即是结果。请看具体分析:  

public class Test_b1 {
 public static void main(String[] args) {
  long k = 0;
  for (k = 1; k <= 100000l; k++)
   if (Math.floor(Math.sqrt(k + 100)) == Math.sqrt(k + 100)
     && Math.floor(Math.sqrt(k + 168)) == Math.sqrt(k + 168))
    System.out.println(k);
 }
}

b2

import java.util.*;
public class Test_b2 {
 public static void main (String[]args){
  int day=0;
  int month=0;
  int year=0;
  int sum=0;
  int leap;  
  System.out.print("请输入年,月,日/n");  
  Scanner input = new Scanner(System.in);
  year=input.nextInt();
  month=input.nextInt();
  day=input.nextInt();
  switch(month) /*先计算某月以前月份的总天数*/ 
  {  
  case 1:
   sum=0;break;  
  case 2:
   sum=31;break;  
  case 3:
   sum=59;break;  
  case 4:
   sum=90;break;  
  case 5:
   sum=120;break;  
  case 6:
   sum=151;break;  
  case 7:
   sum=181;break;  
  case 8:
   sum=212;break;  
  case 9:
   sum=243;break;  
  case 10:
   sum=273;break;  
  case 11:
   sum=304;break;  
  case 12:
   sum=334;break;  
  default:
   System.out.println("data error");break;
  }  
  sum=sum+day; /*再加上某天的天数*/ 
  if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/ 
   leap=1;  
  else 
   leap=0;  
  if(leap==1 && month>2)/*如果是闰年且月份大于2,总天数应该加一天*/ 
   sum++;  
  System.out.println("It is the the day:"+sum);
  }
}

b3

//题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
//  第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
//以后每天早上都吃了前一天剩下   的一半零一个。
//到第10天早上想再吃时,见只剩下一个桃子了。
//求第一天共摘了多少。

public class Test_b3 {
 static int total(int day){
   if(day == 10) {
    return 1;
   } else {
    return (total(day+1)+1)*2;
   }
 }
 public static void main(String[] args) {
  System.out.println(total(1));
 }
}

b4

//
//题目:两个乒乓球队进行比赛,各出三人。
//甲队为a,b,c三人,乙队为x,y,z三人。
//已抽签决定比赛名单。
//有人向队员打听比赛的名单。
//a说他不和x比,c说他不和x,z比,
//请编程序找出三队赛手的名单。  


import java.util.ArrayList;

public class Test_b4 {
  String a,b,c;
  public static void main(String[] args) {
   String[] op = { "x", "y", "z" };
   ArrayList<Test_b4> arrayList=new ArrayList<Test_b4>();
   for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
     for (int k = 0; k < 3; k++) {
      Test_b4 a=new Test_b4(op[i],op[j],op[k]);
      if(!a.a.equals(a.b)&&!a.b.equals(a.c)&&!a.a.equals("x")
        &&!a.c.equals("x")&&!a.c.equals("z")){
       arrayList.add(a);
      }
     }
   for(Object a:arrayList){
   System.out.println(a);
   }
  }
  public Test_b4(String a, String b, String c) {
   super();
   this.a = a;
   this.b = b;
   this.c = c;
  }
  @Override
  public String toString() {
   return "a的对手是"+a+","+"b的对手是"+b+","+"c的对手是"+c+"/n";
  }
}

b5

//题目:有n个人围成一圈,顺序排号。
//从第一个人开始报数(从1到3报数),
//凡报到3的人退出圈子,
//问最后留下的是原来第几号的那位。  
import java.util.Scanner;
public class Test_b5 {
 public static void main(String[] args) {
     Scanner s = new Scanner(System.in);
     int n = s.nextInt();
     boolean[] arr = new boolean[n];
     for(int i=0; i<arr.length; i++) {
      arr[i] = true;//下标为TRUE时说明还在圈里
     }
     int leftCount = n;
     int countNum = 0;
     int index = 0;
     while(leftCount > 1) {
      if(arr[index] == true) {//当在圈里时
       countNum ++; //报数递加
       if(countNum == 3) {//报道3时
        countNum =0;//从零开始继续报数
        arr[index] = false;//此人退出圈子
        leftCount --;//剩余人数减一
       }
      }
      index ++;//每报一次数,下标加一
      if(index == n) {//是循环数数,当下标大于n时,说明已经数了一圈,
       index = 0;//将下标设为零重新开始。
      }
     }
     for(int i=0; i<n; i++) {
      if(arr[i] == true) {
       System.out.println(i + 1);//注意标 0 或 1
      }
     }
     }
}

b6

//
//连续正整数
//
//问题描述:
//一个正整数有可能可以被表示为 n(n>=2) 个连续正整数之和,如:
//15=1+2+3+4+5
//15=4+5+6
//15=7+8
//请编写程序,根据输入的任何一个正整数,找出符合这种要求的所有连续正整数序列。
//输入数据:一个正整数,以命令行参数的形式提供给程序。
//输 出数据:在标准输出上打印出符合题目描述的全部正整数序列,每行一个序列,
//每个序列都从该序列的最小正整数开始、以从小到大的顺序打印。
//如果结果有多个序 列,按各序列的最小正整数的大小从小到大打印各序列。
//此外,序列不允许重复,序列内的整数用一个空格分隔。
//如果没有符合要求的序列,输出 “NONE” 。
//例如,对于 15 ,其输出结果是:
//1 2 3 4 5
//4 5 6
//7 8
//对于 16 ,其输出结果是:
import java.util.Scanner;

public class Test_b6 {

 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  int sum;
  int n = sc.nextInt();
  for(int i = 1; i <=(n/2); i++) {
   sum = 0;
   for(int j = i; ; j++) {
    sum = sum + j;
    
    if(sum == n) {
     System.out.print(n+"=" );
     for (int k = i; k < j; k++) {
      System.out.print(k +"+");
     }
     System.out.println(j);
     break;
    }
    if(sum > n)
     break;
   
   }
  }

 }

}

b7

//输出1到1000中能被7整除或者以7结尾的数
public class Test_b7 {

 public static void main(String[] args) {
  // for (int i = 1; i <= 1000; i++) {
  // if (i % 7 == 0 || i % 10 == 7)
  // System.out.println(i);
  // }

  // 不过这个程序要对1000个数进行遍历,其实这些数据规律性很强,可以利用这些规律,
  // 能被7整除也就是7的倍数,乘以1、2、3、4等等,以7结尾,相当于7+0、7+10、7+20、7+30等等。下面的代码是改造后的代码:

  int value1 = 7; // 表示7 的倍数这一组
  int value2 = 7; // 表示余数为7的这一组
  while (true) {
   if (value1 < value2) {
    if (value1 > 1000)
     break;
    System.out.println(value1);
    value1 += 7;
   } else if (value1 == value2) {
    if (value1 > 1000)
     break;
    System.out.println(value1);
    value1 += 7;
    value2 += 10;
   } else {
    if (value2 > 1000)
     break;
    System.out.println(value1);
    value2 += 10;
   }
  }
  // 如何不考虑顺序:
//  int number1 = 1000 / 7; // 能被7整除的数的个数
//  int number2 = 1000 / 10; // 余数为7的个数
//  int value = 7;
//  for (int i = 0; i < number1; i++) {
//   System.out.println(value);
//   value += 7;
//  }
//  value = 7;
//  for (int i = 0; i < number2; i++) {
//   if (value % 7 != 0) { // 去掉重复的
//    System.out.println(value);
//    value += 10;
//   }
//  }
 }
}

c2


import java.util.Random;  
 
public class Test_c2 {  
    private Random random = new Random();  
    //数组大小  
    private static final int SIZE = 10;  
    //要重排序的数组  
    private int[] positions = new int[SIZE];  
      
    public Test_c2() {  
        for(int index=0; index<SIZE; index++) {  
            //初始化数组,以下标为元素值  
            positions[index] = index;  
        }  
        //顺序打印出数组的值  
        dwn();  
    }  
      
    //重排序  
    public void changePosition() {  
        for(int index=SIZE-1; index>=0; index--) {  
            //从0到index处之间随机取一个值,跟index处的元素交换  
            exchange(random.nextInt(index+1), index);  
        }  
        dwn();  
    }  
      
    //交换位置  
    private void exchange(int p1, int p2) {  
        int temp = positions[p1];  
        positions[p1] = positions[p2];  
        positions[p2] = temp;  
    }  
      
    //打印数组的值  
    private void dwn() {  
        for(int index=0; index<SIZE; index++) {  
            System.out.print(positions[index]+" ");           
        }  
        System.out.println();  
    }  
 
    public static void main(String[] args) {  
     Test_c2 rs = new Test_c2();  
        rs.changePosition();  
        rs.changePosition();  
        rs.changePosition();  
    }  

 

d0

// 一个N位的十进制正整数,如果它的每个位上的数字的N次方的和等于这个数本身,则称其为花朵数。
//
// 例如:
//
// 当N=3时,153就满足条件,因为 1^3 + 5^3 + 3^3 = 153,这样的数字也被称为水仙花数(其中,“^”表示乘方,5^3表示5的3次方,也就是立方)。
//
// 当N=4时,1634满足条件,因为 1^4 + 6^4 + 3^4 + 4^4 = 1634。
//
// 当N=5时,92727满足条件。
//
// 实际上,对N的每个取值,可能有多个数字满足条件。
//
// 
//
// 程序的任务是:求N=21时,所有满足条件的花朵数。注意:这个整数有21位,它的各个位数字的21次方之和正好等于这个数本身。
//
// 如果满足条件的数字不只有一个,请从小到大输出所有符合条件的数字,每个数字占一行。因为这个数字很大,请注意解法时间上的可行性。要求程序在3分钟内运行完毕。
import java.math.BigInteger;

import java.util.Arrays;

public class Test_d0 {

 private static BigInteger[] table = new BigInteger[10];

 public static void main(String[] args) {

  long time = System.nanoTime();

  find(21);

  time = System.nanoTime() - time;

  System.out.println(time / 1000000000.0 + "s");

 }

 public static void find(int n) {

  for (int i = 0; i < 10; i++)

   table[i] = BigInteger.valueOf(i).pow(n);

  int[] nums = new int[n];

  int index = 0;

  int num = 0;

  BigInteger sum = BigInteger.ZERO;

  BigInteger MIN = BigInteger.TEN.pow(n - 1);

  BigInteger MAX = BigInteger.TEN.pow(n).subtract(BigInteger.ONE);

  while (true) {

   if (index < nums.length && num < 10) {

    BigInteger temp = sum.add(table[num]);

    if (temp.compareTo(MAX) < 0) {

     nums[index] = num;

     index++;

     sum = temp;

     continue;

    }

   } else if (index >= nums.length && sum.compareTo(MIN) > 0) {

    int[] temp = getArray(sum);

    if (check(nums, true, temp, false))

     System.out.println(sum);

   } else if (index <= 0) {

    break;

   }

   index--;

   num = nums[index];

   sum = sum.subtract(table[num]);

   num++;

  }

 }

 public static boolean check(int[] a1, boolean copy1, int[] a2, boolean copy2) {

  if (a1.length != a2.length)

   return false;

  if (copy1)

   a1 = a1.clone();

  if (copy2)

   a2 = a2.clone();

  Arrays.sort(a1);

  Arrays.sort(a2);

  return Arrays.equals(a1, a2);

 }

 public static int[] getArray(BigInteger big) {

  String s = String.valueOf(big);

  int length = s.length();

  int[] res = new int[length];

  for (int i = 0; i < length; i++)

   res[i] = s.charAt(i) - '0';

  return res;

 }

}
 

d1

public class Test_d1 {

 /*
  * 625这个数字很特别,625的平方等于390625,刚好其末3位是625本身。除了625,还有其它的3位数有这个特征吗?
  * 请编写程序,寻找所有这样的3位数:它的平方的末3位是这个数字本身。 输出结果中,从小到大,每个找到的数字占一行。比如那个625就输出为: 625
  */
//  public static void main(String[] args) {
//  int k, m;
//  for (int i = 100; i <= 999; i++) {
//  k = i * i;
//  m = k / 1000;
//  if ((k - m * 1000) == i)
//  System.out.println(i);
//  }
//  }

 public static void main(String[] args) {

  int i, mulRs, last3;

  for (i = 100; i <= 999; i++) {

   mulRs = (int) Math.pow(i, 2);

   last3 = mulRs % 1000;

   if (last3 == i)
    System.out.println(i);

  }

 }

}
 

d2

public class Test_d1 {

 /*
  * 625这个数字很特别,625的平方等于390625,刚好其末3位是625本身。除了625,还有其它的3位数有这个特征吗?
  * 请编写程序,寻找所有这样的3位数:它的平方的末3位是这个数字本身。 输出结果中,从小到大,每个找到的数字占一行。比如那个625就输出为: 625
  */
//  public static void main(String[] args) {
//  int k, m;
//  for (int i = 100; i <= 999; i++) {
//  k = i * i;
//  m = k / 1000;
//  if ((k - m * 1000) == i)
//  System.out.println(i);
//  }
//  }

 public static void main(String[] args) {

  int i, mulRs, last3;

  for (i = 100; i <= 999; i++) {

   mulRs = (int) Math.pow(i, 2);

   last3 = mulRs % 1000;

   if (last3 == i)
    System.out.println(i);

  }

 }

}
 

d3

import java.util.Vector;

//在A B C D E F 六人中随机抽取3人中奖,要求中奖人不能重复。请完善以下代码:

public class Test_d3
{
 public static void main(String[] args)
 {
  Vector<Object> a = new Vector<Object>();
  for(char i='A'; i<='F'; i++)  a.add("" + i);  
  for(int k=0; k<3; k++)
  {
//   int d = ____________________________;
//   int d = (int)(Math.random()*a.size());
   int d = (int)(Math.random()*(6-k));
   System.out.println(a.remove(d));
  }
 }
}
 

d4

//不同进制的数值间的转换是软件开发中很可能会遇到的常规问题。下面的代码演示了如何把键盘输入的3进制数字转换为十进制。试完善之。
import java.io.*;

public class Test_d4 {
 public static void main(String args[]) throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String s = br.readLine();
  int n = 0;
  for (int i = 0; i < s.length(); i++) {
   char c = s.charAt(i);
   if (c < '0' || c > '2')
    throw new RuntimeException("Format error");
   // n = ______________________;
   n = n + Character.getNumericValue(c)
     * (int) Math.pow(3, (s.length() - i - 1));
//   n = n + (c - 48) * (int) Math.pow(3, s.length() - i - 1);

  }
  System.out.println(n);
 }
}

d5

// 有如下程序,完成的功能为:找出数组中的最大元素。请填写程序的中空白,使程序运行正确。

public class Test_d5 {
 public static void main(String[] args) {
  int array[] = { 0, 34, 67, 90, 21, -9, 98, 1000, -78 };
  System.out.println(new Test_d5().findMax(array, 0));
 }

 public int findMax(int array[], int index) {
  if (array == null || array.length == 0) {
   return 0;
  }
  int max = array[0];
  if (index < array.length - 1) {
   // max=____________________
   max = findMax(array, index + 1);
  }
  if (max < array[index])
   max = array[index];

  return max;
 }
}

d6

//电视台开宝箱节目:打进电话的人可以开启一个宝箱。箱子中有一件礼品。
//礼品是iphone的机率为1/12;是mp3 的机率为1/5;是洗衣粉的机率为1/2;剩余是KFC优惠券。
// 每次打进电话,宝箱会重置。
// 以下程序模拟了该抽奖过程。请填写缺失的部分。
public class Test_d6 {
 
 public static void main(String[] args)
 {

  int a1=0; int a2 =0; int a3 =0; int a4 =0;
//  int i = (int) Math.random() * _____________;
//  for(int k =1;k<=1000000;k++){

   int i = (int) (Math.random() * 60 )+ 1;
   if (i < 5) {
    a1++;
    System.out.println("恭喜中了:iphone手机");
   }else if (i < 17) {
    a2++;
    System.out.println("恭喜中了:mp3");
   } else if (i < 47) {
    a3++;
    System.out.println("恭喜中了:洗衣粉");
   } else {
    a4++;
    System.out.println("恭喜中了:KFC优惠券");
   }
//  }
//  System.out.println(""+1.0/12+1.0/5+1.0/2+(1-(1.0/12+1.0/5+1.0/2)));
//  System.out.println("a1"+a1/1000000.0);
//  System.out.println("a2"+a2/1000000.0);
//  System.out.println("a3"+a3/1000000.0);
//  System.out.println("a4"+a4/1000000.0);
 }

}
 

d7

//下列代码求出一个二进制串中连续的1或连续的0出现的最大次数。请填缺失代码。
// 例如:s = “101100111100011”
// 则返回:4
// 又例如:s=”0111100000”
// 则返回:5
public class Test_d7 {

 public static void main(String args[]) {
  int max_1 = 0;
  int max_0 = 0;
  int n_1 = 0; // 当前1连续的次数
  int n_0 = 0; // 当前0连续的次数
  String s = "0111100000";
  for (int i = 0; i < s.length(); i++) {
   if (s.charAt(i) == '0') {
    n_0++;
    // ________;
    n_1 = 0;
   } else {
    n_1++;
    // _________;
    n_0 = 0;
   }

   if (n_1 > max_1)
    max_1 = n_1;
   if (n_0 > max_0)
    max_0 = n_0;
  }

  System.out.println(max_1 > max_0 ? max_1 : max_0);
 }
}
 

d8

//下列代码把16进制表示的串转换为3进制表示的串。试完善之。
// 例如:x=“5”
// 则返回:“12”
// 又例如:x=”F”
// 则返回:“120”
import java.util.*;

public class Test_d8 {

 private static int getRealValue(char x) {
  if (x >= '0' && x <= '9')
   return x - '0';
  if (x >= 'a' && x <= 'f')
   return x - 'a' + 10;
  if (x >= 'A' && x <= 'F')
   return x - 'A' + 10;
  return 0;
 }

 public static String jin_zhi_16_3(String x) {
  int n = 0; // 累加真值
  for (int i = 0; i < x.length(); i++) {
   // n = _________ + getRealValue(x.charAt(i)); 填空

   n = n + getRealValue(x.charAt(i));
  }

  String t = "";
  for (;;) {
   if (n == 0)
    break;
   t = (n % 3) + t;
   // _____________; 填空
   n = n - n / 3;

  }

  return t;
 }

 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  String s = sc.nextLine();

  System.out.println(Test_d8.jin_zhi_16_3(s));
 }
}
 

d9

//整数的分划问题。
// 如,对于正整数n=6,可以分划为:
// 6
// 5+1
// 4+2, 4+1+1
// 3+3, 3+2+1, 3+1+1+1
// 2+2+2, 2+2+1+1,2+1+1+1+1
// 1+1+1+1+1+1+1
// 现在的问题是,对于给定的正整数n,编写算法打印所有划分。
// 用户从键盘输入 n (范围1~10)
// 程序输出该整数的所有划分。

public class Test_d9 {

 public static void main(String[] args) {

  int n = 6;
  int last = n;

  for (int i = n; i > 0; i--) {

   last = n - i;

   if (last == 0) {
    System.out.println(i);
    continue;
   }

   find(i + "", last, i);

   System.out.println("");

  }

 }

 public static void find(String Str, int last, int max) {

  if (last < 0)
   return;

  if (last == 0)
   System.out.print(Str + ";");

  for (int i = max; i > 0; i--) {

   find(Str + "+" + i, last - i, i);

  }

 }

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值