Предмет: Информатика,
автор: omuhametali
For a positive integer n, we define a function f:
f (n) = - 1 + 2 - 3 + .. + (- 1) nn
Your task is to calculate f (n) for a given integer n.
Input data
The only line contains a positive integer n (1 ≤ n ≤ 1015).
Output
Output f (n) in a single line.
with c++
Ответы
Автор ответа:
0
#include <iostream>
using namespace std;
int main()
{
int n = 0, f = 0, t = 0;
cin >> n;
if (n <= 1015 && n >= 1) {
for (int i = 1; i <= n; ++i) {
t = -i;
if (i % 2 == 0) {
t = abs(t);
}
cout << t << " ";
f += t;
}
} else {
cout << "Error" << endl;
}
cout << endl << "Res: " << f;
return 0;
}
using namespace std;
int main()
{
int n = 0, f = 0, t = 0;
cin >> n;
if (n <= 1015 && n >= 1) {
for (int i = 1; i <= n; ++i) {
t = -i;
if (i % 2 == 0) {
t = abs(t);
}
cout << t << " ";
f += t;
}
} else {
cout << "Error" << endl;
}
cout << endl << "Res: " << f;
return 0;
}
Похожие вопросы
Предмет: Физика,
автор: jaroslavpurin
Предмет: Биология,
автор: aabdulaziz36
Предмет: Қазақ тiлi,
автор: Nurasylap
Предмет: Математика,
автор: даша25102003
Предмет: Математика,
автор: semchenkova3019