|
|
вернуться в форумПоказать все сообщения Спрятать все сообщения#include<stdio.h> int main(){ int n,sum=0,i; scanf("%d",&n);- if(n>0){ sum=(n*(n+1))/2; } else if(n<0){ sum=(n*(-1*n+1))/2; } printf("%d",sum); } Maybe for N < 0 the formula is SUM = ((2-N)*(N+1))/2 Edited by author 06.11.2017 19:47 The first term is N The last term is 1 So the average is (1+N)/2 There are (-N) terms below zero And there are two more terms They are 0 and one So there are (2-N) total terms So the sum is average by total number of terms But that should not make a difference Case "N == 0" isn't processed Edited by author 20.05.2020 08:52 Edited by author 20.05.2020 08:52 |
|
|