Algorithm
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1101
Sequence of Numbers and Sum
Adapted by Neilor Tonin, URI Brazil
Read an undetermined number of pairs values M and N (stop when any of these values is less or equal to zero). For each pair, print the sequence from the smallest to the biggest (including both) and the sum of consecutive integers between them (including both).
Input
The input file contains pairs of integer values M and N. The last line of the file contains a number zero or negative, or both.
Output
For each pair of numbers, print the sequence from the smallest to the biggest and the sum of these values, as shown below.
Input Sample | Output Sample |
5 2 |
2 3 4 5 Sum=14 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<stdio.h>
int main()
{
int a,b,i,j,t,sum=0,temp;
for(j = 0; j < 100; j++)
{
scanf("%d %d",&a,&b);
if(a <= 0 || b < = 0>
break;
if(a>b)
{
temp=a;
a=b;
b=temp;
}
for(i = a; i <= b; i++)
{
printf("%d ",i);
sum=sum+i;
}
printf("Sum=%d\n",sum>;
sum=0;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("fs")
const inputs = readFileSync("/dev/stdin", "utf8").split("\n")
const inputPairs = inputs.map((inputPair) => inputPair.split(" ").map((num) => Number.parseInt(num))
)
function orderLimits([firstLimit, secondLimit = firstLimit]) {
const inferior = Math.min(firstLimit, secondLimit)
const superior = Math.max(firstLimit, secondLimit)
return [inferior, superior]
}
function sumsArray(arr = [0], initialValue = 0) {
return arr.reduce((acc, cur) => acc + cur, initialValue)
}
function makeIntSequence(minLimit, maxLimit) {
return Array.from(
{ length: maxLimit - minLimit + 1 },
(_, i) => minLimit + i
)
}
function main() {
const responses = []
for (const [f, s] of inputPairs) {
if (f <= 0 || s <= 0) break
const ordenedLimits = orderLimits([f, s])
const sequence = makeIntSequence(...ordenedLimits)
const sum = sumsArray(sequence)
responses.push(`${sequence.join(" ")} Sum=${sum}`)
}
console.log(responses.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
m = []
a = soma = 0
total = []
sum = []
while True:
pri, seg = input().split(' ')
pri = int(pri); seg = int(seg)
if pri < = 0 or seg <= 0:
break
m+=[pri, seg]
total+=[m[:]]
m.clear()
a+=1
for i in range(0, a):
if total[i][0] < total[i][1]:
for k in range(total[i][0],total[i][1]+1):
print(k,end=' ')
soma+=k
if total[i][1] < total[i][0]:
for p in range(total[i][1],total[i][0]+1):
print(p, end=' ')
soma+=p
sum+=[soma]
print(f'Sum={sum[0]}')
soma = 0; sum = []
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Java Programming
Code -
Java Programming
import java.io.IOException;
import java.util.Scanner;
public class Main{
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
while (true) {
int m = input.nextInt();
int n = input.nextInt();
if (m == 0 || m < 0 || n == 0 || n < 0){
break;
}
if (m < n){
int temp = m;
m = n;
n = temp;
}
int sum = 0;
for (int i = n; i < = m ; i ++){
sum += i;
System.out.printf("%d ", i);
}
System.out.printf("Sum=%d\n", sum);
}
}
}
Copy The Code &
Try With Live Editor
Input
Output