Задан одномерный массив целых чисел. найти произведение тех ее элементов, которые делятся на 4 без остатка (алгоритм и pascal-программа)
167
214
Ответы на вопрос:
Var a: array[1..100] of integer; i,p,n: integer; begin writeln('введите количество элементов'); read(n); p: =1; randomize; {случайные числа} for i: =1 to n do {от 1 до n элемента} begin a[i]: =random(100)-50; {случайные числа, можешь указать любые значения} if a[i] mod 4=0 then{если элемент массива делится на 4 с остатком 0} p: =p*a[i]; {умножаем текущее p на этот элемент} end; writeln(p); {выводим произведение} end. в случае,если в массиве будет число 0, оно будет делится на 4 без остатка, поэтому произведение будет 0. если это не нужно, то строку if a[i] mod 4=0 then замени на if (a[i]< > 0) and (a[i] mod 4=0) then тогда 0 учитываться не будет.
12 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 #include< fstream> #include < iostream> #include< vector> #include < stack> #include< string> #include< algorithm> using namespace std; stack< int> st; int error = 0; void operation(string command) { if (command == "drop") { if (st.size() > 0) { st.pop(); return; } else { error = 1; return; } } if (command == "swap") { if (st.size() > = 2) { int a = st.top(); st.pop(); int b = st.top(); st.pop(); st.push(a); st.push(b); return; } else { error = 1; return; } } if (command == "dup") { if (st.size() > 0) { int a = st.top(); st.push(a); return; } else { error = 1; return; } } if (command == "over") { if (st.size() > = 2) { int a = st.top(); st.pop(); int b = st.top(); st.pop(); st.push(b); st.push(a); st.push(b); return; } else { error = 1; return; } } if (command == "+") { if (st.size() > = 2) { int a = st.top(); st.pop(); int b = st.top(); st.pop(); st.push(a + b); } else { error = 1; return; } } if (command == "-") { if (st.size() > = 2) { int a = st.top(); st.pop(); int b = st.top(); st.pop(); st.push(b - a); return; } else { error = 1; return; } } if (command == "*") { if (st.size() > = 2) { int a = st.top(); st.pop(); int b = st.top(); st.pop(); st.push(b*a); return; } else { error = 1; return; } } if (command == "/") { if (st.size() > = 2) { int a = st.top(); st.pop(); int b = st.top(); st.pop(); st.push(b/a); return; } else { error = 1; return; } } } bool isnumber(string x) { for (int i = 0; i < x.length(); i++) { if (x[i] > '9' || x[i] < '0') { return false; } } return true; } int main() { vector< int> v; ifstream in; ofstream out; in.open("input.txt"); out.open("output.txt"); string command; while (in > > command& & error ! = 1) { if (isnumber(command) == true) { st.push(atoi(command.; continue; } operation(command); } if (error == 1) { out < < "error"; return 0; } if (st.size() == 0) { out < < "empty"; return 0; } while (st.size() ! = 0) { v.push_back(st.; st.pop(); } reverse(v. v.; for (int i = 0; i < v.size(); i++) { out < < v[i] < < " "; } in.close(); out.close(); }
Популярно: Информатика
-
borshliba27.11.2022 13:27
-
кент6317.11.2020 22:57
-
IUcenhik15.06.2020 15:33
-
irisha19078412.02.2022 17:54
-
vasifalieyev00125.05.2023 03:46
-
kirikdementev08.10.2021 20:34
-
qsn5085721.11.2020 11:02
-
Dashaass55414.10.2021 18:48
-
irinamiller9306.05.2020 16:48
-
rsharapov01.04.2020 07:51