By GokiSoft.com| 20:10 20/09/2023|
Lập Trình C

Bài tập ôn luyện mảng 1 chiều - Lập trình C BT1894

Bài 1 : Nhập vào mảng gồm N số nguyên - In ra danh sách ngược của mảng đó

Ví du : mảng nhập vào là : 1, 2, 3, 4, 5, 6 -> In ra kết quả như sau : 6, 5, 4, 3, 2, 1

Bài 2 : Nhập vào mảng gồm N số nguyện -> Thực hiện hoán đổi ngược lại vị tri của mang

ví du : mảng a gồm các phần tử 1, 2, 3, 4, 5, 6 -> yêu cầu chuyển đổi để mảng a chưa danh sách ngược : 6, 5, 4, 3, 2, 1

Liên kết rút gọn:

https://gokisoft.com/1894

Bình luận

avatar
Nguyễn đình quân [T2008A]
2020-09-24 07:50:41



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

//bat dau code;
//bai 2;
int main(int argc, char *argv[]) {
int i = 0, n, t;
		printf("\nNhap n: ");
		scanf("%d, &n");
		int a[n];
	for(i = 1; i < n; i++){
		
		printf("\nNhap n[%d]: ", i);
		scanf("%d, &a[i]");
}		
	for(i = n -1; i >= 0; i--){
	printf("%d",a[i]);
	scanf("%d", &a[i]);
	
}
       
     //ket thuc code;
	return 0;
	
}
	






avatar
Nguyễn Anh Vũ [T2008A]
2020-09-20 08:36:11



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

//bat dau code;
//bai 2;
int main(int argc, char *argv[]) {
int i = 0, n, t;
		printf("\nNhap n: ");
		scanf("%d, &n");
		int a[n];
	for(i = 1; i < n; i++){
		
		printf("\nNhap n[%d]: ", i);
		scanf("%d, &a[i]");
}		
	for(i = n -1; i >= 0; i--){
	printf("%d",a[i]);
	scanf("%d", &a[i]);
	
}
       
     //ket thuc code;
	return 0;
	
}
	



avatar
Nguyễn Anh Vũ [T2008A]
2020-09-20 08:35:29



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

//bat dau code;
//bai 2;
int main(int argc, char *argv[]) {
int i = 0, n, t;
		printf("\nNhap n: ");
		scanf("%d, &n");
		int a[n];
	for(i = 1; i < n; i++){
		
		printf("\nNhap n[%d]: ", i);
		scanf("%d, &a[i]");
}		
	for(i = n -1; i >= 0; i--){
	printf("%d",a[i]);
	scanf("%d", &a[i]);
	
}
       
     //ket thuc code;
	return 0;
	
}
	



avatar
Nguyễn Anh Vũ [T2008A]
2020-09-20 08:18:36



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
        //bat dau code;
	//BAI 1
	int n;
	printf("\nNhap so n:");
	scanf("%d", &n);
	int h[n], i;
	
	for(i = 0; i < n; i++){
		printf("\nNhap h[%d]:", i);
		scanf("%d", &h[i]);
	}
	for(i = n -1; i >= 0; i--){
		printf("\nNhap h[%d]:", i);
		scanf("%d", &h[i]);
	}
	
	//ket thuc code;
	return 0;


avatar
TRẦN VĂN ĐIỆP [Teacher]
2020-09-18 07:13:49



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int N;
	printf("\nNhap N: ");
	scanf("%d", &N);
	int t[N];//length: N, index = 0 -> N - 1
	//Nhap phan tu trong mang N
	int i;
	for(i=0;i<N;i++) {
		printf("\nNhap N[%d] = ", i);
		scanf("%d", &t[i]);
	}
	//TEST ket qua phan tu trong mang
	printf("\nDanh sach cac phan tu trong mang\n");
	for(i=0;i<N;i++) {
		printf("%d ", t[i]);
	}
	//Cau 1: In ra phan tu theo thu tu tu N-1 => 0
	printf("\nIn mang nguoc lai\n");
	for(i=N-1;i>=0;i--) {
		printf("%d ", t[i]);
	}
	//Cau 2: {1, 2, 3, 4, 5} => t: {5, 4, 3, 2, 1}
	int N2 = N/2;
	for(i=0;i<N2;i++) {
		//swap => doi vi tri phan tu i vs N - i
		//1, 2, 5, 8, 9 => 9, 8, 5, 2, 1
		//Giai thuat
		//0 <=> 4, 1 <=> 3, 2???
		//0 <=> 5 - 0 - 1 = 4
		//1 <=> 5 - 1 - 1 = 3
		//2 : stop
		//Cong thuc tong quat
		//N => N2 = N/2
		//i <=> N-i-1, i+1 <=> N-i-1-1 = N - i - 2
		//i + 2 <=> N - i - 3 (i=0)
		int tmp = t[i];
		t[i] = t[N-i-1];
		t[N-i-1] = tmp;
		//a = 2, b = 5 => swap => a = 5, b = 2
		//tmp = a = 2, a = 5, b = tmp = 2
	}
	printf("\nDanh sach cac phan tu trong mang\n");
	for(i=0;i<N;i++) {
		printf("%d ", t[i]);
	}
	return 0;
}


