import java.util.Arraysimport java.util.Random
import java.util.Scanner
public class SerachFunction {
public static int[] allnum=new int[20]
// picture ...like a picture
public void picOne(){
System.out.println("************")
System.out.println("1.选择排序")
System.out.println("2.冒泡排序")
System.out.println("3.插入排序")
System.out.println("4.全排列")
System.out.println("5.数字分解为数字和")
System.out.println("6.杀死小朋友问题")
System.out.println("7.阶乘")
System.out.println("8.双色球")
System.out.println("9.100-1000的水仙花数")
System.out.println("10.正数分解因数")
System.out.println("11.恐怖的事情 千万不要输入11")
System.out.println("输入-1退出")
System.out.println("输入-2查看问题注释")
System.out.println("************")
}
public void picTwo(){
System.out.println("注意输入单个字符参数(根据选项输入,多个参数以逗号分割): ")
}
public void picThree(){
System.out.println("5号问题为输入某一数字 出现这个数字可由哪些数字相加而得")
System.out.println("6号问题为9(输入)个已编号的小朋友围一圈报数1,2,3报到3的拖出去xx问剩下的小朋友是几号")
}
public void picFour(){
System.out.println("0>_<0~")
System.out.println("恐怖的事情发生了...说了不要点...")
System.out.println("楼主累死了....")
}
// main
public static void main(String[] args){
SerachFunction fc=new SerachFunction()
fc.inputFunction()
}
public void inputFunction(){ //接受指令并调用相关函数
int command
char[] params
Scanner sc=new Scanner(System.in)
while(true){
picOne()
if((command=sc.nextInt())==-1){
break
}
picTwo()
params=getParamsArray(sc.next())
System.out.println()
switch(command){
case 1:
selectSort(params)
break
case 2:
bubbleSort(params)
break
case 3:
insertionSort(params)
break
case 4:
fullArray(params,0)
break
case 5:
resolveNum(getQuondam(params),0)
break
case 6:
killChildren(getQuondam(params))
break
case 7:
factorial(getQuondam(params),1)
break
case 8:
twoColorBall()
break
case 9:
daffodil()
break
case 10:
int all=0
primeFactor(getQuondam(params),0)
break
case 11:
picFour()
break
case -2:
picThree()
break
}
System.out.println("Y(^_^)Y")
}
System.out.println("(*^_^*)")
}
/*
* com function
* */
public char[] getParamsArray(String params){
params=params.replaceAll(",","")
return params.toCharArray()
}//to get char[] params
// 选择排序
public void selectSort(char[] params){
for(int i=0i<params.length-1i++){
for(int m=i+1m<params.lengthm++){
if(params[i]>params[m]){
changePlace(params,m,i)
}
}
}
showChar(params)
System.out.println("选择排序结束")
}
//冒泡排序
public void bubbleSort(char[] params){
for(int i=params.length-1i>0i--){
for(int m=0m<im++){
if(params[m]>params[m+1]){
changePlace(params,m,m+1)
}
}
}
showChar(params)
System.out.println("冒泡排序结束")
}
//插入排序
public void insertionSort(char[] params){
for(int i=1i<params.lengthi++){
for(int m=i-1m>=0m--){
if(params[m+1]<params[m]){
changePlace(params,m+1,m)
}
}
}
showChar(params)
System.out.println("插入排序结束")
}
//全排列
public void fullArray(char[] params,int begin){
if(begin==params.length){
showChar(params)
}
for(int i=begini<params.lengthi++){
if(begin<=params.length){
changePlace(params,begin,i)
fullArray(params,begin+1)
changePlace(params,begin,i)
}
}
}
//数字分解为数字和
public void getAllNum(int[] num,int end){
System.out.print("分解方式:")
for(int i=0i<endi++){
System.out.print(num[i])
if(i+1!=end){
System.out.print(',')
}
}
System.out.println("")
}
public int getArrayT(int[] num,int end){
int tot=0
for(int i=0i<endi++){
tot+=num[i]
}
return tot
}
public boolean getShoud(int[] num,int end){
for(int i=0i<endi++){
if(i+1<end){
if(num[i+1]<num[i]){
return false
}
}
}
return true
}
public void resolveNum(int num,int begin){
if(getArrayT(allnum,begin)==num){
if(getShoud(allnum,begin)){
getAllNum(allnum,begin)
}
}
for(int i=1i<=numi++){
if(getArrayT(allnum,begin)<=num){
allnum[begin]=i
resolveNum(num,begin+1)
}
}
}
// 杀死小朋友
public void killChildren(int child){
int alivechild=child
int num=0
int index=0
boolean[] childisdead=new boolean[child]
Arrays.fill(childisdead,true)
while(alivechild!=1){
if(childisdead[index]){
num++
if(num%3==0){
alivechild--
childisdead[index]=false
num=0
}
}
index++
if(index==child)
index=0
}
for(int i=0i<childisdead.lengthi++){
if(childisdead[i])
System.out.println((int)(i+1)+"号小朋友还活着")
}
}
//阶乘
public void factorial(int end,long output){
if(end==1){
System.out.println(output)
return
}
output=output*end
factorial(end-1,output)
}
//two color ball
public void twoColorBall(){
int[] redball=new int[6]
int blueball=(int)(Math.random()*15+1)
int index=0
while(true){
boolean bol=true
int red=(int)(Math.random()*32+1)
for(int i=0i<=indexi++){
if(red==redball[index]){
bol=false
}
}
if(bol){
redball[index]=red
index++
}
if(index==6){
break
}
}
System.out.print("红色:")
for(int i=0i<6i++){
System.out.print(redball[i]+",")
}
System.out.println("")
System.out.println("蓝色:"+blueball)
}
//水仙花
public void daffodil(){
int hundreds=0,decade=0,unit=0
System.out.print("水仙花数:")
for(int i=100i<1000i++){
hundreds=i/100
decade=(i%100)/10
unit=(i%100)%10
hundreds=hundreds*hundreds*hundreds
decade=decade*decade*decade
unit=unit*unit*unit
if(i==(hundreds+decade+unit)){
System.out.print(i+",")
}
}
System.out.println("")
}
//分解为因数
public void getPriShow(int[] num,int end){
System.out.print("分解方式:")
for(int i=0i<endi++){
System.out.print(num[i])
if(i+1!=end){
System.out.print('*')
}
}
System.out.println("")
}
public void primeFactor(int num,int begin){
if(num==1){
if(getShoud(allnum,begin)){
getPriShow(allnum,begin)
}
return
}
for(int i=2i<=numi++){
if(num%i==0){
allnum[begin]=i
primeFactor(num/i,begin+1)
}
}
}
//展示char数组
public void showChar(char[] params){
for(int i=0i<params.lengthi++){
System.out.print(params[i])
if(i+1!=params.length){
System.out.print(',')
}
}
System.out.println("")
}
//两数换位置
public void changePlace(char[] params,int begin,int end){
int act
act=params[begin]
params[begin]=params[end]
params[end]=(char) act
}
//得到原数
public int getQuondam(char[] num){
return Integer.parseInt(String.valueOf(num))
}
}
//水仙花水的那个就是
代码如下
class Box{
private int width
private int length
private int height
public Box(int width,int length,int height){
this.width = width
this.length = length
this.height = height
}
public void showBox(){
System.out.println("盒子的width、length、height分别为"+width+","+length+","+height)
}
}
如果有帮助到你,请点击采纳
哈哈 我昨天刚做了这个作业 你够幸运import java.awt.*
import java.awt.event.*
import javax.swing.*
import java.math.*
public class Homework10_3 {
public static void main(String args[]){
MathWindow win=new MathWindow()
}
}
class MathWindow extends JFrame{
JTextField text1,text2,text3
JPanel ps,pn
MathWindow(){
text1=new JTextField(10)
text2=new JTextField(10)
text3=new JTextField(10)
JButton button1,button2,button3,button4
button1=new JButton("加")
button2=new JButton("减")
button3=new JButton("乘")
button4=new JButton("除")
ps=new JPanel()
pn=new JPanel()
pn.add(text1)
pn.add(text2)
pn.add(text3)
ps.add(button1)
ps.add(button2)
ps.add(button3)
ps.add(button4)
add(pn,BorderLayout.CENTER)
add(ps,BorderLayout.SOUTH)
setBounds(100,100,370,150)
setVisible(true)
validate()
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=text1.getText()
String s2=text2.getText()
try{ BigInteger n1=new BigInteger(s1)
BigInteger n2=new BigInteger(s2)
n2=n1.add(n2)
text3.setText(n2.toString())
}
catch(NumberFormatException ee){
text3.setText("请输入数字字符")
text1.setText(null)
text2.setText(null)
}
}
})
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=text1.getText()
String s2=text2.getText()
try{ BigInteger n1=new BigInteger(s1)
BigInteger n2=new BigInteger(s2)
n2=n1.subtract(n2)
text3.setText(n2.toString())
}
catch(NumberFormatException ee){
text3.setText("请输入数字字符")
text1.setText(null)
text2.setText(null)
}
}
})
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=text1.getText()
String s2=text2.getText()
try{ BigInteger n1=new BigInteger(s1)
BigInteger n2=new BigInteger(s2)
n2=n1.multiply(n2)
text3.setText(n2.toString())
}
catch(NumberFormatException ee){
text3.setText("请输入数字字符")
text1.setText(null)
text2.setText(null)
}
}
})
button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=text1.getText()
String s2=text2.getText()
try{ BigInteger n1=new BigInteger(s1)
BigInteger n2=new BigInteger(s2)
if(n2.toString()=="0"){
text3.setText("除数不能为0")
}
else
{
n2=n1.divide(n2)
text3.setText(n2.toString())
}
}
catch(NumberFormatException ee){
text3.setText("请输入数字字符")
text1.setText(null)
text2.setText(null)
}
}
})
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)