Archive for 一月, 2015

7
一月

程式設計一程式 1/7: Ex10.cs

   Posted by: admin    in 103(上)程式設計(一)

using System;
using System.IO;
 
namespace Ex10
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = 1000;
            int i, j, k;
            int max, index;
            int[] a = new int[6];//int a[6];
            int[] ball = new int[49];//int ball[49];
 
            Random rnd = new Random();
            try//FILE* fout;  
            {
                //fopen("lotto.txt" "w");
                StreamWriter sw = new StreamWriter("lotto.txt");
                for (k = 0; k < N; k++)
                {
                    //給每一顆球不同的重量 
                    for (i = 0; i < 49; i++)
                        //ball[i] = rnd.nextInt();//rand();
                        ball[i] = rnd.Next();
                    //取出最重的球(6次) 
                    for (i = 0; i < 6; i++)
                    {
                        max = 0; index = 0;
                        for (j = 0; j < 49; j++)
                        {
                            if (ball[j] > max)
                            {
                                max = ball[j];
                                index = j;
                            }
                        }
                        a[i] = index + 1;//最重的球編號 
                        ball[index] = 0; //把最重的球重量歸零 
                    }
                    //System.out.printf("The number is %2d %2d %2d %2d %2d %2d\n", 
                    Console.WriteLine("{0,2} {1,2} {2,2} {3,2} {4,2} {5,2}", a[0], a[1], a[2], a[3], a[4], a[5]);
                    sw.WriteLine("{0,2} {1,2} {2,2} {3,2} {4,2} {5,2}", a[0], a[1], a[2], a[3], a[4], a[5]);
                }
                sw.Close();
            }
            catch (FileNotFoundException fe)
	        {
                //System.out.printf(fe.getMessage());
	    	    Console.WriteLine(fe.Message);
	        }
	        catch (IOException e)
	        {
	    	    //System.out.printf(fe.Message);
                Console.WriteLine(e.Message);
	        }
        }
    }
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,