[ create a new paste ] login | about

Link: http://codepad.org/Wk8SrCl3    [ raw code | output | fork | 2 comments ]

C, pasted on Sep 27:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100

void nhap (int a[], int &n)
{
	do
	{
		printf("\nNhap so phan tu: ");
		scanf("%d", &n);
		if(n <= 0 || n > MAX)
		{
			printf("\nSo phan tu khong hop le. Xin kiem tra lai !");
		}
	}while(n <= 0 || n > MAX);
	for(int i = 0; i < n; i++)
	{
		printf("\nNhap a[%d]: ", i);
		scanf("%d", &a[i]);
	}
}

void xuat(int a[], int n)
{
	for(int i = 0; i < n; i++)
	{
		printf("%4d", a[i]);
	}
}

int TimSoNhoNhat(int a[], int n)
{
	int min = a[0];
	for(int i = 1; i < n; i++)
	{
		if(a[i] < min)
		{
			min = a[i];  //min = (min < a[i])? min: a[i];
		}
	}
	return min;
}

// kiểm tra xem	ước số có phải là ước chung lớn nhất của mảng hay không
bool KiemTraUocSoChung(int a[], int n, int UocSo)
{
	for(int i = 0; i < n; i++)
	{
		if(a[i] % UocSo != 0)
		{
			return false;
		}
	}
	return true;
}

int TimUocSoLonNhatCuaMang(int a[], int n)
{
	// Do 1 số muốn chia hết tất cả các số trong mảng thì số đó
	// phải chia hết cho số nhỏ nhất trong mảng
	for(int UocSo = TimSoNhoNhat(a, n); UocSo >= 1; UocSo--)  
	{
		if(KiemTraUocSoChung(a, n, UocSo) == true)
		{
			return UocSo;
		}
	}
	return 1;
}
int main()
{
	int n;
	int a[MAX];

	nhap(a, n);
	xuat(a, n);

	int Kq = TimUocSoLonNhatCuaMang(a, n);
	printf("\nUoc So chung lon nhat cua mang la %d", Kq);

	getch();
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
Line 17: error: conio.h: No such file or directory
Line 6: error: expected ';', ',' or ')' before '&' token
In function 'xuat':
Line 26: error: 'for' loop initial declaration used outside C99 mode
In function 'TimSoNhoNhat':
Line 35: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 46: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraUocSoChung'
In function 'TimUocSoLonNhatCuaMang':
Line 62: error: 'for' loop initial declaration used outside C99 mode
Line 64: error: 'true' undeclared (first use in this function)
Line 64: error: (Each undeclared identifier is reported only once
Line 64: error: for each function it appears in.)


Create a new paste based on this one


Comments:
posted by FreshStudentHCMUS on Nov 10
bài này nên tìm trị tuyệt đối của số nhỏ nhất thì chuẩn hơn vì nếu mảng có phần tử âm thì dòng "for(int UocSo = TimSoNhoNhat(a, n); UocSo >= 1; UocSo--) " sẽ chạy ko đc.
Viết lại tí ạ
int FindabsMin(int a[], int n)
{
for (int i = 0; i < n; i++)
{
if (a[i] < 0)
a[i] = -a[i];
else a[i] = a[i];
}
int min = a[0];
for (int i = 0; i < n; i++)
{
if (a[i] < min)
min = a[i];
}
return min;
}
int Exc171(int a[], int n)
{
int GCD = FindabsMin(a,n);
for (int i = 0; i < n; i++)
{
while (a[i] % GCD != 0)
{
GCD--;
}
}
return GCD;
}
reply
posted by dong.nq192760@sis.hust.edu.vn on Feb 24
#include <iosteam>
#include <Windows.h>
#include <iomanip>
#include <vector>
using namespace std;
int main(){
int n;
do {
cout << "Nhap so phan tu cua mang : ";
cin >> n;
if (n <= 0) {
cout << "Yeu cau cua ban khong hop le !! " << endl;
cout << "Yeu cau ban nhap lai !! " << endl;
Sleep(2000);
system("cls");
}
} while (n <= 0);
int* p = new int[n];
cout << "Nhap gia tri cho mang : " << endl;
for (int i = 0; i < n; i++) {
cout << "Nhap p[" << i + 1 << "] = ";
cin >> p[i];
}
cout << "UOC CHUNG LON NHAT CUA TAT CA CAC PHAN TU TRONG MANG LA : ";
vector<int> array;
for (int i = 1; i <= p[0]; i++) {
if (p[0] % i == 0) {
array.push_back(i);
}
}
for (int i = 0; i < n; i++) {
for (int j = 1; j <= p[i]; j++) {
for (int k = 0; k < array.size(); k++) {
if (p[i] % j != 0 && array.at(k) == j) {
array.erase(array.begin() + k);
}
}
}
}
int Max = array.at(0);
for (int i = 0; i < array.size(); i++) {
if (Max < array[i]) {
Max = array[i];
}
}
if (Max != NULL) {
cout << setw(5) << Max << endl;
}
else {
cout << setw(5) << " 0 " << endl;
}
delete[] p;

return 0;
}

//Day la bai lam cua em bang C++
//Em thay dung vector vao bai nay kha la hay
// nen em Share cho moi nguoi doc


reply