Практикум по программированию. Основы. Массивы. Максимальное число
From AsIsWiki
(Difference between revisions)
Line 9: | Line 9: | ||
<pre> | <pre> | ||
− | |||
− | |||
− | |||
public class Task07 { | public class Task07 { | ||
Line 17: | Line 14: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
− | |||
− | |||
System.out.println(); | System.out.println(); | ||
Line 24: | Line 19: | ||
System.out.println(" и подсчет количества таких чисел "); | System.out.println(" и подсчет количества таких чисел "); | ||
System.out.println("--------------------------------------"); | System.out.println("--------------------------------------"); | ||
− | |||
System.out.print(" Массив:"); | System.out.print(" Массив:"); | ||
Line 55: | Line 49: | ||
<pre> | <pre> | ||
+ | // g++ 4.2 | ||
+ | |||
+ | #include <iostream> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main() { | ||
+ | |||
+ | cout << "\n Поиск максимального числа в массиве,\n"; | ||
+ | cout << " и подсчет количества таких чисел\n"; | ||
+ | cout << "--------------------------------------\n"; | ||
+ | cout << " Массив:"; | ||
+ | |||
+ | srand((int)time(0)); | ||
+ | |||
+ | const int n = 10; | ||
+ | int a[n], k = 0, max = 0; | ||
+ | |||
+ | for (int i = 0; i < n; i++) { | ||
+ | a[i] = rand() % 10; | ||
+ | printf("%3d", a[i]); | ||
+ | if (a[i] > max) { | ||
+ | max = a[i]; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | cout << "\n--------------------------------------\n"; | ||
+ | |||
+ | for (int i = 0; i < n; i++) { | ||
+ | if (a[i] == max) { | ||
+ | k++; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | printf(" Максимальное число %d встречается %d раз(а)\n\n", max, k); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
</pre> | </pre> | ||
Latest revision as of 11:08, 19 April 2015
Contents |
[edit] Java
public class Task07 { private static final int n = 10; public static void main(String[] args) { System.out.println(); System.out.println(" Поиск максимального числа в массиве, "); System.out.println(" и подсчет количества таких чисел "); System.out.println("--------------------------------------"); System.out.print(" Массив:"); int a[] = new int[n]; int k = 0, max = 0; for (int i = 0; i < n; i++) { a[i] = (int) (10 * Math.random()); System.out.printf("%3d", a[i]); if (a[i] > max) { max = a[i]; } } System.out.println("\n--------------------------------------"); for (int i = 0; i < n; i++) { if (a[i] == max) { k++; } } System.out.printf(" Максимальное число %d встречается %d раз(а)\n", max, k); } }
[edit] C++
// g++ 4.2 #include <iostream> using namespace std; int main() { cout << "\n Поиск максимального числа в массиве,\n"; cout << " и подсчет количества таких чисел\n"; cout << "--------------------------------------\n"; cout << " Массив:"; srand((int)time(0)); const int n = 10; int a[n], k = 0, max = 0; for (int i = 0; i < n; i++) { a[i] = rand() % 10; printf("%3d", a[i]); if (a[i] > max) { max = a[i]; } } cout << "\n--------------------------------------\n"; for (int i = 0; i < n; i++) { if (a[i] == max) { k++; } } printf(" Максимальное число %d встречается %d раз(а)\n\n", max, k); return 0; }
[edit] Pascal