Archive for 十一月, 2014

26
十一月

程式設計一程式 11/26: ex6

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

#include <stdio.h>
#include <stdlib.h> 
#include <time.h> 
int main()
{
    int i, j, a[6];
    int ball[49], max, index;
    srand((unsigned) time( NULL ) ); //設定亂數的啟始點
    //給每一顆球不同的重量 
    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]);
 
    return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

26
十一月

程式設計一程式 11/19: midterm

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

#include <stdio.h>
int main(int argc, char *argv[])
{
    int a, b, c;
    int num=5, i;
    FILE* fin;
 
    fin = fopen("test.txt", "r");
    for (i =0; i<5; i++)
    {
       fscanf(fin, "%d %d %d\n", &a, &b, &c);
       if (( a >= 60 && b >=60 && c >=60) ||( a + b + c >=210))
          printf("P(%d, %d, %d)\n", a, b, c);
       else 
          printf("M(%d, %d, %d)\n", a, b, c);
    }
 
    return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

12
十一月

程式設計一程式 11/12: ex5

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

#include <stdio.h>
#include <stdlib.h> 
#include <time.h> 
int main()
{
    int max, num=5, i, a;
    FILE* fout;
    printf("Input:");
    scanf("%d", &max);
    srand((unsigned) time( NULL ) ); //設定亂數的啟始點
 
    fout = fopen("randnum.txt", "w");
 
    for (i =0; i<5; i++)
    {
       a = rand % max() + 1;
       printf("the number is= %d\n" a);
       fprintf(fout, "%d\n", a);
    }
 
    fclose(fout);
    return 0;
}
======================
    int num=5, i, a;
    FILE* fin;
 
    fin = fopen("randnum.txt", "r");
 
    for (i =0; i<5; i++)
    {
       fscanf(fin, "%d\n", &a);
       printf("the number is= %d\n", a);
    }
 
    fclose(fin);
    return 0;

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

5
十一月

程式設計一程式 11/5: quiz1

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

#include 
int main(int argc, char *argv[])
{
    int a, b, c, all, x, y, z;
    a = atoi(argv[1]);
    b = atoi(argv[2]);  
    c = atoi(argv[3]);
    x = lcm(a, b);
    y = lcm(b, c);
    z = lcm(x, y);
    all= (z/a)*(z/b)*(z/c);
    printf("input=(%d, %d, %d)ans=%d ", a, b, c, all);
    return 0;
int lcm(int a, int b)
{
    int num1, num2, r;
    if (a > b)
    {
        num1 = a; num2 = b;
    }
    else
    {
        num1 = b; num2 = a;
    }
    r = num1 % num2;
    //如果 r是0 
    while (r !=0)
    {
          num1 = num2;
          num2 = r;
          r = num1 % num2;
    }
    return a*b/num2;
==========================================
    int a, i, j, sum;
    a = atoi(argv[1]);
    for (i=2; i< a; i++)
    {
        sum = 0;
        for (j = 1; j < i; j++)
        {
            if (i % j ==0)
            {
                  //sum = sum + j;
                  sum += j;
            }
        }
        if (sum == i)
           printf(" %d", i);
    }

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,