Việt - Nhật 's class
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.
Latest topics
» Oblivion - Phim Club
Week11 (full) I_icon_minitime06/02/14, 11:10 am by sshin_conan

» các bạn/anh/chị cho hỏi đề thi giải tích 1
Week11 (full) I_icon_minitime03/05/13, 09:47 pm by ictk56

» Đề nghị sát nhập 2 diễn đàn
Week11 (full) I_icon_minitime18/02/12, 11:01 am by ngoanhtuan_hn

» Hội nghị sinh viên Việt Nam Nhật Bản tuyển thành viên
Week11 (full) I_icon_minitime17/02/12, 11:43 pm by michiosatoo

» mình có câu này, mong các bạn dịch giúp!!!
Week11 (full) I_icon_minitime16/11/11, 06:17 pm by asakurayo

» nho forum chut
Week11 (full) I_icon_minitime15/11/11, 11:29 am by asakurayo

» CS 1.6 MAP MAKER
Week11 (full) I_icon_minitime15/11/11, 07:55 am by asakurayo

» ubuntu-win
Week11 (full) I_icon_minitime12/10/11, 05:46 pm by darklord226

» diễn đàn hedspi k56
Week11 (full) I_icon_minitime11/10/11, 09:41 pm by lakazai

Similar topics

Week11 (full)

+4
Woodyx
huy_d_t
truonggiang.bka
haidang001
8 posters

Go down

Week11 (full) Empty Week11 (full)

Bài gửi by haidang001 30/04/11, 05:33 pm

Bài tập lần này khá nhẹ đô So good.
Đề bài:
Bài 1: avegare
Code:

    Cho mot mang so thuc. Viet ham su dung con tro lam tham so tra ve con tro tro toi phan tu co gia tri gan nhat voi gia tri trung binh cua mang. Test bang chuong trinh.
Bài 2: kombat
Code:

    Mot game doi khang cho phep tao ra cac dau thu (moi dau thu co ba chi so suc manh, nhanh nhen, va mau)
    Viet chuong trinh tao ra ba dau thu voi cac chi so ban dau tuy chon. Chuong trinh su dung ham: arena nhan tham so la cac dau si, dua ra ket qua la nguoi thang cuoc. Sau mot tran dau nguoi thang cuoc cac chi so tang 2%. Nguoi thua cuoc tang 1%. Ngoai ra mot dau si con duoc diem % thuong ngau nhien.
    Cho ba nguoi thi dau vong tron 1, 2, 3, . . . so lan tuy chon va in ra ket qua cung chi so cuoi cung cua moi dau thu.

Tip:
Bài 1: average
No tip
Bài 2: kombat
No tip

Solution:
Bài 1: average
Code:

/*Copyright (c) haidang001 (tm) (@yahoo.com)*/
/*2011 Allright reserved*/

/*    Cho mot mang so thuc. Viet ham su dung con tro lam tham so tra ve con tro tro toi phan tu co gia tri gan nhat voi gia tri trung binh cua mang. Test bang chuong trinh.*/

#include <stdio.h>
#define maxsize 100

void enter(double a[], int *n);
double calsum(double a[], int n), *average(double a[], int n, double val), absolute(double);

main()
{
  double a[maxsize], sum = 0, *ave;
  int n;
 
  enter(a, &n);
  sum = calsum(a, n);
  ave = average(a, n, sum / n);
  printf("\nElement having the nearest value to the average of array: %p\n", ave);
  printf("Value: %-.2lf\n", *ave);

  printf("\nS_have fun! :)\n");
  return 0;
}

double *average(double a[], int n, double val)
{
  int i, imin = 1;

  for(i = 1; i <= n; i++)
    if (absolute(val - a[i]) < absolute(val - a[imin]))
      imin = i;
  return (a + imin);
}

void enter(double a[], int *n)
{
  double temp;
  int i;

  printf("Size of array: ");
  scanf("%d", n);
  for(i = 1; i <= *n; i ++)
    {
      printf("A[%d] = ", i);
      scanf("%lf", &temp);
      a[i] = temp;
    };
 
  return;
}

double calsum(double a[], int n)
{
  double res = 0;
  int i;

  for(i = 1; i <= n; i++)
    res = res + a[i];
 
  return res;
}

double absolute(double x)
{
  return (x > 0) ? x : -x;
}

