URI Online Judge Solution 1180 Lowest Number and Position Using C, Python Programming Language.
Write a program that reads a number N. This N is the size of a array X[N]. Next, read each of the numbers of X, find the smallest element of this array and its position within the array, printing this information.
Input
The first line of input contains one integer N (1 < N <1000), indicating the number of elements that should be read to an array X[N] of integer numbers. The second row contains each of the N values, separated by a space.
Output
The first line displays the message “Menor valor:” followed by a space and the lowest number read in the input. The second line displays the message “Posicao:” followed by a space and the array position in which the lowest number is, remembering that the array starts at the zero position.
Input Sample | Output Sample |
10 | Menor valor: -5 |
Solution Using C:
#include <stdio.h>
int main() {
int i,n,min,p;
min = p=0;
scanf("%d",&n);
int arr[n];
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
if(i==0)
{
min = arr[i];
p = i;
}
else
{
if(min>arr[i])
{
min = arr[i];
p = i;
}
}
}
printf("Menor valor: %d\nPosicao: %d\n",min,p);
return 0;
}
int main() {
int i,n,min,p;
min = p=0;
scanf("%d",&n);
int arr[n];
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
if(i==0)
{
min = arr[i];
p = i;
}
else
{
if(min>arr[i])
{
min = arr[i];
p = i;
}
}
}
printf("Menor valor: %d\nPosicao: %d\n",min,p);
return 0;
}
Comments
Post a Comment