给定一个整数N ,任务是找到具有备用符号(即1 – 2 + 3 – 4 + 5 – 6 +…)的前N个自然数的总和。
例子:
Input: N = 6
Output: -3
Explanation:
1 – 2 + 3 – 4 + 5 – 6 = -3
Therefore, the required output is -3.
Input: N = 5
Output: 3
Explanation:
1 – 2 + 3 – 4 + 5 = 3
Therefore, the required output = 3
天真的方法:请按照以下步骤解决问题:
- 初始化一个变量,例如alternateSum,以存储前N个自然数的替代符号之和。
- 使用变量i遍历[1,N]范围,并检查i是否为偶数。如果发现是真的,则更新alterSum + = -i 。
- 否则,更新alterSum + = i 。
- 最后,打印alternateSum的价值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the sum of first
// N natural numbers with alternate signs
int alternatingSumOfFirst_N(int N)
{
// Stores sum of alternate sign
// of first N natural numbers
int alternateSum = 0;
for (int i = 1; i <= N; i++) {
// If is an even number
if (i % 2 == 0) {
// Update alternateSum
alternateSum += -i;
}
// If i is an odd number
else {
// Update alternateSum
alternateSum += i;
}
}
return alternateSum;
}
// Driver Code
int main()
{
int N = 6;
cout<
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find the sum of first
// N natural numbers with alternate signs
static int alternatingSumOfFirst_N(int N)
{
// Stores sum of alternate sign
// of first N natural numbers
int alternateSum = 0;
for(int i = 1; i <= N; i++)
{
// If is an even number
if (i % 2 == 0)
{
// Update alternateSum
alternateSum += -i;
}
// If i is an odd number
else
{
// Update alternateSum
alternateSum += i;
}
}
return alternateSum;
}
// Driver Code
public static void main(String[] args)
{
int N = 6;
System.out.print(alternatingSumOfFirst_N(N));
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to implement
# the above approach
# Function to find the sum of
# First N natural numbers with
# alternate signs
def alternatingSumOfFirst_N(N):
# Stores sum of alternate sign
# of First N natural numbers
alternateSum = 0
for i in range(1, N + 1):
# If is an even number
if (i % 2 == 0):
# Update alternateSum
alternateSum += -i
# If i is an odd number
else:
alternateSum += i
return alternateSum
# Driver Code
if __name__ == "__main__" :
N = 6
print(alternatingSumOfFirst_N(N))
# This code is contributed by Virusbuddah_
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find the sum of first
// N natural numbers with alternate signs
static int alternatingSumOfFirst_N(int N)
{
// Stores sum of alternate sign
// of first N natural numbers
int alternateSum = 0;
for(int i = 1; i <= N; i++)
{
// If is an even number
if (i % 2 == 0)
{
// Update alternateSum
alternateSum += -i;
}
// If i is an odd number
else
{
// Update alternateSum
alternateSum += i;
}
}
return alternateSum;
}
// Driver Code
public static void Main(String[] args)
{
int N = 6;
Console.Write(alternatingSumOfFirst_N(N));
}
}
// This code is contributed by 29AjayKumar
Javascript
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the sum of first
// N natural numbers with alternate signs
int alternatingSumOfFirst_N(int N)
{
// Stores sum of alternate sign
// of first N natural numbers
int alternateSum = 0;
// If N is an even number
if (N % 2 == 0) {
// Update alternateSum
alternateSum = (-N) / 2;
}
// If N is an odd number
else {
// Update alternateSum
alternateSum = (N + 1) / 2;
}
return alternateSum;
}
// Driver Code
int main()
{
int N = 6;
cout<
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to find the sum of first
// N natural numbers with alternate signs
static int alternatingSumOfFirst_N(int N)
{
// Stores sum of alternate sign
// of first N natural numbers
int alternateSum = 0;
// If N is an even number
if (N % 2 == 0)
{
// Update alternateSum
alternateSum = (-N) / 2;
}
// If N is an odd number
else
{
// Update alternateSum
alternateSum = (N + 1) / 2;
}
return alternateSum;
}
// Driver Code
public static void main(String[] args)
{
int N = 6;
System.out.print(alternatingSumOfFirst_N(N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program to implement
# the above approach
# Function to find the sum of first
# N natural numbers with alternate signs
def alternatingSumOfFirst_N(N):
# Stores sum of alternate sign
# of first N natural numbers
alternateSum = 0;
# If N is an even number
if (N % 2 == 0):
# Update alternateSum
alternateSum = (-N) // 2;
# If N is an odd number
else:
# Update alternateSum
alternateSum = (N + 1) // 2;
return alternateSum;
# Driver Code
if __name__ == '__main__':
N = 6;
print(alternatingSumOfFirst_N(N));
# This code contributed by shikhasingrajput
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to find the sum of first
// N natural numbers with alternate signs
static int alternatingSumOfFirst_N(int N)
{
// Stores sum of alternate sign
// of first N natural numbers
int alternateSum = 0;
// If N is an even number
if (N % 2 == 0)
{
// Update alternateSum
alternateSum = (-N) / 2;
}
// If N is an odd number
else
{
// Update alternateSum
alternateSum = (N + 1) / 2;
}
return alternateSum;
}
// Driver Code
public static void Main(String[] args)
{
int N = 6;
Console.Write(alternatingSumOfFirst_N(N));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
-3
时间复杂度: O(N)
辅助空间: O(1)
高效方法:为了优化上述方法,该思想基于以下观察结果:
If N is an even number then the sum of alternate sign of first N natural numbers are = (-N) / 2.
If N is an odd number then the sum of alternate sign of first N natural numbers are = (N + 1) / 2.
请按照以下步骤解决问题:
- 初始化一个变量,例如alternateSum,以存储前N个自然数的替代符号之和。
- 检查N是否为偶数。如果发现为真,则更新alterSum =(-N)/ 2 。
- 否则,更新alterSum =(N + 1)/ 2 。
- 最后,打印alternateSum的价值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the sum of first
// N natural numbers with alternate signs
int alternatingSumOfFirst_N(int N)
{
// Stores sum of alternate sign
// of first N natural numbers
int alternateSum = 0;
// If N is an even number
if (N % 2 == 0) {
// Update alternateSum
alternateSum = (-N) / 2;
}
// If N is an odd number
else {
// Update alternateSum
alternateSum = (N + 1) / 2;
}
return alternateSum;
}
// Driver Code
int main()
{
int N = 6;
cout<
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to find the sum of first
// N natural numbers with alternate signs
static int alternatingSumOfFirst_N(int N)
{
// Stores sum of alternate sign
// of first N natural numbers
int alternateSum = 0;
// If N is an even number
if (N % 2 == 0)
{
// Update alternateSum
alternateSum = (-N) / 2;
}
// If N is an odd number
else
{
// Update alternateSum
alternateSum = (N + 1) / 2;
}
return alternateSum;
}
// Driver Code
public static void main(String[] args)
{
int N = 6;
System.out.print(alternatingSumOfFirst_N(N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program to implement
# the above approach
# Function to find the sum of first
# N natural numbers with alternate signs
def alternatingSumOfFirst_N(N):
# Stores sum of alternate sign
# of first N natural numbers
alternateSum = 0;
# If N is an even number
if (N % 2 == 0):
# Update alternateSum
alternateSum = (-N) // 2;
# If N is an odd number
else:
# Update alternateSum
alternateSum = (N + 1) // 2;
return alternateSum;
# Driver Code
if __name__ == '__main__':
N = 6;
print(alternatingSumOfFirst_N(N));
# This code contributed by shikhasingrajput
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to find the sum of first
// N natural numbers with alternate signs
static int alternatingSumOfFirst_N(int N)
{
// Stores sum of alternate sign
// of first N natural numbers
int alternateSum = 0;
// If N is an even number
if (N % 2 == 0)
{
// Update alternateSum
alternateSum = (-N) / 2;
}
// If N is an odd number
else
{
// Update alternateSum
alternateSum = (N + 1) / 2;
}
return alternateSum;
}
// Driver Code
public static void Main(String[] args)
{
int N = 6;
Console.Write(alternatingSumOfFirst_N(N));
}
}
// This code is contributed by 29AjayKumar
Java脚本
输出:
-3
时间复杂度: O(1)
辅助空间: O(1)