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

[Test] Kiểm tra 60 phút - Lập trình C

Bài 1:

- Nhập vào mảng gồm N số nguyên

Yêu cầu:

- Tính tổng các số chia hết cho 5

- Tìm các số chính phương (a luỹ thừa 2).

- Sắp xếp mảng sao cho các phần tử chẵn ở đầu mảng, các phần tử lẻ ở cuối mảng

Bài 2:

Tạo struct sinhvien gồm các thuộc tính sau char name[50], rollno[20], int age.

Khai báo 1 mảng gồm N sinh viên => N nhập từ bàn phím

Hiển thị thông tin sinh viên có tuổi là số chính phương

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đặng Trần Nhật Minh [T2008A]
Đặng Trần Nhật Minh

2020-09-30 09:04:46


BAI 1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void swap(int &a, int &b) {
	
	int temp = a;
	a = b;
	b = temp;
	
}

int main() {

	printf("\nNhap n: ");
	int n; scanf("%d", &n);
	
	int a[n], sum = 0;
	
	printf("\nNhap mang: ");
	
	for (int i = 0; i < n; i++) {
		
		printf("\nNhap a[%d]: ", i);
		scanf("%d", &a[i]);
		
		if (a[i] % 5 == 0) sum += a[i];
		
	}
	
	printf("\nTong cac so chia het cho 5 la: %d", sum);
	
	printf("\nCac so chinh phuong la: ");
	
	for (int i = 0; i < n; i++) {
		
		int test = sqrt(a[i]);
		
		if (test * test == a[i]) 
			printf("\n%d", a[i]);
		
	}
	
	for(int i = 0; i < n - 1; i++)
        for (int j = i; j < n; j++)
            if (a[j] % 2 == 0) {swap(a[i], a[j]); break;}
	
	printf("\nSau sap xep: ");
	
	for (int i = 0; i < n; i++) 
		printf("%d ", a[i]);

}



Nguyễn Xuân Mai [T2008A]
Nguyễn Xuân Mai

2020-09-30 09:01:36



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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//bai 2
typedef struct student_st{
	char name[50], rollno[20];
	int age;
}student;

void hienthi(student *p){
	int i,c,d=0;
	for(i=0;i<3;i++){ 
		int c=sqrt(p[i].age);
		if(p[i].age==pow(c,2)){
			d++;
			printf("\nCac sinh vien co tuoi la so chinh phuong:");
			printf("\nTen: %s\nRollno: %s\nTuoi:%d",p[i].name,p[i].rollno,p[i].age);
		}
    }
    if(d==0){
		printf("\nKhong co sinh vien nao co tuoi la so chinh phuong");
	}
}

int main(int argc, char *argv[]) {
	//bai 1
	int i,n,sum=0,sum1=0;
	printf("\nNhap N:");
	scanf("%d",&n);
	int t[n];
	for(i=0;i<n;i++){
		printf("\nNhap t[%d]:",i);
		scanf("%d",&t[i]);
	}
	
	for(i=0;i<n;i++){
		if(t[i]%5==0){
			sum+=t[i];
		}
	}printf("\nTong cac so chia het cho 5 = %d",sum);
	
	for(i=0;i<n;i++){
		int a=sqrt(t[i]);
		if(t[i]==pow(a,2)){
			sum1+=t[i];
		}
	}printf("\nTong cac so chinh phuong = %d\n",sum1);
	
	printf("\nSo chan:");
	for(i=0;i<n;i++){
		if(t[i]%2==0){
			printf("%d\t",t[i]);
		}
	}
	
	printf("\nSo le:");
	for(i=0;i<n;i++){
		if(t[i]%2!=0){
			printf("%d\t",t[i]);
		}
	}
	
	//bai 2
	int i,b;
	printf("Nhap so sinh vien: ");
	scanf("%d",&b);
	student studentList[b];
	for(i=0;i<b;i++){
		printf("\nSinh vien %d",i+1);
		fflush(stdin);fflush(stdout);
		printf("\nNhap ten cua sinh vien:");
		gets(studentList[i].name);
		printf("\nNhap rollno:");
		gets(studentList[i].rollno);
		printf("\nNhap tuoi:");
		scanf("%d",&studentList[i].age);
	}
	hienthi(studentList);
	return 0;
}