Created
November 16, 2019 08:56
-
-
Save tinwritescode/3eda81d5def342d070306848e0dc3405 to your computer and use it in GitHub Desktop.
anh_khoa.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
File: anh_khoa.cpp | |
Author: Trung Tin Nguyen | |
Date: 16.11.2019 | |
Course: Unknown | |
Desc/Usage: Cho mảng a, cho x. Tìm ước của x trong mảng đó, xếp xuống cuối mảng. | |
*/ | |
#define DEBUG | |
#include <iostream> | |
#include <cstring> | |
#define MAX_NUMBERS 100 | |
using namespace std; | |
int find(int a[], int n, int x, bool divisible[]) { | |
for(int i = n -1; i >= 0; --i) { | |
if(divisible[i] == false) return i; | |
} | |
return -1; | |
} | |
void swap(int arr[], int a, int b) { | |
if(b == -1) return; | |
arr[a] = arr[a] + arr[b]; | |
arr[b] = arr[a] - arr[b]; | |
arr[a] = arr[a] - arr[b]; | |
} | |
int main() { | |
#ifndef DEBUG | |
int n; // n là số phần tử của mảng | |
int a[MAX_NUMBERS]; | |
cout << "Nhap n: ", cin >> n; | |
for(int i = 0; i < n; i++) { | |
cout << "a[" << i << "] = ", cin >> a[i]; | |
} | |
int x; | |
cout << "Nhap x: ", cin >> x; | |
#else | |
int a[] = {3, 4, 7, 9, 2}; | |
int n = sizeof(a) / sizeof(*a); | |
const int x = 12; | |
#endif | |
// Init divisible array | |
bool divisible[MAX_NUMBERS]; | |
for (int i = 0; i < n; i++) divisible[i] = !(x % a[i]); | |
for(int i = 0; i < n; i++) { | |
if(divisible[i] == true) { | |
swap(a, i, find(a, n, x, divisible)); | |
} | |
} | |
for(int i = 0; i < n; i++) { | |
cout << a[i] << " "; | |
} | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment