提交 dba179f3 编写于 作者: 依然范特西S's avatar 依然范特西S

dome02:SCDN每日一练:等差数列求和

上级 2dfe6e08
package dome;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author liuyang01
* @date 2022-09-26
* 类描述:
* 修改记录:
*/
//SCDN每日一练:等差数列求和
/*输入三个整数,之间用空格隔开。
*第1个数作为首项,
*第2个数作为末项,
*第3个数作为公差
* **/
public class demo02 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str_0 = scan.nextLine();
String[] line_list_0 = str_0.trim().split(" ");
ArrayList<Integer> arr = new ArrayList<>();
for(int i = 0; i < line_list_0.length; i++){
arr.add(Integer.parseInt(line_list_0[i]));
}
scan.close();
int result = solution(arr);
System.out.println(result);
}
public static int solution(ArrayList<Integer> arr){
int result = 0;
// TODO: 请在此编写代码
int a = arr.get(0);
int b = arr.get(1);
int c = arr.get(2);
if((b-a)%c == 0){
int s = (b-a)/c + 1;
result = (a+b)*s/2;
return result;
}
return -1;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册