[ create a new paste ] login | about

Link: http://codepad.org/LsBsab75    [ raw code | output | fork ]

C, pasted on Jan 6:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include<iostream>
#include<ctime>
#include<cmath>
using namespace std;

void TaoMangNgauNhien(int* a, int n)
{
	srand(time(0));
	for(int i = 0; i < n; i++)
	{
		// Random từ 1 đến 50
		a[i] = rand() % 50 + 1;
	}
}

void NhapMang(int*a, int n)
{
	for(int i = 0; i < n; i++)
	{
		printf("\nNhap a[%d] = ", i);
		scanf("%d", a++);
	}
}

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

bool KiemTraDoiXung(int x)
{
	int sodaonguoc = 0;
	int temp = x;
	while(temp != 0)
	{
		sodaonguoc = sodaonguoc * 10 + temp % 10;
		temp = temp / 10;
	}
	if(sodaonguoc == x)
		return true;
	return false;
}

int TimSoDoiXungDauTien(int* a, int n)
{
	for(int i = 0; i < n; i++)
	{
		bool checkdoixung = KiemTraDoiXung(a[i]);
		if(checkdoixung == true)
			return a[i];
	}
	return -1;
}

bool KiemTraSoCoChuSoDauLe(int x)
{
	int temp = x;
	while(temp > 9)
	{
		temp /= 10;
	}
	if(temp % 2 == 0)
		return true;
	return false;
}

int TimSoCoChuSoDauTienLe(int* a, int n)
{
	for(int i = 0; i < n; i++)
	{
		bool checkchusodaule = KiemTraSoCoChuSoDauLe(a[i]);
		if(checkchusodaule)
			return a[i];
	}
	return -1;
}

bool KiemTraSoCoDang2Mu(int x)
{
	int i = 0;
	while(true)    // Mặc định chạy vô tận
	{
		if(pow(2, i) == x)    // Chứng tỏ x có dạng 2^k
			return true;
		if(pow(2, i) > x)
			return false;    // ĐK để thoát vòng lặp
		i++;
	}
	return false;
}

void LietKeSoCoDang2Mu(int* a, int n)
{
	for(int i = 0; i < n; i++)
	{
		bool checksodang2mu = KiemTraSoCoDang2Mu(a[i]);
		if(checksodang2mu == true)
			cout<<a[i]<<" ";
	}
}
int TimMax(int* a, int n)
{
	int max = a[0];
	for(int i = 1; i < n; i++)
	{
		if(a[i] > max)
			max = a[i];
	}
	return max;
}

int TimBCNN(int* a, int n)
{
	int max = TimMax(a, n);
	int BCNN = max;

	while(true)
	{
		bool check = true;
		for(int i = 0; i < n; i++)
		{
			if(BCNN % a[i] != 0)
			{
				check = false;
				break;
			}
		}
		if(check == true)
			return BCNN;
		BCNN += max;
	}
}

void LietKeChuSoXuatHienItNhat(int* a, int n)
{
	int demchuso[10] = {0};  // Mảng có công dụng đếm số lần xuất hien cua mỗi chữ số
	for(int i = 0; i < n; i++)
	{
		if(a[i] == 0)
		{
			demchuso[0]++;
		}
		else
		{
			int temp = a[i];
			while(temp != 0)
			{
				int chuso = temp % 10;
				demchuso[chuso]++;
				temp /= 10;
			}
		}
	}
	int min = 0;
	for(int i = 0; i < 10; i++)
	{
		if(demchuso[i] != 0)
		{
			if(min == 0)
				min = demchuso[i];
			else
			{
				if(demchuso[i] < min)
				{
					min = demchuso[i];
				}
			}
		}
	}
	cout << "Nhung chu so xuat hien it nhat trong mang la: ";
	for(int i = 0; i < 10; i++)
	{
		if(demchuso[i] == min)
			cout << i <<" ";
	}
}
int TongPhanTuChuSoDauLe(int* a, int n)
{
	int tong = 0;
	for(int i = 0; i < n; i++)
	{
		bool checkchusodaule = KiemTraSoCoChuSoDauLe(a[i]);
		if(checkchusodaule)
			tong += a[i];
	}
	return tong;
}
bool KiemTraNT(int x)
{
	if(x < 2)
	{
		return false;
	}
	if(x > 2)
	{
		if(x % 2 == 0)
			return false;
		else 
		{
			for(int i = 3; i <= (int) sqrt(x); i += 2)
			{
				if(x % 2 == 0)
					return false;
			}
		}
	}
	return true;
}
int TongNguyenToTrongMang(int* a, int n)
{
	int tong = 0;
	for(int i = 0; i < n; i++)
	{
		if(KiemTraNT(a[i]))
			tong += a[i];
	}
	return tong;
}
int DemNguyenTo(int* a, int n)
{
	int dem = 0;
	for(int i = 0; i < n; i++)
	{
		if(KiemTraNT(a[i]))
			dem++;
	}
	return dem;
}