//S_have fun! :)
Bài 2: kombat
Riêng bài 2, ae bỏ qua 3 hàm này (vì hơi phức tạp chút So good )
Code:

void leftwin();
void rightwin();
void gamedraw();
Code full đây:
Code:

/*Copyright (c) haidang001 (tm) (@yahoo.com)*/
/*2011 Allright reserved*/

/*    Mot game doi khang cho phep tao ra cac dau thu (moi dau thu co ba chi so suc manh, nhanh nhen, va mau)
    Viet chuong trinh tao ra ba dau thu voi cac chi so ban dau tuy chon. Chuong trinh su dung ham: arena nhan tham so la cac dau si, dua ra ket qua la nguoi thang cuoc. Sau mot tran dau nguoi thang cuoc cac chi so tang 2%. Nguoi thua cuoc tang 1%. Ngoai ra mot dau si con duoc diem % thuong ngau nhien.
    Cho ba nguoi thi dau vong tron 1, 2, 3, . . . so lan tuy chon va in ra ket qua cung chi so cuoi cung cua moi dau thu.*/

#include
#include
#include

#define maxsize 4
#define win 0.02
#define draw 0.015
#define fail 0.01
#define reward 0.007

void menu(), enter(double a[][maxsize], int *), play(double [][maxsize], int, int), printindex(double a[][maxsize]), leftwin(), rightwin(), sleep(double), gamedraw(), update(double *, double);
double read(char []), point(double a[][maxsize], int x);
char name[4][50] = {"", "Koltec_haidang\0", "xlKD\0", "Huy.d.t\0"};

main()
{
  double a[maxsize][maxsize];
  char ans[100];
  int n;
  srand((unsigned)time(NULL));
 
  menu();
  enter(a, &n);
  while (n > 0)
    {
      n--;
      play(a, 1, 2);
      printf("(Next round %s Vs %s)", name[2], name[3]);
      gets(ans);
     
      play(a, 2, 3);
      printf("(Next round %s Vs %s)", name[3], name[1]);
      gets(ans);
     
      play(a, 3, 1);
      if (n > 0)
    printf("(Next round %s Vs %s)", name[1], name[2]);
      gets(ans);
    };
 
  printf("\nS_have fun! :)\n");
  return 0;
}

double point(double a[][maxsize], int x)
{
  double t = a[x][1];
  t = t * a[x][2] * a[x][3];
  return t;
}


int arena(double a[][maxsize], int x, int y)
{
  if (point(a, x) == point(a, y))
    return 0;
  else
    if (point(a, x) < point(a, y))
      return y;
    else
      return x;
}

void update(double *x, double add)
{
  *x = *x + (*x) * add;
  return;
}

void play(double a[][maxsize], int x, int y)
{
  int i, t;
 
  printf("\n\t\tHEDSPI 's Arena\n");
  printf("\t    %s Vs %s\n\n", name[x], name[y]);
  printf("%-30s%-30s\n", name[x], name[y]);
  printf("%5sS : %-21.2lf%5sS : %-.2lf\n", "", a[x][1], "", a[y][1]);
  printf("%5sA : %-21.2lf%5sA : %-.2lf\n", "", a[x][2], "", a[y][2]);
  printf("%5sHP: %-21.2lf%5sHP: %-.2lf\n", "", a[x][3], "", a[y][3]); 
  t = arena(a, x, y);
  if (t == 0)
    {
      printf("\n");
      gamedraw();
      printf("Arena drawed\n");
      for(i = 1; i <= 3; i ++)
    {
      update(&a[x][i], draw);
      update(&a[y][i], draw);
    };
    }
  else
    {
      printf("\n");
      (t == x) ? leftwin() : rightwin();
      printf("%s win\n", name[t]);
      for(i = 1; i <= 3; i++)
    {
      update(&a[t][i], win);
      update(&a[x + y - t][i], fail);
    };
    };
 
  t = (rand() % 2 == 0) ? x : y;
  printf("%s randomly receives reward\n", name[t]);
  for(i = 1; i <= 3; i++)
    update(&a[t][i], reward);
 
  printf("Index after kombat:\n");
  printindex(a);
  return;
}

