Posts Tagged ‘103(上)’

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: , , ,

31
十二月

程式設計一程式 12/31: Ex9Class.java

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

import java.io.*;  //#include <stdio.h>
import java.util.*;//#include <stdlib.h> 
public class Ex9Class {
	public 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;
	    {
		    FileWriter writer = new FileWriter("lotto.txt"); //fopen("lotto.txt" "w");
		    PrintWriter pw = new PrintWriter(writer);
 
		    for (k = 0; k < N;  k++)
		    {
		      //給每一顆球不同的重量 
		      for (i=0; i<49; i++)
		      {
		          //ball[i] = rand();
		    	  ball[i] = rnd.nextInt();////產生1個亂數
		      }
		      //取出最重的球(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", a[0], a[1], a[2], a[3], a[4], a[5]);
               pw.printf("%2d %2d %2d %2d %2d %2d", a[0], a[1], a[2], a[3], a[4], a[5]); 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

Tags: , , ,

24
十二月

程式設計一程式 12/24: ex8

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

//ex8a
int compare( const int *arg1, const int *arg2 );
//對陣列a的6個元素是:a[0], a[1], ....a[5]排序
       qsort(a, 6, sizeof(int), compare);
int compare( const int *arg1, const int *arg2 )
{
   return *arg1-*arg2;
}
=========
//ex8b
qsort(bingo, 6, sizeof(int), compare );
//舊的陣列比較,沒有排序
for (i = 0; i&lt; 6; i++)
          {
              for (j = 0; j&lt; 6; j++)
              {
                  if (bingo[i] == a[j])
                       matches++;
              }
          }
//新的陣列比較,有排序
for (i = 0, j=0; i&lt; 6 &amp;&amp; j &lt; 6;   )
          {
              if (bingo[i] == a[j])
              {
                  i++; j++; matches++;
              }
              else if (bingo[i] &gt; a[j])
                   j++
              else //if (bingo[i] &lt; a[j])
                   i++;
          }
//ex8a.c 完整
#include <stdio.h>
#include <stdlib.h> 
#include <time.h> 
int compare( const int *arg1, const int *arg2 );
int main()
{
    int i, j, k, N, a[6];
    int ball[49], max, index;
    FILE* fout;
 
    scanf("%d", &N);
    srand((unsigned) time( NULL ) ); //設定亂數的啟始點
    fout = fopen("lotto.txt", "w");
    for (k = 0; k < N;  k++)
    {
      //給每一顆球不同的重量 
      for (i=0; i<49; i++)
      {
          ball[i] = rand();
      }
      //取出最重的球(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; //把最重的球重量歸零 
       }
       //對陣列a的6個元素是:a[0], a[1], ....a[5]排序
       qsort(a, 6, sizeof(int), compare);
       printf("%2d %2d %2d %2d %2d %2d\n", a[0], a[1], a[2], a[3], a[4], a[5]);  
       fprintf(fout, "%2d %2d %2d %2d %2d %2d\n",a[0], a[1], a[2], a[3], a[4], a[5]);      
    }
    fclose(fout);
    return 0;
}
int compare( const int *arg1, const int *arg2 )
{
   return *arg1-*arg2;
}
//ex8b.c
#include <stdio.h>
int compare( const int *arg1, const int *arg2 );
int main()
{
    int N;
    int i, j, k, a[6], bingo[6], count[7];
    int cc, matches;
    FILE* fin;
 
    scanf("%d %d %d %d %d %d", 
          &bingo[0], &bingo[1], &bingo[2], &bingo[3],  &bingo[4],  &bingo[5]);
    qsort(bingo, 6, sizeof(int), compare );
 
    count[0] = count[1] = count[2] = count[3]= count[4]= count[5] = count[6] =0;
 
    fin = fopen("lotto-sort.txt", "r");
    cc = fscanf(fin, "%d %d %d %d %d %d", 
          &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
    while (cc == 6)
    {
          matches = 0;
          /*for (i = 0; i< 6; i++)
          {
              for (j = 0; j< 6; j++)
              {
                  if (bingo[i] == a[j])
                       matches++;
              }
          }*/
          for (i = 0, j=0; i< 6 && j < 6;   )
          {
              if (bingo[i] == a[j])
              {
                  i++; j++; matches++;
              }
              else if (bingo[i] > a[j])
                   j++
              else //if (bingo[i] < a[j])
                   i++; 
          } 
          count[matches]++;
          cc = fscanf(fin, "%d %d %d %d %d %d", 
                        &a[0], &a[1], &a[2], &a[3],  &a[4], &a[5]);
    }
    for (k = 6; k >=0; k--)
         printf("Match %d numbers = %5d\n", k, count[k]);
 
    fclose(fin);
    return 0;
}
int compare( const int *arg1, const int *arg2 )
{
   return *arg1-*arg2;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

17
十二月

程式設計一程式 12/17: Quiz2

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

#include 
 #include <stdio.h>
int main(int argc, char *argv[])
{
    int i, P;
    int H, M, h, m, S, D, Total;
    FILE* fin;
 
    fin = fopen("q2.txt", "r");
 
    fscanf(fin, "%d\n", &P);
    for (i=0; i<P; i++)
    {
        fscanf(fin, "%d %d %d %d %d %d\n",
         &H, &M, &h, &m, &S, &D);
         if (m > M)
            Total = (h -H + 1) * S * D;
         else
            Total = (h -H) * S * D;
        printf("%d\n", Total);
    }
    fclose(fin);
    return 0;
} }}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

3
十二月

程式設計一程式 12/03: ex7

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

int main()
{
    int N;
    int i, j, k, a[6];
    int ball[49], max, index;
    FILE* fout;  
    srand((unsigned) time( NULL ) ); //設定亂數的啟始點   
    scanf("%d"  &N);
    fout = fopen("lotto.txt" "w");
 
    for (k = 0; k < N  k++)
    {
      //給每一顆球不同的重量 
      for (i=0; i<49; i++)
      {
          ball[i] = rand();
      }
      //取出最重的球(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; //把最重的球重量歸零 
       }
 
       printf("The number is %2d %2d %2d %2d %2d %2d\n", a[0], a[1], a[2], a[3], a[4], a[5]);
       fprintf(fout,        "%2d %2d %2d %2d %2d %2d\n", a[0], a[1], a[2], a[3], a[4], a[5]);
    }
    fclose(fout);
    return 0;
}
======================
int main()
{
    int N;
    int i, j, k, a[6], bingo[6], count[7];
    int cc, matches;
    FILE* fin;
 
 
    scanf("%d %d %d %d %d %d", 
          &bingo[0], &bingo[1], &bingo[2], &bingo[3],  bingo[4]  bingo[5]);
    count[0] = count[1] = count[2] = count[3]= count[4]= count[5] = count[6] =0;
 
    fin = fopen("lotto.txt", "r");
    cc = fscanf(fin, "%d %d %d %d %d %d", 
          &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
    while (cc == 6)
    {
          matches = 0;
          for (i = 0; i< 6; i++)
          {
              for (j = 0; j< 6; j++)
              {
                  if (bingo[i] == a[j])
                       matches++;
              }
          }
          count[matches]++;
          cc = fscanf(fin, "%d %d %d %d %d %d", 
                        &a[0], &a[1], &a[2], &a[3],  a[4], &a[5]);
    }
    for (k = 6; k >=0; k--)
         printf("Match %d numbers = %5d\n", k count[k]);
 
    fclose(fin);
    return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,