void XoaPhanTu(int*& a, int& n, int vitrixoa)
{
	for(int i = vitrixoa; i < n - 1; i++)
	{
		a[i] = a[i+1];
	}
	n--;
	a = (int*)realloc(a, n * sizeof(int));
}

void XoaPhanTuTrungGiuLai1(int a[], int& n)
{
	for(int i = 0; i < n - 1; i++)
	{
		for(int j = i  + 1; j < n; j++)      // Duyệt kiểm tra những đứa đứng sau xem có đứa nào trùng với
			// a[i] không?
		{
			if(a[j] == a[i])
			{
				XoaPhanTu(a, n, j);
				j--;
			}
		}
	}
}

void ThemPhanTu(int*& a,int& n, int vitrithem, int giatrithem)
{
	n++;
	a = (int*)realloc(a, n * sizeof(int));
	for(int i = n - 1; i > vitrithem; i--)
	{
		a[i] = a[i - 1];
	}
	a[vitrithem] = giatrithem;
}

void Them0VaoSauChanDuong(int*& a, int& n)
{
	for(int i = 0; i < n; i++)
	{
		if(a[i] % 2 == 0 && a[i] > 0)
		{
			ThemPhanTu(a, n, i + 1, 0);
			i++;
		}
	}
}

int TimMinLe(int* a, int n)
{
	int min = 0;
	for(int i = 0; i < n; i++)
	{
		if(a[i] % 2 != 0)
		{
			min = a[i];
			break;
		}
	}
	if(min == 0)
		return min;
	for(int i = 0; i < n; i++)
	{
		if(a[i] < min && a[i] % 2 != 0)
		{
			min = a[i];
		}
	}
	return min;
}
void HoanVi(int* x, int* y)
{
	int temp = *x;
	*x = *y;
	*y = temp;
}
void SapXepGiamDan(int* a, int n)
{
	for(int i = 0; i < n - 1; i++)
	{
		for(int j = i + 1; j < n; j++)
		{
			if(a[i] < a[j])
			{
				HoanVi(&a[i], &a[j]);
			}
		}
	}
}
// Hàm huyền thoại 
bool KiemTraTrung(int* a, int n, int index)
{
	for(int i = 0; i < index; i++)
	{
		if(a[i] == a[index])
			return true;         // Bị trùng số đằng trước
	}
	return false;
}
int DemSoLanXuatHienCuax(int* a, int n, int x)
{
	int dem = 0;
	for(int i = 0; i < n; i++)
	{
		if(a[i] == x)
			dem++;
	}
	return dem;
}
void LietKeTanXuat(int* a, int n)
{
	SapXepGiamDan(a, n);
	printf("\n");
	XuatMang(a, n);
	for(int i = 0; i < n; i++)
	{
		if(KiemTraTrung(a, n, i))
			continue;
		else 
		{
			int dem = DemSoLanXuatHienCuax(a, n, a[i]);
			printf("%d xuat hien %d lan\n",a[i],dem);
		}
	}
}

bool KiemTraMangDoiXung(int* a, int n)
{
	for(int i = 0; i < n / 2; i++)
	{
		if(a[i] != a[n - 1 - i])
			return false;
	}
	return true;
}
int main()
{
	int* a = NULL;
	int n = 0;
	int chon;
	system("color 1f");
	do 
	{
		system("cls");
		cout<<"0. Thoat\n\n";
		cout<<"1. Nhap Mang\t\t\t\t";
		cout<<"2. Xuat Mang\n\n";
		cout<<"3. Tim So Doi Xung Dau Tien\t\t";
		cout<<"4. Tim So Co Chu So Dau Le Dau Tien\n\n";
		cout<<"5. Liet Ke Cac So Co Dang 2^k\t\t";
		cout<<"6. Tim So Chan Lon Nhat Nho < ALL So Le\n\n";
		cout<<"7. Tim BCNN Cua Cac Phan Tu\t\t";
		cout<<"8. Liet Ke Chu So Xuat Hien It Nhat\n\n";
		cout<<"9. Tong Phan Tu Co Chu So Dau Le\t";
		cout<<"10. Tinh Trung Binh Cong Cac So NT\n\n";
		cout<<"11. Xoa Trung Chi Giu Lai 1\t\t";
		cout<<"12. Them 0 Vao Sau Phan Tu Chan Duong\n\n";
		cout<<"Moi ban chon menu:\n";
		cin>>chon;
		switch (chon)
		{
		case 0:break;
		case 1:
			{
				do 
				{
					cout<<"Nhap so luong phan tu:\n";
					cin>>n;
					if(n < 0)
						cout<<"So luong khong hop le. Nhap lai\n";
				}while(n < 0);
				// Cấp phát bộ nhớ cho con trỏ
				a = new int[n];
				NhapMang(a, n);
				break;
			}
		case 2:XuatMang(a, n); system("pause"); break;
		case 3: break;
		case 4: break;
		case 5: break;
		case 6: break;
		case 7: break;
		case 8: break;
		case 9: break;
		case 10: break;
		case 11: break;
		case 12: break;
		default:cout<<"Chon menu sai!"; system("pause");break;
		}
	}while(chon != 0);
	// Thu hồi những ô nhớ ở vùng HEAP con trỏ a đang tham chiếu đến
	if(a != NULL)
		delete []a;
	system("pause");
	return 0;
}


