R soni va n o`lchamli massiv berilgan. Yig`idisi R
Download 346.47 Kb.
|
1-amaliy
Masala:
Masala kodi: #include #include using namespace std; int main() { int n; cout << "Massiv uzunligini kiriting: "; cin >> n; int arr[n]; cout << "Massiv elementlarini kiriting: "; for (int i = 0; i < n; i++) { cin >> arr[i]; } int closest_sum = INT_MAX; int closest_i, closest_j; cout << "Yig'indini topish uchun ikkita elementni kiriting: "; int target; cin >> target; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int sum = arr[i] + arr[j]; if (abs(target - sum) < abs(target - closest_sum)) { closest_sum = sum; closest_i = i; closest_j = j; } } } cout << "Yaqin yig'indilar: " << arr[closest_i] << " va " << arr[closest_j] << endl; return 0; } Natija: Download 346.47 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling