玩玩小程序
一、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 screen
account = content.split("#");
for(int i=0,len=account.length;i<len;i++){
System.out.println(account[i]);
}
// " " replace # and save in the floder
content = content.replace("#", " ");
new Io().writeTxt("c:\\chulihou.txt", content); //chulihou.txt这出题人也够NB嗒
//delete the floder
File 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();
}
}

浙公网安备 33010602011771号