玩玩小程序

来源:互联网 发布:java软件开发培训中心 编辑:程序博客网 时间:2024/05/21 08:25
一、L的3.41次方+40000L的1.56次方=21780889,帮我算出L

解决方案,正常算,确实不会算,采用无限趋近法,得到近似值,编程采用递归


import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.Console;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.util.ArrayList;import java.util.List;public class Io {public static void main(String args[]) throws Exception{new Io().operate(1, 1000, 1);}private void operate(double min,double max,double add){boolean bool = true;List<Double> list = new ArrayList<Double>();List<Double> listSub = new ArrayList<Double>();double sub = 0;for(double i=min;i<max;i+=add){list.add(i);double result = Math.pow(i, 3.41) + Math.pow(i, 1.56)*40000;if((sub = Math.abs(result-21780889)) < 0.001){bool = false;break;}else{listSub.add(sub);}}if(bool){int pos = 0;for(int j=0;j<listSub.size()-1;j++){pos = j;if(listSub.get(j)<listSub.get(j+1)){break;}}operate(list.get(pos-1), list.get(pos+1), add/10);}else{System.out.println("最终结果:"+list.get(list.size()-1)+"    "+sub);}}}


二、


import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.Console;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.util.ArrayList;import java.util.List;public class Io {public static void main(String args[]) throws Exception{String path = "c:\\weibo.txt";String content = null;String account[];//read the txt;content = new Io().readTxt(path);// show all of the accounts on the screenaccount = content.split("#");for(int i=0,len=account.length;i<len;i++){System.out.println(account[i]);}// " " replace # and save in the flodercontent = content.replace("#", " ");new Io().writeTxt("c:\\chulihou.txt", content);  //chulihou.txt这出题人也够NB嗒//delete the floderFile file = new File(path);file.delete();}private String readTxt(String path) throws Exception{StringBuffer string = new StringBuffer();String temp;BufferedReader in =new BufferedReader(new FileReader(path));while((temp = in.readLine()) != null){string.append(temp);}in.close();return string.toString();}private void writeTxt(String path, String content) throws Exception{FileWriter   fw=new   FileWriter( path ); BufferedWriter   bw   =new   BufferedWriter(fw); bw.write( content );bw.flush();bw.close();fw.close();}}


原创粉丝点击