void printindex(double a[][maxsize])
{
  int i;
 
  printf("\n-----------------------------------------------\n");
  printf("|%-15s|%-9s|%-9s|%-9s|\n", "Character", "Strength", "Agility", "HP");
  printf("-----------------------------------------------\n");
  for(i= 1; i <= 3; i++)
    printf("|%-15s|%-9.2lf|%-9.2lf|%-9.2lf|\n", name[i], a[i][1], a[i][2], a[i][3]);
  printf("-----------------------------------------------\n");
 
  return;
}

void menu()
{
  printf("\n\t\tKombat stimulater v1.0\n");
  printf("\tCopyright (c) haidang001 (tm) (@yahoo.com)\n");
  printf("\t\t2011 Allright reserved\n\n");
  return;
}

double read(char str[])
{
  double x;
  do
    {
      printf("%s", str);
      scanf("%lf", &x);
      getchar();
    }
  while (x <= 0);
  return x;
}

void enter(double a[][maxsize], int *n)
{
  int i;
  for(i = 1; i <= 3; i ++)
    {
      printf("Enter three index(s) of %s:\n", name[i]);
      a[i][1] = read("\tStrength index: ");
      a[i][2] = read("\tAgility index: ");
      a[i][3] = read("\tHP index: ");
      printf("\n");
    };

  do
    {
      printf("How many times you want characters to play: ");
      scanf("%d", n);
      getchar();
    }
  while (*n < 1);
  return;
}

void sleep(double seconds)
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait);
  return;
}

void gamedraw()
{
  int i, j;
  char s[23] = "~!@#x$%^|&*(),.<>?/[]{}";

  srand((unsigned)time(NULL));
 
  for(i = 1; i <= 29; i++)
    {
      fflush(stdout);
      printf("%c", '\015');
      for(j = 1; j < i; j++)
      printf("=");
      printf(">");
      printf("%-*s", 60 - 2 * i, "");
      printf("<");
      for(j = 2; j <= i; j++)
      printf("=");
      sleep(0.1);
    };

  for(j = 1; j <= 100; j++)
    {
      printf("%c", '\015');
      if ((j % 3 != 1) || (j == 100))
    for(i = 1; i <= 28; i++)
      printf("=");
      else
    printf("%28s", "");

      if (j == 100)
    printf(">||<");
      else
    printf(">%c%c<", s[rand() % 22], s[rand() % 22]);

      if ((j % 3 != 0) || (j == 100))
    for(i = 1; i <= 28; i++)
      printf("=");
      else
    printf("%28s", "");
      fflush(stdout);
      sleep(0.1);
    };

  for(j = 1; j <= 7; j++)
    {
      printf("%c", '\015');
      if (j % 2 == 1)
    for(i = 1; i <= 28; i++)
      printf("=");
      else
    printf("%28s", "");
      printf(">||<");
      if (j % 2 == 1)
    for(i = 1; i <= 28; i++)
      printf("=");
      else
    printf("%28s", "");
      fflush(stdout);
      sleep(0.5);
    };
 
  printf("\n");
 
  return ;
}

void leftwin()
{
  int i, j;
  char s[] = "~!@#x$%^&*(),.<>?/[]{}";

  srand((unsigned)time(NULL));
 
  for(i = 1; i <= 29; i++)
    {
      fflush(stdout);
      printf("%c", '\015');
      for(j = 1; j < i; j++)
      printf("=");
      printf(">");
      printf("%-*s", 60 - 2 * i, "");
      printf("<");
      for(j = 2; j <= i; j++)
      printf("=");
      sleep(0.1);
    };

  for(j = 1; j <= 100; j++)
    {
      printf("%c", '\015');
      if ((j % 5 != 1) || (j == 100))
    for(i = 1; i <= 28; i++)
      printf("=");
      else
    printf("%28s", "");

      if (j == 100)
    printf(">**<");
      else
    printf(">%c%c<", s[rand() % 22], s[rand() % 22]);

      if ((j % 3 != 0) || (j == 100))
    for(i = 1; i <= 28; i++)
      printf("=");
      else
    printf("%28s", "");
      fflush(stdout);
      sleep(0.1);
    };

  for(j =  30; j <= 60; j++)
    {
      printf("%c", '\015');
      printf("%*s", j - 30, "");
      for(i = 1; i <= 29; i++)
    printf("=");
      printf(">|");
      for(i = 1; i <= 59 - j; i++)
    printf("=");
      fflush(stdout);
      sleep(0.1);
    };

  for(j = 1; j <= 7; j++)
    {
      printf("%c", '\015');
      printf("%30s", "");
      if ((j == 7) || (j % 2 == 1))
    for(i = 1; i <= 29; i++)
      printf("=");
      else
    printf("%29s", "");
      printf(">|");
      fflush(stdout);
      sleep(0.5);
    };
 
  printf("\n");
 
  return ;
}

