`
chendang1314
  • 浏览: 102197 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

跟踪每日一题

    博客分类:
  • java
阅读更多
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 
 * @author Administrator
 * 随机产生100个1到99的数,得到最大和最小以及大于50的个数
 *
 */
public class TestFor01 {

	/**
	 * 引用redom方法:
	 * int nextInt() 
          返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。 
 	   int nextInt(int n) 
          返回一个伪随机数,它是从此随机数生成器的序列中取出的、在 0(包括)
          和指定值(不包括)之间均匀分布的 int值。 

	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Random rd=new Random();
		int count=0;
		int[] tmp=new int[100];
		for(int i=0;i<100;i++){
			if((tmp[i]=rd.nextInt(100))>50)
				count++;
			
				if(i!=0&&i%5==0){
					System.out.println(";");
				}
					
				System.out.print(tmp[i]+"|");
			
		}
		System.err.println();
		System.err.println("The Rresult:");
		System.err.println("大于50的总数:"+count);
		List l=Compare(tmp);
		System.err.println("Max======="+l.get(0));
		System.err.println("Min======="+l.get(1));
	}
	/**
	 * @param 
	 * @return
	 */
	public static  List Compare(int[] param){
		List res=new ArrayList();
		int max=0;
		int min=100;
		for(int i=0;i<param.length;i++){
			if(max<param[i])
				max=param[i];
			if(min>param[i])
				min=param[i];
		}
		res.add(max);
		res.add(min);
		return res;
	}
}

public class TestFor02 {

	/**
	 * 1到11有4个“1”,1中一个“1”,10中一个“1”,
	 * 11中两个“1”,1000中,有多少个“1”,欢迎给出你的解法!
	 * @param args
	 */
	
	public static int Count(int param){
		int count=0;
//		char[] tmp=Integer.toString(param).toCharArray();
//		for(int i=0;i<tmp.length;i++){
//			if(tmp[i]=='1')
//				count++;
//		}
		String s=String.valueOf(param);
		for(int j=0;j<s.length();j++){
			if(s.charAt(j)=='1')
				count++;
		}
		return count;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int result=0;
		for(int i=1;i<=1000;i++){
			result+=Count(i);
		}
		System.out.println("The result is=====:"+result);
	}
}



import java.text.DecimalFormat;

public class TestFor03 {

	/**
	 * @param args
	 * 02 A 04 A 06 A 08 A 10 A; 
	 * 12 A 14 A 16 A 18 A 20 A; 
     * 22 A 24 A 26 A 28 A 30 A; 
     * 32 A 34 A 36 A 38 A 40 A; 
     * 42 A 44 A 46 A 48 A 50 A; 
		重点1:数字的格式:02,04 
		重点2:如果不是这道题,已经忘记除了i++以外的写法了
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DecimalFormat df=new DecimalFormat();
		df.applyPattern("00");
		for(int i=2;i<=50;i+=2){
			if(i%10==0)
				System.out.println(" "+df.format(i)+"A;");
			else
				System.out.print(" "+df.format(i)+"A");
		}
	}
}



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TestFor04 {

	/**
	 * @param args
	 * 
	 * 昨天本来想写一段很简单的小程序,关于文件复制,但是遇到了问题,
	 * 所以把问题提出来,希望大家能够帮我解决,万分感谢! 
	 * 如何将一个文件从一个目录复制到另一个目录?
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Method1();
//		Method2();
	}
	public static void Method1(){
		String src= "E:\\html\\form.htm";
		String tar="F:\\";
		InputStream is=null;
		OutputStream os=null;
		File srcFile=new File(src);
		String tarFile=tar+srcFile.getName();
		byte[] buf=new byte[1024];
		try {
			is=new FileInputStream(srcFile);
			os=new FileOutputStream(tarFile);
			int c;
			while((c=is.read(buf))>0) {
				os.write(buf,0,c);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try
			{
				if(is!=null)
					is.close();
				if(os!=null)
					os.close();
			}
			catch(IOException e)
			{
				System.out.println(e);
			}		
		}
	}
	/**
	 * 有错待解,可能是。。。。
	 */
	public static void Method2(){
        try {   
            String[] command = new String[] {   
                    "copy",   
                    "E:\\html\\form.htm",   
                    "F:\\" };   
            Process child = Runtime.getRuntime().exec(command);   
        } catch (IOException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
        }   
	}
}




import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class TestFor05 {

	/**
	 * @param args
	 * 一个for语句循环10次产生10个100以内的随机数
	 * 要求数字不为0和不重复 
	 * 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Set tmp=new HashSet();
		Random rd=new Random();
		for(int i=0;i<10;i++){
			int j;
			if((j=rd.nextInt(100))!=0)
				tmp.add(j);
		}
		System.out.println(tmp);
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics