lỗi ở đây là ko ai chịu xài nhập xuất = file cả. :O
Tìm thấy 22 mục
Tam giác Pascal - 27/11/2009, 21:40
còn muốn nó cân thì làm gì đc ?
số sẽ bị phình ra từ từ, ai mà kiểm soát cho nổi, tam giác chỉ cân khi n là số nhỏ.
tam giác vuông như cái bài cũ nhìn mới đẹp đc.
.
Tam giác Pascal - 27/11/2009, 21:36
- Code:
#include "stdio.h"
#define MAX 100
int a[2][MAX];
void xuat(int *a,int j,int n){
int i;
printf("%*c",(n-j+1)*2,' ');
for (i=1;i<=j;i++)
printf("%2d%2c",a[i],' ');
printf("\n");
}
int main()
{
int n = 10;
int i,j;
int flag = 0;
a[flag][1] = 1;
xuat(a[flag],1,n);
for (i=2;i<=n;i++){
flag = 1-flag;
for (j=1;j<=i;j++) a[flag][j] = a[1-flag][j]+a[1-flag][j-1];
xuat(a[flag],i,n);
}
return 0;
}
làm luôn phần trình bày, thực ra là chép từ cái code ở trên.
Tam giác Pascal - 27/11/2009, 21:31
- Code:
#include "stdio.h"
#define MAX 100
int a[2][MAX];
void xuat(int *a,int n){
int i;
for (i=1;i<=n;i++)
printf("%5d",a[i]);
printf("\n");
}
int main()
{
int n = 5;
int i,j;
int flag = 0;
a[flag][1] = 1;
xuat(a[flag],1);
for (i=2;i<=n;i++){
flag = 1-flag;
for (j=1;j<=i;j++) a[flag][j] = a[1-flag][j]+a[1-flag][j-1];
xuat(a[flag],i);
}
return 0;
}
phần trình bày chính giữa, làm biếng làm lắm. :O.
help me !! ( mất kiến thức mẫu giáo ^__^ ) - 26/11/2009, 22:01
thầy dạy * là dành cho C pro xài.
& là reference dành cho mấy em C++ đời sau.
Hỏi chút xíu coi. - 26/11/2009, 21:58
her her, vậy send đại nick 1 đứa con gái nào lớp 85 có onl đi.
Code các giải thuật về sắp xếp - 26/11/2009, 21:55
Tự sướng :
- Code:
#include <stdio.h>
#include <conio.h>
#include <mem.h>
#define MAX 1000
int a[MAX];
int b[MAX];
int n;
void swap(int *a,int *b){
int t = *a;
*a = *b;
*b = t;
}
void xuat(int *a){
int i;
for (i=0;i<n;i++) printf(" %d",a[i]);
printf("\n");
}
void selectionSort(){
memmove(b,a,sizeof(b));
printf("Selection Sort : \n");
int i,j;
for (i=0;i<n-1;i++)
for (j=i+1;j<n;j++)
if (b[i] > b[j]) swap(&b[i],&b[j]);
xuat(b);
}
void bubbleSort(){
memmove(b,a,sizeof(b));
printf("Bubble Sort : \n");
int i,j;
for (i=0;i<n-1;i++)
for (j=n-1;j>i;j--)
if (b[j] < b[j-1]) swap(&b[j],&b[j-1]);
xuat(b);
}
void shakerSort(){
memmove(b,a,sizeof(b));
printf("Shaker Sort : \n");
printf("Day ban dau :");
xuat(b);
int left,right,k;
int j;
left = 0; right = n-1; k = n-1;
while (left < right){
for (j=right;j>left;j--)
if (b[j] < b[j-1]){
swap(&b[j],&b[j-1]);
k = j;
}
printf("Sau khi don gia tri min ve phia ben trai :");
xuat(b);
left = k;
for (j=left;j<right;j++)
if (b[j] > b[j+1]){
swap(&b[j],&b[j+1]);
k = j;
}
right = k;
printf("Sau khi don gia tri max ve phia ben phai :");
xuat(b);
}
printf("Day sau khi sap xep :\n");
xuat(b);
}
void insertionSort(){
memmove(b,a,sizeof(b));
printf("Insertion Sort : \n");
int i,j;
int x;
for (i=1;i<n;i++){
x = b[i];
j = i-1;
while (j >= 0 && x < b[j]){
b[j+1] = b[j];
j--;
}
b[j+1] = x;
}
xuat(b);
}
void binaryInsertionSort(){
memmove(b,a,sizeof(b));
printf("Binary Insertion Sort : \n");
int i,j;
int x;
int lo,hi,mid;
for (i=1;i<n;i++){
x = b[i];
lo = 0; hi = i-1;
do
{
mid = (lo + hi)/2;
if (x > b[mid]) lo = mid + 1;
else hi = mid - 1;
}
while (lo <= hi);
for (j=i;j>lo;j--) b[j] = b[j-1];
b[lo] = x;
// xuat(b);
}
xuat(b);
}
void shellSort(){
memmove(b,a,sizeof(b));
printf("Shell Sort : \n");
int i,j,h;
int x;
h = n/2;
while (h != 0){
for (i=h;i<n;i++){
x = b[i];
j = i-h;
while (j >= 0 && b[j] > x){
b[j+h] = b[j];
j -= h;
}
b[j+h] = x;
}
h = h/2;
}
xuat(b);
}
void adjust(int root,int endnode){
int t = b[root];
int r;
while (root*2+1 <= endnode){
r = root*2+1;
if (r < endnode && b[r] < b[r+1]) r++;
if (t >= b[r]) break;
b[root] = b[r];
root = r;
}
b[root] = t;
}
void heapSort(){
memmove(b,a,sizeof(b));
printf("Heap Sort : \n");
int r,i,t;
for (r=(n-1)/2;r>=0;r--) adjust(r,n-1);
for (i=n-1;i>0;i--){
t = b[0];
b[0] = b[i];
b[i] = t;
adjust(0,i-1);
}
xuat(b);
}
void qsort(int lo,int hi){
int i,j,t;
int k = b[(lo+hi)/2];
i = lo; j = hi;
do
{
while (i < hi && b[i] < k) i++;
while (j > lo && b[j] > k) j--;
if (i <= j){
t = b[i];
b[i] = b[j];
b[j] = t;
i++; j--;
}
}
while (i <= j);
if (lo < j) qsort(lo,j);
if (i < hi) qsort(i,hi);
}
void quickSort(){
memmove(b,a,sizeof(b));
printf("Quick Sort : \n");
qsort(0,n-1);
xuat(b);
}
void merge(int src[],int des[],int fi1,int fi2,int len){
int la1,la2;
int ndes = fi1;
la1 = fi1+len;
la2 = fi2+len;
if (la2 > n) la2 = n;
while (fi1 < la1 && fi2 < la2){
if (src[fi1] < src[fi2])
des[ndes] = src[fi1++];
else
des[ndes] = src[fi2++];
ndes++;
}
if (fi1 < la1) memmove(des+ndes,src+fi1,(la1-fi1)*sizeof(b[0]));
else memmove(des+ndes,src+fi2,(la2-fi2)*sizeof(b[0]));
}
void mergeByLen(int src[],int des[],int len){
int i,j;
for (i=0;i+len<n;i+=(len*2))
{
// printf("%d %d\n",i,i+len);
merge(src,des,i,i+len,len);
}
// printf("%d\n",i);
// xuat(des);
}
void mergeSort(){
memmove(b,a,sizeof(b));
int t[MAX];
printf("Merge Sort : \n");
// xuat(b);
int flag = 1;
int len = 1;
while (len < n){
if(flag) mergeByLen(b,t,len);
else mergeByLen(t,b,len);
len = len*2;
flag = !flag;
}
if (!flag) memcpy(b,t,n*sizeof(b[0]));
xuat(b);
}
int main(){
int i;
FILE *fi = fopen("sort.in","r");
fscanf(fi,"%d",&n);
for (i=0;i<n;i++) fscanf(fi,"%d",&a[i]);
fclose(fi);
printf("Day ban dau : \n");
xuat(a);
selectionSort();
bubbleSort();
insertionSort();
binaryInsertionSort();
shellSort();
heapSort();
quickSort();
mergeSort();
for (i=0;i<79;i++) printf("_");
printf("\n");
printf("Giai thich shaker sort :\n");
shakerSort();
getch();
return 1;
}
có chỗ ko hiểu cần người chỉ giáo - 26/11/2009, 21:50
không xài return NULL nếu đó là hàm void, chỉ xài trên con trỏ.
xài return ; (bỏ null)
Bai tap 3.1 su dung danh sach lien ket doi - 26/11/2009, 21:47
ngắn hơn, nhanh hơn, khó hiểu hơn nhưng hội đủ đ/k of 1 bài toán 
Bai tap 3.1 su dung danh sach lien ket doi - 26/11/2009, 21:45
- Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node {
int val;
node *prev;
node *next;
};
struct list {
node *fst;
node *lst;
};
void init(list &a){
a.fst = a.lst = NULL;
}
node* new_val(int val){
node *p;
p = (node*) malloc(sizeof(node));
p->val = val;
return p;
}
void make_list(list &a,int val){
node *p = new_val(val);
p->prev = a.lst;
p->next = NULL;
if (a.fst == NULL) a.lst = a.fst = p;
else
{
(a.lst)->next = p;
a.lst = (a.lst)->next;
}
}
node *found(list &a,int val){
node *p = a.fst;
while (p != NULL && p->val != val) p=p->next;
return p;
}
void insert(list &a,node *k,int val){ //insert val vao sau node k
if (k == NULL) return ;
node *p = new_val(val);
p->next = k->next;
k->next = p;
p->prev = k;
if (k == a.lst) a.lst = p;
}
void del(list &a,node *k){ //delete node k
if (k == NULL) return ;
node *trc = k->prev;
node *sau = k->next;
if (trc != NULL) trc->next = sau;
else a.fst = sau; //k la con tro dau
if (sau != NULL) sau->prev = trc;
else a.lst = trc; //k la con tro cuoi
k->prev = k->next = NULL;
free(k);
}
void xuat(list a){
node *p;
p = a.fst;
// p = a.lst;
while (p != NULL){
printf(" %d",p->val);
p = p->next;
// p = p->prev;
}
printf("\n");
}
int main(){
int t;
list a;
FILE *fi = fopen("a.in","r");
init(a);
while (fscanf(fi,"%d",&t) != EOF) make_list(a,t);
fclose(fi);
printf("Danh sach ban dau :\n");
xuat(a);
insert(a,found(a,31),10);
xuat(a);
del(a,found(a,19));
xuat(a);
}
Nhập vào một số nguyên (0 -> <1000) - 26/11/2009, 21:44
thế rãnh wá del bài tui à ?
để cái topic of tui ở đâu rồi ?
help me !! - 26/11/2009, 21:36
cách 1: làm cho con trỏ đầu of danh sách liên kết trỏ tới NULL, rồi nối lại.
cách 2: xài đệ quy để in ngược
void xuat(node *x){
if (x == NULL) return ;
xuat(x->next);
printf("%d",x->value);
}