void rightwin()
{
  int i, j;
  char s[] = "~!@#x$%^&*(),.<>?/[]{}";

  srand((unsigned)time(NULL));
 
  for(i = 1; i <= 29; i++)
    {
      fflush(stdout);
      printf("%c", '\015');
      for(j = 1; j < i; j++)
      printf("=");
      printf(">");
      printf("%-*s", 60 - 2 * i, "");
      printf("<");
      for(j = 2; j <= i; j++)
      printf("=");
      sleep(0.1);
    };

  for(j = 1; j <= 100; j++)
    {
      printf("%c", '\015');
      if ((j % 3 != 1) || (j == 100))
    for(i = 1; i <= 28; i++)
      printf("=");
      else
    printf("%28s", "");

      if (j == 100)
    printf(">**<");
      else
    printf(">%c%c<", s[rand() % 22], s[rand() % 22]);

      if ((j % 5 != 0) || (j == 100))
    for(i = 1; i <= 28; i++)
      printf("=");
      else
    printf("%28s", "");
      fflush(stdout);
      sleep(0.1);
    };

  for(j =  32; j >= 4; j--)
    {
      printf("%c", '\015');
      for(i = 1; i <= j - 4; i++)
    printf("=");
    printf(">|<");
      for(i = 1; i <= 29; i++)
    printf("=");
      printf("%*s", 32 - j, "");
      fflush(stdout);
      sleep(0.1);
    };

  for(j = 1; j <= 7; j++)
    {
      printf("%c", '\015');
      printf("|<");
      if ((j == 7) || (j % 2 == 1))
    for(i = 1; i <= 29; i++)
      printf("=");
      else
    printf("%29s", "");
      printf("%29s", "");
      fflush(stdout);
      sleep(0.5);
    };
 
  printf("\n");
 
  return ;
}

//S_have fun! :)



Được sửa bởi haidang001 ngày 04/05/11, 09:57 pm; sửa lần 2.
haidang001
haidang001

Tổng số bài gửi : 91
Points : 176
Join date : 25/09/2010
Age : 31

http://haidang001.tk

Về Đầu Trang Go down

Week11 (full) Empty Re: Week11 (full)

Bài gửi by truonggiang.bka 01/05/11, 06:37 pm

anh e cho hoi:bai 1 thi tra ve gia tri cua con tra hay tra ve gia tri cua phan tu mang ma con tro tro toi vay?bai 2 thi dung con tro nhu the nao?t khong thay cho nao can thiet phai dung den con tro ca?
truonggiang.bka
truonggiang.bka

Tổng số bài gửi : 5
Points : 6
Join date : 04/10/2010

Về Đầu Trang Go down

Week11 (full) Empty Re: Week11 (full)

Bài gửi by huy_d_t 02/05/11, 01:54 pm

bài 1:
Code:
//compile with command: gcc -lm --std=c99 hw1_week11.c

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

#define maxsize 100

float avg(float x[maxsize],int n) {
  float a=0;
  for(int i=0;i<n;i++) {
    a+=x[i]/n;
  }
  return a;
}

float min(float x[maxsize],int n) {
  float min=x[0];
  for(int i=1;i<n;i++)
    if (x[i]<min)
      min=x[i];
  return min;
}

void swap(float *a,float *b) {
  float tmp;
  tmp=*a;
  *a=*b;
  *b=tmp;
}

void init(float x[maxsize],int n) {
  srand(time(NULL));
  for(int i=0;i<n;i++)
    x[i]=rand()%100+1;
}

void deltacalc(float a[maxsize],float b[maxsize],int n,float d) {
  for( int i=0;i<n;i++)
    b[i]=fabs(a[i]-d);
}

float *avgnear(float a[maxsize],float b[maxsize],int n,int *n1, float x) {
  float cmp=min(b,n);
  int min;
  for(int i=0;i<n;i++) {
    if (b[i]==cmp)
      min=i;
  }
  *n1=min;
  return (a+min);
}

