mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-16 15:18:37 +08:00
Add ru version (#1865)
* Add Russian docs site baseline * Add Russian localized codebase * Polish Russian code wording * Update ru code translation. * Update code translation and chapter covers. * Fix pythontutor extraction. * Add README and landing page. * placeholder of profiles * Use figures of English version * Remove chapter paperbook
This commit is contained in:
76
ru/codes/cpp/chapter_computational_complexity/iteration.cpp
Normal file
76
ru/codes/cpp/chapter_computational_complexity/iteration.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* File: iteration.cpp
|
||||
* Created Time: 2023-08-24
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Цикл for */
|
||||
int forLoop(int n) {
|
||||
int res = 0;
|
||||
// Циклическое суммирование 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Цикл while */
|
||||
int whileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // Инициализация условной переменной
|
||||
// Циклическое суммирование 1, 2, ..., n-1, n
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
i++; // Обновить условную переменную
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Цикл while (двойное обновление) */
|
||||
int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // Инициализация условной переменной
|
||||
// Циклическое суммирование 1, 4, 10, ...
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// Обновить условную переменную
|
||||
i++;
|
||||
i *= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Двойной цикл for */
|
||||
string nestedForLoop(int n) {
|
||||
ostringstream res;
|
||||
// Цикл по i = 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
// Цикл по j = 1, 2, ..., n-1, n
|
||||
for (int j = 1; j <= n; ++j) {
|
||||
res << "(" << i << ", " << j << "), ";
|
||||
}
|
||||
}
|
||||
return res.str();
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
cout << "\nРезультат суммирования в цикле for res = " << res << endl;
|
||||
|
||||
res = whileLoop(n);
|
||||
cout << "\nРезультат суммирования в цикле while res = " << res << endl;
|
||||
|
||||
res = whileLoopII(n);
|
||||
cout << "\nРезультат суммирования в цикле while (двойное обновление) res = " << res << endl;
|
||||
|
||||
string resStr = nestedForLoop(n);
|
||||
cout << "\nРезультат двойного цикла for " << resStr << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user