Archive for 十月, 2014

29
十月

程式設計一程式 10/29: ex4

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

#include 
#include <stdlib.h> 
#include <time.h> 
int main()
{
    int i, a[6];
    srand((unsigned) time( NULL ) ); //設定亂數的啟始點
    for (i=0; i<6; i++)
    {
      a[i]= rand()%49 + 1;// 產生一個虛擬的亂數值
    }
    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: , ,

22
十月

程式設計一程式 10/22: ex3

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

#include 
int main(int argc, char *argv[])
{
    int a, b;
    int num1, num2, r;
    a = atoi(argv[1]);
    b = atoi(argv[2]);   
    printf("gcd(%d, %d)=%d ", a, b, gcd);
    return 0;
}
int gcd(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 num2;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

15
十月

程式設計一程式 10/15: ex2

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

#include 
int main(int argc, char *argv[])
{
    int a, b, c
    a = ato(argv[1])
    b = ato(argv[2])
    c = a * b;
    printf(" %3d * %3d = %4d (0x%X)\n", a, b, c);
    return 0;
}
=======================================
int main(int argc, char *argv[])
{
    double a, b, c;
    a=atof(argv[1]); 
    b=atof(argv[2]); 
    c = a * b;
    printf(" %.2f * %.2f = %.4f \n", a, b, c);
    return 0;//<-返回值 
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,