Output:
Line 18: error: iostream: No such file or directory
Line 15: error: ctime: No such file or directory
Line 15: error: cmath: No such file or directory
Line 4: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace'
In function 'TaoMangNgauNhien':
Line 9: error: 'for' loop initial declaration used outside C99 mode
In function 'NhapMang':
Line 18: error: 'for' loop initial declaration used outside C99 mode
In function 'XuatMang':
Line 27: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 33: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraDoiXung'
In function 'TimSoDoiXungDauTien':
Line 49: error: 'for' loop initial declaration used outside C99 mode
Line 51: error: 'bool' undeclared (first use in this function)
Line 51: error: (Each undeclared identifier is reported only once
Line 51: error: for each function it appears in.)
Line 51: error: expected ';' before 'checkdoixung'
Line 52: error: 'checkdoixung' undeclared (first use in this function)
Line 52: error: 'true' undeclared (first use in this function)
t.c: At top level:
Line 58: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraSoCoChuSoDauLe'
In function 'TimSoCoChuSoDauTienLe':
Line 72: error: 'for' loop initial declaration used outside C99 mode
Line 74: error: 'bool' undeclared (first use in this function)
Line 74: error: expected ';' before 'checkchusodaule'
Line 75: error: 'checkchusodaule' undeclared (first use in this function)
t.c: At top level:
Line 81: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraSoCoDang2Mu'
In function 'LietKeSoCoDang2Mu':
Line 97: error: 'for' loop initial declaration used outside C99 mode
Line 99: error: 'bool' undeclared (first use in this function)
Line 99: error: expected ';' before 'checksodang2mu'
Line 100: error: 'checksodang2mu' undeclared (first use in this function)
Line 100: error: 'true' undeclared (first use in this function)
Line 101: error: 'cout' undeclared (first use in this function)
In function 'TimMax':
Line 107: error: 'for' loop initial declaration used outside C99 mode
In function 'TimBCNN':
Line 120: error: 'true' undeclared (first use in this function)
Line 122: error: 'bool' undeclared (first use in this function)
Line 122: error: expected ';' before 'check'
Line 123: error: 'for' loop initial declaration used outside C99 mode
Line 127: error: 'check' undeclared (first use in this function)
Line 127: error: 'false' undeclared (first use in this function)
In function 'LietKeChuSoXuatHienItNhat':
Line 140: error: 'for' loop initial declaration used outside C99 mode
Line 158: error: redefinition of 'i'
Line 140: error: previous definition of 'i' was here
Line 158: error: 'for' loop initial declaration used outside C99 mode
Line 173: error: 'cout' undeclared (first use in this function)
Line 174: error: redefinition of 'i'
Line 158: error: previous definition of 'i' was here
Line 174: error: 'for' loop initial declaration used outside C99 mode
In function 'TongPhanTuChuSoDauLe':
Line 183: error: 'for' loop initial declaration used outside C99 mode
Line 185: error: 'bool' undeclared (first use in this function)
Line 185: error: expected ';' before 'checkchusodaule'
Line 186: error: 'checkchusodaule' undeclared (first use in this function)
t.c: At top level:
Line 191: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraNT'
In function 'TongNguyenToTrongMang':
Line 215: error: 'for' loop initial declaration used outside C99 mode
In function 'DemNguyenTo':
Line 225: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 233: error: expected ';', ',' or ')' before '&' token
Line 243: error: expected ';', ',' or ')' before '&' token
Line 259: error: expected ';', ',' or ')' before '&' token
Line 270: error: expected ';', ',' or ')' before '&' token
In function 'TimMinLe':
Line 285: error: 'for' loop initial declaration used outside C99 mode
Line 295: error: redefinition of 'i'
Line 285: error: previous definition of 'i' was here
Line 295: error: 'for' loop initial declaration used outside C99 mode
In function 'SapXepGiamDan':
Line 312: error: 'for' loop initial declaration used outside C99 mode
Line 314: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 324: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraTrung'
In function 'DemSoLanXuatHienCuax':
Line 336: error: 'for' loop initial declaration used outside C99 mode
In function 'LietKeTanXuat':
Line 348: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 360: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraMangDoiXung'
In function 'main':
Line 378: error: 'cout' undeclared (first use in this function)
Line 392: error: 'cin' undeclared (first use in this function)
Line 406: error: 'new' undeclared (first use in this function)
Line 406: error: expected ';' before 'int'
Line 426: error: 'delete' undeclared (first use in this function)
Line 426: error: expected expression before ']' token


Create a new paste based on this one


Comments: