11,489
社区成员
![](https://csdnimg.cn/release/cmsfe/public/img/topic.427195d5.png)
![](https://csdnimg.cn/release/cmsfe/public/img/me.40a70ab0.png)
![](https://csdnimg.cn/release/cmsfe/public/img/task.87b52881.png)
![](https://csdnimg.cn/release/cmsfe/public/img/share-circle.3e0b7822.png)
import java.util.Scanner;
public class DengCha {
public static void main(String args[]) {
System.out.println("请输入三个数字,由系统判断是否构成等差数列");
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int sum = a + b + c;
in.close();
if ((a - c) % b != 0) {
System.out.print("-1");
} else {
System.out.print(sum);
}
}
}
# Python
```python
def calculate_arithmetic_series_sum(first, last, common_diff):
n = (last - first) // common_diff + 1
if (last - first) % common_diff == 0:
series_sum = n * (first + last) // 2
return series_sum
else:
return -1
输入首项、末项和公差
first_term, last_term, common_difference = map(int, input("输入三个整数,以空格分隔:").split())
计算等差数列的和
result = calculate_arithmetic_series_sum(first_term, last_term, common_difference)
输出结果
print(result)
```
C语言实现
#include<stdio.h>
int fun(int head,int end,int tol){
int h1,e1,t1;
int i,n;
int sum=0;
int all;
h1=head,e1=end,t1=tol;
for(i=0;sum!=end;i++){
if(sum<=end){
sum=h1+i*tol;
}
if(sum>end){
// retrun -1;;
break;
}
}
n=i;
all=h1+(n-1)*tol;
if(all!=end){
all=-1;
}
return all;
}
int main(){
int on;
int h,e,t;
printf("输入三个数,第一位是首项A1,第二位是末项An,最后是公差d:\n");
scanf("%d %d %d",&h,&e,&t);
on=fun(h,e,t);
printf("%d",on);
}
C语言代码
int main()
{
int a = 0;
int c = 0;
int b = 0;
scanf("%d %d %d", &a, &c, &b );
if ((a - c) % b != 0)
{
printf("-1");
}
else
{
int n = (c - a) / b + 1;
int s = n * (a + c) / 2;
printf("%d\n", s);
}
return 0;
}
Python代码
# 方法一:直接套公式(生成浮点类型数字)
a1 = int(input())
an = int(input())
d = int(input())
s = (a1 + an) / 2 * ((an - a1) / d + 1)
print(s)
# ※推荐 方法二:生成等差数列直接用函数求和(是整型等差数列就输出整型,是浮点型等差数列就输出浮点型)
a1 = int(input())
an = int(input())
d = int(input())
ls = []
fill = a1
while (fill <= an):
ls.append(fill)
fill += d
print(sum(ls))
C++代码
// 方法一(只能保留一位小数)
#include <iostream>
using namespace std;
int main()
{
int a1, an, d;
cin >> a1 >> an >> d;
double s = (a1 + an) / 2.0 * ((an - a1) / d + 1);
cout << s;
return 0;
}
// ※推荐 方法二
#include <iostream>
using namespace std;
int main()
{
int a1, an, d;
cin >> a1 >> an >> d;
int arr[105] = {};
int fill = a1;
int index = 0;
while (fill <= an)
{
arr[index] = fill;
fill += d;
index++;
}
int sum = 0;
for (int i = 0; i < (an - a1) / d + 1; i++)
{
sum += arr[i];
}
cout << sum;
return 0;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int d = scanner.nextInt();
// 判断是否能构成等差数列
if ((b - a) % d != 0) {
System.out.println(-1);
return;
}
// 计算数列项数
int n = (b - a) / d + 1;
// 计算数列和
int sum = (a + b) * n / 2;
System.out.println(sum);
}
a,b,c =map(int,input().split())
if (b-a)%c == 0:
print(((b**2-a**2)/c+a+b)/2)
else:
print(-1)
a, b, c = map(int, input().split())
d = (b-a) / c + 1
e = (b-a) % c
if e != 0:
print(-1)
else:
f = d * (a+b) / 2
print(int(f))
```python
# 读取输入
input_str = input("请输入三个整数,用空格隔开:")
input_list = input_str.split()
if len(input_list) != 3:
print(-1)
else:
a, b, d = map(int, input_list)
# 计算项数n
n = (b - a) // d + 1
# 判断是否能构成等差数列
if n <= 0:
print(-1)
else:
# 计算等差数列的和
Sn = n * (2 * a + (n - 1) * d) // 2
print(Sn)
```
#include <stdio.h>
#include <stdlib.h>
void solution(int arr[3]){
// TODO: 请在此编写代码
int x = arr[0],sub = arr[0];
while (x < arr[1]){
x += arr[2];
sub += x;
}
if (x == arr[1]){
printf("%d",sub);
}
else{
printf("%d",-1);
}
}
int main() {
int arr[3];
for (int i = 0; i < 3; i++)
{
scanf("%d", &arr[i]);
}
solution(arr);
return 0;
}
let a = prompt("a的值为1-9,请输入a的值:") - 0;
let b = prompt("请输入b的值:") - 0;
let c = prompt("请输入c的值:") - 0;
// 定义空变量来存放等差数列的求和公式
let sum;
// 定义等差数列的和
let sumnum=0;
// 设立判断等差数列的条件
if (a == b % c) {
for (let i = a; i <= b; i++) {
// 通过sum变量来得出等差数列的求和公式间接的去求出需要经过几次循环(i)得出b;
sum = a + i * c;
// 设立判断条件当sum变量与b值相等时则间接求出i
if(sum==b){
// console.log(i);
// 得到i值后再次以i为循环次数循环
for (var j = 0; j <= i; j++) {
sum=a+j*c;
// 将每次循环得到的等差数列存入sumnum变量中
sumnum+=sum;
}
console.log(sumnum);
}
}
} else {
console.log(-1);
}
# 定义一个函数,用于求解等差数列的和
def solution(a, l, b):
# 判断所给三个整数是否能构成等差数列
if (l - a) % b != 0:
# 如果不能,返回-1
return -1
else:
# 如果能,计算项数n
n = (l - a) // b + 1
# 计算等差数列的和,利用公式Sn = n * (a + l) / 2
s = n * (a + l) / 2
# 返回等差数列的和
return s
# 输入三个整数,用空格分隔,并转换为列表
a, l, b = map(int, input().split())
# 调用函数,求解等差数列的和,并输出结果
print(solution(a, l, b))