Anda di halaman 1dari 10

//P1: generar numeros aleatorios del o al 100 en C.

#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;

/* run this program using the console pauser or add your own getch,
system("pause") or input loop */

int main()
{ int i;
time_t t;
srand( (unsigned) time (&t));
printf("\n 10 numeros aleatorios entre <0-100>");
for(i=1; i<=10; i++)
printf("\n %d",rand()%101);
getch();
return 0;
}
/P2:generea dos dados y suma
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;

/* run this program using the console pauser or add your own getch,
system("pause") or input loop */

int main()
{ int d1,d2,suma;
time_t t;
srand( (unsigned) time (&t));
d1=rand()%6+1;
d2=rand()%6+2;
suma=d1+d2;
printf("\n dado1=%d \n dado2=%d \n suma=%d",d1,d2,suma);
return 0;
}
//P3: generar n numero aleatorios entre a y b
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;

/* run this program using the console pauser or add your own getch,
system("pause") or input loop */

int main()
{ int i,n,a,b,num;
time_t t;
srand( (unsigned) time (&t));
printf("\n Ingrese <n>:"); scanf("%d",&n);
printf("\n Ingrese menor <a>:"); scanf("%d",&a);
printf("\n Ingrese mayor <b>:"); scanf("%d",&b);
for(i=1; i<=n; i++)
{
num=rand()%(b-a+1)+a;
printf("\n numero %d=%d",i,num);
return 0;
}

//p5
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;

/* run this program using the console pauser or add your own getch,
system("pause") or input loop */

int main()
{ int i,j,n,m,a,b,A[100],B[100],C[100],temp;
time_t t;
srand( (unsigned) time (&t));
printf("\n Ingrese tamao de A [ ] <n>:"); scanf("%d",&n);
printf("\n Ingrese tamao de B [ ] <n>:"); scanf("%d",&m);
printf("\n Ingrese valor menor <a>:"); scanf("%d",&a);
printf("\n Ingrese valor mayor <b>:"); scanf("%d",&b);
//* primer vector

for(i=1; i<=n; i++)

{
A[i]=rand()%(b-a+1)+a;
C[i]=A[i];
}
for(i=1; i<=m; i++)

B[i]=rand()%(b-a+1)+a;
C[n+1]=B[i];
}
//* ORDENANDO TERCER VECTOR
for(i=1; i<=n+m; i++)
for(j=i+1; j<=n+m; j++)
if(c[i]>c[j])
{
temp=C[i];
C[i]=C[j];
C[j]=temp;
}
//*imprimiendo
for(i=1;i<=n+m;i++)
printf("\n C[%d]=%d",i,C[i]);

return 0;
}

P6:
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;

/* run this program using the console pauser or add your own getch,
system("pause") or input loop */

int main()
{ int i,j,n,m,a,b,c,d,A[100],B[100],C[100],temp;
time_t t;
srand( (unsigned) time (&t));
printf("\n Ingrese tamao de A [ ] <n>:"); scanf("%d",&n);
printf("\n Ingrese valor menor <a>:"); scanf("%d",&a);
printf("\n Ingrese valor mayor <b>:"); scanf("%d",&b);
printf("\n Ingrese tamao de B [ ] <n>:"); scanf("%d",&k);
printf("\n Ingrese valor menor <a>:"); scanf("%d",&c);
printf("\n Ingrese valor mayor <b>:"); scanf("%d",&d);
//* primer vector

for(i=1; i<=n; i++)

{
A[i]=rand()%(b-a+1)+a;
C[i]=A[i];
}
for(i=1; i<=k; i++)

B[i]=rand()%(d-c+1)+c;
C[n+1]=B[i];
}
//* ORDENANDO TERCER VECTOR
for(i=1; i<=n+k; i++)
for(j=i+1; j<=n+k; j++)
if(c[i]>c[j])
{
temp=C[i];
C[i]=C[j];
C[j]=temp;
}
//*imprimiendo
for(i=1;i<=n+k;i++)
printf("\n C[%d]=%d",i,C[i]);

//mediana
if(C%2!=0) {
printf("\nEL valor de la mediana es : %d",C[i/2]);
} else {
printf("\nEL valor 1 de la mediana es : %d",C[i/2]);
printf("\nEL valor 2 de la mediana es : %d",C[(i/2)-1]);
}

return 0;
}

/*programa p6.cpp*/
/*Busqueda Heuristica*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<iostream>
#include<time.h>
using namespace std;
int main(){
int i,j,n,m[100][100],cont=0,t1=0,t2=0,t3=0,t4=0,controlador=0;
time_t t;
srand((unsigned) time(&t));
printf("\n tananho de la matriz[20]*[20]:\n");
for(i=1;i<=20;i++)
for(j=1;j<=20;j++){
m[i][j]=rand()%20+01;
printf("%2d ",m[i][j]);
}
i=1; j=1;
do{
if(j!=0 && t1!=j)
if(m[i][j]<6){
t1=j;
j++;
if(j!=0){
cont++;
printf("\nmovimiento %d del [%d,%d] al [%d,
%d] entonces avanza",cont,i,j-1,i,j);
}
}
if(j==0 || t1==j){
printf("\nno hay mas movimientos");
controlador=1; break;
}
if(j!=0 && t2!=j)
if(m[i][j]>5 && m[i][j]<11){
t2=j;
j--;
if(j!=0){
cont++;
printf("\nmovimiento %d del [%d,%d] al [%d,
%d] entonces retrocede",cont,i,j+1,i,j);
}
}
if(j==0 || t2==j){
printf("\nno hay mas movimientos");
controlador=1; break;
}
if(i!=0 && t3!=i)
if(m[i][j]>10 && m[i][j]<16){
t3=i;
i--;
if(i!=0){
cont++;
printf("\nmovimiento %d del [%d,%d] al [%d,
%d] entonces sube",cont,i+1,j,i,j);
}
}
if(i==0 || t3==i){
printf("\nno hay mas movimientos");
controlador=1; break;
}
if(i!=0 && t4!=i)
if(m[i][j]>15){
t4=i;
i++;
if(i!=0){
cont++;
printf("\nmovimiento %d del [%d,%d] al [%d,
%d] entonces baja",cont,i-1,j,i,j);
}
}
if(i==0 || t4==i){
printf("\nno hay mas movimientos");
controlador=1; break;
}
} while(controlador==0);
printf("\nnumero de movimientos=%d",cont);
}

Anda mungkin juga menyukai