Hacer un programa o algoritmo que permita mostrar artículos de abarrotes ordenados por mayor precio y mostrar los 5 artículos de menor precio.
Solución
- Entrada: Arroz 29, Papa 15, Azúcar 10, Pan 2, Jabón 5,Harina 16,Leche 45
- Salida: Leche 45, Arroz 29, Harina 16, Papa 15, Azúcar 10, Jabón 5, Pan 2
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Main
{
class Program
{
static void Main(string[] args)
{
String cad="",aux;
int w=0,cont=0,tem;
Console.WriteLine("ingresa la cantidad de articulos a ingresar");
int t = Convert.ToInt16(Console.ReadLine());
if (t < 6)
{
Console.WriteLine("ingresa mas de 6 articulos");
}
else
{
String[] Art = new String[t];
int[] Pre = new int[t];
for (int i = 0; i < t; i++)
{
Art[i] = "";
Pre[i] = 0;
}
while (t > cont)
{
Console.WriteLine("Ingresa articulo");
String n = Console.ReadLine();
Art[cont] = n;
Console.WriteLine("Ingresa precio");
int p = Convert.ToInt16(Console.ReadLine());
Pre[cont] = p;
cont++;
}
while (w != (t - 1))
{
w = 0;
for (int k = 0; k < (t - 1); k++)
{
if (Pre[k] >= Pre[k + 1])
{
w++;
}
else
{
tem = Pre[k];
Pre[k] = Pre[k + 1];
Pre[k + 1] = tem;
aux = Art[k];
Art[k] = Art[k + 1];
Art[k + 1] = aux;
}
}
}
for (int j = 0; j < t; j++)
{
cad = cad + "Articulo: " + Art[j] + " Precio: " + Pre[j] + "\n";
}
Console.WriteLine(cad);
}
Console.ReadLine();
}
}
}
1 comentarios:
Me ayudo genial sigan subiendo mas códigos en c#
Publicar un comentario