avatar
Đặng Trần Nhật Minh [T2008A]
2020-09-18 06:09:59


BAI 2
#include "bits/stdc++.h"

int main() {

	int n, a[n];
	
	printf("\nNhap n: "); scanf("%d", &n);
	
	for (int i = 0; i < n; i++) printf("\na[%d] = ", i + 1), scanf("%d", &a[i]);
	
	for (int i = 0; i < (n / 2); i++) {
		
		int temp = a[i];
		a[i] = a[n - i - 1];
		a[n - i - 1] = temp;
		
	}
	
	for (int i = 0; i < n; i++) printf("\na[%d] = %d", i + 1, a[i]);
	
	return 0;

}


avatar
Nguyễn Hữu Hiếu [T2008A]
2020-09-18 06:04:37



//Bai 2


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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	
	int i,n,j,m,k;
	
	printf("Nhap so phan tu n = ");
	scanf("%d", &n);
	m = n;
	int mang[n], hoandoi[m];
	
	for(i=0;i<n;i++) {
		printf("Nhap phan tu thu %d: ", i);
		scanf("%d", &mang[i]);
		j = n-1-i;;
		hoandoi[j] = mang[i];
		j--;
	}
	for(i=0;i<n;i++) {
		printf("%d, ", hoandoi[i]);
	}
	
	
	return 0;
}


avatar
Đặng Trần Nhật Minh [T2008A]
2020-09-18 05:45:58

BAI 1


#include "bits/stdc++.h"

int main() {

	int n, a[n];
	
	printf("\nNhap n: "); scanf("%d", &n);
	
	for (int i = 0; i < n; i++) printf("\na[%d] = ", i + 1), scanf("%d", &a[i]);
	
	for (int i = n - 1; i >= 0; i--) printf("%d ", a[i]);
	
	return 0;

}


avatar
Nguyễn Hữu Hiếu [T2008A]
2020-09-18 05:29:28



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	
	int n,i;
	printf("\nNhap so phan tu cua mang n = ");
	scanf("%d", &n);
	int mang[n];
	for(i=0;i<n;i++) {
		printf("\nNhap phan tu thu %d: ", i);
		scanf("%d", &mang[i]);
	}	
	
	printf("\nPhan tu cua mang nguoc lai la: ");
	
		for(i=n-1;i>=0;i--) {
		printf("%d", mang[i]);			
	}


	
	
	
	return 0;
}


avatar
Đỗ Mạc Nam [T2008A]
2020-09-18 04:57:29

Bai 2


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

int main(int argc, char *argc[]) 
{
	int n;
	printf("\nNhap n: ");
	scanf("%d", &n);
	if(n<=0) {
		printf("\nSai");
	}
	else{
		int a[n],i=0;
		while(i<n){
			printf("Nhap a[%d] = ",i);
			scanf("%d",&a[i]);
		i++; 
		}
		
	}
	int b;
	printf("\n1.Ban co muon dao nguoc vi tri cua mang khong: ");
	printf("\n2.Co: ");
	printf("\n3.Khong: ");
	printf("\n4.Chon: ");  
	scanf("%d", &b);
	
	switch(b){
			case 1:
				i = n - 1;
				while(i>=0){
					printf("\nGia tri Mang nguoc la: %d", k[i]);
					i--;
				}
			break;
			
			case 2:
				printf("\nKhong co gi thay doi: ");

			break;
		}

	return 0;
}