+ 1
Solve according to C language
You are given a sorted (either in the increasing or in the decreasing order) sequence of numbers, ending with a -1. You can assume that are at least two numbers before the ending -1. Let us call the sequence x0 x1 ... xn -1. You have to output the number of distinct elements the sorted sequence. Kindly do not use arrays in the code.
2 Respuestas
+ 1
Given the input is sorted, when the current number is equal to the previous number it is not distinct so shouldn't be counted.
0
#include<stdio.h>
int main()
{
int prev,curr,diff=0;
/* at least two numbers are present */
scanf("%d %d",&prev,&curr);
printf("%d", curr-prev);
do {
prev = curr; // shift current to previous
scanf("%d", &curr); // read next : the new current
if (curr == -1) { // we are done
printf("\n"); // print newline and break
break;
}
diff = curr - prev; // compute new sum of pairs
printf(" %d", diff); // print sum with a space
} while(1);
return 0;
}