void main () {
  float x[maxsize];
  int n;
  //khoi tao
  printf("Input number of elements:  ");
  scanf("%d",&n);
  init(x,n);
  printf("All elements of array (randomization generated): \n\n");
  for(int i=0;i<n;i++) { 
    printf(" x[%03d]: %06.2f |",i,x[i]);
    if ((i+1)%5==0)
      printf("\n");
  }

  float delta[maxsize];
  deltacalc(x,delta,n,avg(x,n));
  printf("\n\nArithmetic mean of all elements:  %.2f\n\n",avg(x,n));
  float *ave;
  int *n1;
  ave=avgnear(x,delta,n,n1,avg(x,n));
  printf("Element that nearest AM: X[%d]\n",*n1);
  printf("Memory Address: %p\n", ave);
  printf("Value: %6.2f\n\n",*ave);
}
huy_d_t
huy_d_t

Tổng số bài gửi : 112
Points : 142
Join date : 26/09/2010
Đến từ : Đại dâm tặc

Về Đầu Trang Go down

Week11 (full) Empty Re: Week11 (full)

Bài gửi by Woodyx 02/05/11, 09:29 pm

nói zậy chớ t vẫn chưa hiểu rõ đề bài lun. Sad
Woodyx
Woodyx

Tổng số bài gửi : 26
Points : 35
Join date : 27/09/2010
Age : 31
Đến từ : Hà nội pro

Về Đầu Trang Go down

Week11 (full) Empty Re: Week11 (full)

Bài gửi by Feed|1st 04/05/11, 08:44 pm

đọc xong éo hiểu gì cả @@ mà k hiểu tại sao mình nhập số liệu thì trận nào kotex cũng thắng @@
Feed|1st
Feed|1st

Tổng số bài gửi : 26
Points : 37
Join date : 27/09/2010

Về Đầu Trang Go down

Week11 (full) Empty Re: Week11 (full)

Bài gửi by em_la_T.O.P 04/05/11, 09:08 pm

em tưởng hàm sleep phải include cả <windows.h>
mà nó éo compile được trên linux thì bỏ mợ
em_la_T.O.P
em_la_T.O.P

Tổng số bài gửi : 137
Points : 173
Join date : 26/09/2010

Về Đầu Trang Go down

Week11 (full) Empty Re: Week11 (full)

Bài gửi by haidang001 04/05/11, 09:22 pm

Feed|1st đã viết:đọc xong éo hiểu gì cả @@ mà k hiểu tại sao mình nhập số liệu thì trận nào kotex cũng thắng @@
Tại ông thích nhập cho Koltec thắng đấy chứ. Ak, mà hôm qua vừa update code đấy Very Happy.

em_la_T.O.P đã viết:em tưởng hàm sleep phải include cả
mà nó éo compile được trên linux thì bỏ mợ

sleep đây là hàm tự viết, không phải sleep của windows T.O.P ak.
Code:

include `time.h`
void sleep(double seconds)
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait);
  return;
}
haidang001
haidang001

Tổng số bài gửi : 91
Points : 176
Join date : 25/09/2010
Age : 31

http://haidang001.tk

Về Đầu Trang Go down

Week11 (full) Empty Re: Week11 (full)

Bài gửi by em_la_T.O.P 04/05/11, 09:28 pm

So good So good So good So good
em_la_T.O.P
em_la_T.O.P

Tổng số bài gửi : 137
Points : 173
Join date : 26/09/2010

Về Đầu Trang Go down

Week11 (full) Empty Re: Week11 (full)

Bài gửi by Tung_tobe 04/05/11, 10:36 pm

bài này chả hiểu j cả Sad(
Tung_tobe
Tung_tobe

Tổng số bài gửi : 100
Points : 124
Join date : 27/09/2010

Về Đầu Trang Go down

Week11 (full) Empty Re: Week11 (full)

Bài gửi by asakurayo 05/05/11, 12:37 am

kho hieu wa !
asakurayo
asakurayo

Tổng số bài gửi : 38
Points : 62
Join date : 27/12/2010
Age : 32
Đến từ : hai duong

Về Đầu Trang Go down

Week11 (full) Empty Re: Week11 (full)

Bài gửi by Sponsored content


Sponsored content


Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết