Archive for the ‘102(上)程式設計(一)’ Category

4
十二月

程式設計一上課 12/4: Java的亂數處理

   Posted by: admin Tags: , ,

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

4
十二月

程式設計一程式 12/4: Ex11Class.java

   Posted by: admin Tags: , ,

import java.util.*;
import java.io.*;
public class Ex11Class {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i; 
	    int num, pos;
	    try
	    {
		    FileWriter writer = new FileWriter("record.txt");
		    PrintWriter pw = new PrintWriter(writer);
		   //從命令列讀入一個參數 
		    num = Integer.parseInt(args[0]);
 
		  //建立亂數的物件
		    Random rnd = new Random();
		  //產生1000個亂數
		    for (i=0; i<num; i++)
		    {
		        pos = rnd.nextInt();////產生1個亂數
		        if (pos <0) pos=-pos;
		        pos = pos%10000;
		        System.out.printf("%d\n", pos);
		        pw.printf("%d", pos);pw.println();
		    } 
		    writer.close();
	    }
	    catch (FileNotFoundException fe)
	    {
	    	System.out.printf(fe.getMessage());
	    }
	    catch (IOException fe)
	    {
	    	System.out.printf(fe.getMessage());
	    }
	}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

4
十二月

程式設計一程式 12/4: ex11.c

   Posted by: admin Tags: , ,

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int i; 
    int num, pos;
    FILE* out;
 
    //從命令列讀入一個參數 
    num = atoi(argv[1]);
    //設定亂數的啟始點
    srand( (unsigned) time( NULL ) );
 
    out = fopen("number.txt", "w");
    //產生1000個亂數
    for (i=0; i<num; i++)
    {
        pos = rand();////產生1個亂數
        printf("%d\n", pos);
        fprintf(out, "%d\n", pos);
    } 
    fclose(out);
    return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

27
十一月

程式設計一上課 11/27: 比對的複雜度

   Posted by: admin Tags: , ,

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

27
十一月

程式設計一程式 11/27: ex10.c

   Posted by: admin Tags: , ,

int main(int argc, char *argv[])
{
    int i, j, count;
    int bingo[6];
    int num[6], yescount;
    FILE* in;
 
    srand( (unsigned) time( NULL ) );//設定亂數的啟始點
 
    NumGen(bingo);
    //排序
    qsort(bingo, 6, sizeof(int), compare );
    printf("The bingo numbers: %2d %2d %2d %2d %2d %2d\n", 
                bingo[0], bingo[1], bingo[2], bingo[3], bingo[4], bingo[5]);
 
    in = fopen("lotto.txt", "r");
    count = fscanf(in, "%d %d %d %d %d %d\n", &num[0], &num[1], &num[2], &num[3], &num[4], &num[5]); 
    while (count == 6)
    {
          yescount = 0;
          for (i=0, j=0; i< 6, j<6; )
          {
              if (num[i] < bingo[j])
                 i++;
              else if (num[i] > bingo[j])
                 j++;
              else
              {
                  yescount++;
                  i++; j++;
              }
          }
          printf("%2d %2d %2d %2d %2d %2d= %d \n", num[0], num[1], num[2], num[3], num[4], num[5], yescount);
          count = fscanf(in, "%d %d %d %d %d %d\n", &num[0], &num[1], &num[2], &num[3], &num[4], &num[5]);
    }
 
    fclose(in);
    return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark