Sei sulla pagina 1di 13

PERMUTACIONES

Recursividad

Se dice que una funcin es recursiva cuando se define en funcin de si misma. No todas las funciones
pueden llamarse a s mismas, deben estar diseadas especialmente para que sean recursivas, de otro
modo podran conducir a bucles infinitos, o a que el programa termine inadecuadamente. C++ permite
la recursividad. Cuando se llama a una funcin, se crea un nuevo juego de variables locales, de este
modo, si la funcin hace una llamada a s misma, se guardan sus variables y parmetros en la pila, y
la nueva instancia de la funcin trabajar con su propia copia de las variables locales, cuando esta
segunda instancia de la funcin retorna, recupera las variables y los parmetros de la pila y continua la
ejecucin en el punto en que haba sido llamada.

CODIGO EN C++

//Realiza todas las permutaciones posibles de int n = strlen(cad);


las letras de la palabra ABCDE for(i = 0; i < n-l; i++)
#include <iostream> {
#include <string.h> if(n-l > 2) Permutaciones(cad, l+1);
void Permutaciones(char *, int l=0); else cout <<cad << ", ";
using namespace std; /* Intercambio de posiciones */
int main(int argc, char *argv[]) c = cad[l];
{ cad[l] = cad[l+i+1];
char palabra[] = "ABCDE"; cad[l+i+1] = c;
Permutaciones(palabra); if(l+i == n-1)
return 0; {
} for(j = l; j < n; j++) cad[j] = cad[j+1];
void Permutaciones(char * cad, int l) cad[n] = 0;
{ }
char c; /* variable auxiliar para intercambio }
*/ }
int i, j; /* variables para bucles */
CODIGO EN JAVA

package ejercicio13; }
import java.math.BigInteger; int temp;
class permutaciones { int j = a.length - 2;
private int[] a; while (a[j] > a[j + 1]) {
private BigInteger numLeft; j--;
private BigInteger total; }
public permutaciones(int n) { int k = a.length - 1;
if (n < 1) { while (a[j] > a[k]) {
throw new IllegalArgumentException("Min k--;
1"); }
} temp = a[k];
a = new int[n]; a[k] = a[j];
total = getFactorial(n); a[j] = temp;
reset(); int r = a.length - 1;
} int s = j + 1;
public void reset() { while (r > s) {
for (int i = 0; i < a.length; i++) { temp = a[s];
a[i] = i; a[s] = a[r];
} a[r] = temp;
numLeft = new BigInteger(total.toString()); r--;
} s++;
public BigInteger getNumLeft() { }
return numLeft; numLeft =
} numLeft.subtract(BigInteger.ONE);
public BigInteger getTotal() { return a;
return total; }
}
public boolean hasMore() { public static void main(String[] args) {
return int[] indices;
numLeft.compareTo(BigInteger.ZERO) == 1; String[] elements = { "A", "B", "C", "D","E" };
} permutaciones x = new
private static BigInteger getFactorial(int n) { permutaciones(elements.length);
BigInteger fact = BigInteger.ONE; StringBuffer permutation;
for (int i = n; i > 1; i--) { while (x.hasMore()) {
fact = fact.multiply(new permutation = new StringBuffer();
BigInteger(Integer.toString(i))); indices = x.getNext();
} for (int i = 0; i < indices.length; i++) {
return fact;
} permutation.append(elements[indices[i]]);
public int[] getNext() { }
if (numLeft.equals(total)) { System.out.println(permutation.toString());
numLeft = }
numLeft.subtract(BigInteger.ONE); }
return a; }
DERIVADA

Este programa resuelve la derivada del siguiente polinomio: ax4 +bx3 + cx2 + dx + e.

CODIGO EN C++

// 4 3 2
//Desarrollo de derivadas de la forma ax + bx + cx + dx + e
#include<iostream> }
#include<math.h> double primer( double a )
using namespace std; {
double primer( double ); double s=pow(l,3);
double segundo( double); double q=(a*s)*4;
double tercer(double); return q;
double cuarto(double ); }
double quinto(double ); double segundo( double b )
double a,b,c,d,j; {
double l; double t=pow(l,2);
double h,m; double r=(b*t)*3;
void main() return r;
{ }
cout<<" 4 3 2 "<<endl; double tercer( double c )
cout<<"Derivada de la forma ax + bx + {
cx + dx + e"<<endl; double u=pow(l,1);
cout<<"Ingrese valor a: "; double p=(c*u)*2;
cin>>a; return p;
cout<<"Ingrese valor b: "; }
cin>>b; double cuarto( double d )
cout<<"Ingrese valor c: "; {
cin>>c; double w=pow(l,0);
cout<<"Ingrese valor d: "; double v=(d*w)/1;
cin>>d; return v;
cout<<"Ingrese valor e: "; }
cin>>j; double quinto( double j )
cout<<"Ingrese el valor a derivar: "; {
cin>>l; double g=0;
h = primer(a)+ segundo(b)+ tercer(c)+ double h=(j*g);
cuarto(d)+quinto(j); return h;
cout<<"Valor de derivada: "<<h<<endl; }
CODIGO EN JAVA

package problemas; btnOperar = new javax.swing.JButton();


txtResultado = new
import static java.lang.Math.pow; javax.swing.JTextField();
import javax.swing.JOptionPane; jLabel10 = new javax.swing.JLabel();

public class Problema15 extends


javax.swing.JFrame { setDefaultCloseOperation(javax.swing.Window
Constants.EXIT_ON_CLOSE);
/** setTitle("Calculo de Derivadas ::");
* Creates new form Problema15 getContentPane().setLayout(new
*/ org.netbeans.lib.awtextra.AbsoluteLayout());
long x;
double h; jLabel1.setFont(new
public Problema15() { java.awt.Font("Papyrus", 1, 12)); // NOI18N
initComponents(); jLabel1.setText("Ingrese Quinto Valor :");
} getContentPane().add(jLabel1, new
private void initComponents() { org.netbeans.lib.awtextra.AbsoluteConstraints(
jLabel1 = new javax.swing.JLabel(); 30, 240, -1, -1));
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel(); jLabel2.setFont(new
jLabel4 = new javax.swing.JLabel(); java.awt.Font("Papyrus", 1, 12)); // NOI18N
jLabel5 = new javax.swing.JLabel(); jLabel2.setText(" 4 3 2 1 ");
jLabel6 = new javax.swing.JLabel(); getContentPane().add(jLabel2, new
jLabel7 = new javax.swing.JLabel(); org.netbeans.lib.awtextra.AbsoluteConstraints(
jLabel8 = new javax.swing.JLabel(); 90, 70, 130, -1));
jLabel9 = new javax.swing.JLabel();
txtE = new javax.swing.JTextField(); jLabel3.setFont(new
txtA = new javax.swing.JTextField(); java.awt.Font("Papyrus", 1, 12)); // NOI18N
txtB = new javax.swing.JTextField(); jLabel3.setText("ax + bx + cx + dx + e");
txtC = new javax.swing.JTextField(); getContentPane().add(jLabel3, new
txtD = new javax.swing.JTextField(); org.netbeans.lib.awtextra.AbsoluteConstraints(
btnLimpiar = new javax.swing.JButton(); 99, 90, 170, -1));
txtA.addActionListener(new
jLabel4.setFont(new java.awt.event.ActionListener() {
java.awt.Font("Papyrus", 1, 36)); // NOI18N public void
jLabel4.setText(" Derivadas "); actionPerformed(java.awt.event.ActionEvent
getContentPane().add(jLabel4, new evt) {
org.netbeans.lib.awtextra.AbsoluteConstraints( txtAActionPerformed(evt);
200, 20, 370, -1)); }
});
jLabel5.setFont(new getContentPane().add(txtA, new
java.awt.Font("Papyrus", 1, 12)); // NOI18N org.netbeans.lib.awtextra.AbsoluteConstraints(
jLabel5.setText("Modelo"); 230, 120, 160, -1));
getContentPane().add(jLabel5, new getContentPane().add(txtB, new
org.netbeans.lib.awtextra.AbsoluteConstraints( org.netbeans.lib.awtextra.AbsoluteConstraints(
30, 60, -1, -1)); 230, 150, 160, -1));
getContentPane().add(txtC, new
jLabel6.setFont(new org.netbeans.lib.awtextra.AbsoluteConstraints(
java.awt.Font("Papyrus", 1, 12)); // NOI18N 230, 180, 160, -1));
jLabel6.setText("Resultado ::"); getContentPane().add(txtD, new
getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(
org.netbeans.lib.awtextra.AbsoluteConstraints( 230, 210, 160, -1));
530, 140, -1, -1));
btnLimpiar.setFont(new
jLabel7.setFont(new java.awt.Font("Papyrus", 1, 14)); // NOI18N
java.awt.Font("Papyrus", 1, 12)); // NOI18N btnLimpiar.setText("Limpiar");
jLabel7.setText("Ingrese Segundo Valor btnLimpiar.addActionListener(new
:"); java.awt.event.ActionListener() {
getContentPane().add(jLabel7, new public void
org.netbeans.lib.awtextra.AbsoluteConstraints( actionPerformed(java.awt.event.ActionEvent
30, 150, -1, -1)); evt) {
btnLimpiarActionPerformed(evt);
jLabel8.setFont(new }
java.awt.Font("Papyrus", 1, 12)); // NOI18N });
jLabel8.setText("Ingrese Tercer Valor :"); getContentPane().add(btnLimpiar, new
getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(
org.netbeans.lib.awtextra.AbsoluteConstraints( 410, 190, -1, -1));
30, 180, -1, -1));
btnOperar.setFont(new
jLabel9.setFont(new java.awt.Font("Papyrus", 1, 14)); // NOI18N
java.awt.Font("Papyrus", 1, 12)); // NOI18N btnOperar.setText("Operar");
jLabel9.setText("Ingrese Cuarto Valor :"); btnOperar.addActionListener(new
getContentPane().add(jLabel9, new java.awt.event.ActionListener() {
org.netbeans.lib.awtextra.AbsoluteConstraints( public void
30, 210, -1, -1)); actionPerformed(java.awt.event.ActionEvent
getContentPane().add(txtE, new evt) {
org.netbeans.lib.awtextra.AbsoluteConstraints( btnOperarActionPerformed(evt);
230, 240, 160, -1)); }
});
getContentPane().add(btnOperar, new
org.netbeans.lib.awtextra.AbsoluteConstraints( }//GEN-
410, 140, -1, -1)); LAST:event_btnOperarActionPerformed
getContentPane().add(txtResultado, new
org.netbeans.lib.awtextra.AbsoluteConstraints( private void
530, 170, 110, -1)); btnLimpiarActionPerformed(java.awt.event.Acti
onEvent evt) {//GEN-
jLabel10.setFont(new FIRST:event_btnLimpiarActionPerformed
java.awt.Font("Papyrus", 1, 12)); // NOI18N limpiar();
jLabel10.setText("Ingrese Primer Valor :");
getContentPane().add(jLabel10, new
org.netbeans.lib.awtextra.AbsoluteConstraints( }//GEN-
30, 120, -1, -1)); LAST:event_btnLimpiarActionPerformed
private void limpiar(){
pack(); txtA.setText("");
}// </editor-fold>//GEN-END:initComponents txtB.setText("");
txtC.setText("");
private void txtD.setText("");
btnOperarActionPerformed(java.awt.event.Acti txtE.setText("");
onEvent evt) {//GEN- txtA.requestFocus();
FIRST:event_btnOperarActionPerformed
float resultado =0.0f;
String dato = }
JOptionPane.showInputDialog("Ingrese el valor private void
a derivar :"); txtAActionPerformed(java.awt.event.ActionEve
x = Long.valueOf(dato); nt evt) {//GEN-
double x4 = FIRST:event_txtAActionPerformed
Double.valueOf(txtA.getText()); // TODO add your handling code here:
double x3 = }//GEN-LAST:event_txtAActionPerformed
Double.valueOf(txtB.getText()); private double primer(double a){
double x2 = double s = pow(x,3);
Double.valueOf(txtC.getText()); double q = (a*s)*4;
double x1 = return q;
Double.valueOf(txtD.getText());
double x0 = }
Double.valueOf(txtE.getText()); private double segundo(double b){
double s = pow(x,2);
h = primer(x4) + segundo(x3) + tercero(x2) double q = (b*s)*3;
+ cuarto(x1) + quinto(x0); return q;
}
//JOptionPane.showMessageDialog(null,"Resul private double tercero(double c){
tado : " + String.valueOf(h));
txtResultado.setText( String.valueOf(h)); double q = (c*x)*2;
limpiar(); return q;

}
private double cuarto(double d){
return d; java.util.logging.Logger.getLogger(Problema15
} .class.getName()).log(java.util.logging.Level.SE
private double quinto(double e){ VERE, null, ex);
}
return 0; //</editor-fold>
}
/* Create and display the form */
try { java.awt.EventQueue.invokeLater(new
for Runnable() {
(javax.swing.UIManager.LookAndFeelInfo info : public void run() {
javax.swing.UIManager.getInstalledLookAndFe new Problema15().setVisible(true);
els()) { }
if ("Nimbus".equals(info.getName())) { });
}
javax.swing.UIManager.setLookAndFeel(info.g
etClassName()); // Variables declaration - do not modify//GEN-
break; BEGIN:variables
} private javax.swing.JButton btnLimpiar;
} private javax.swing.JButton btnOperar;
} catch (ClassNotFoundException ex) { private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
java.util.logging.Logger.getLogger(Problema15 private javax.swing.JLabel jLabel2;
.class.getName()).log(java.util.logging.Level.SE private javax.swing.JLabel jLabel3;
VERE, null, ex); private javax.swing.JLabel jLabel4;
} catch (InstantiationException ex) { private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
java.util.logging.Logger.getLogger(Problema15 private javax.swing.JLabel jLabel7;
.class.getName()).log(java.util.logging.Level.SE private javax.swing.JLabel jLabel8;
VERE, null, ex); private javax.swing.JLabel jLabel9;
} catch (IllegalAccessException ex) { private javax.swing.JTextField txtA;
private javax.swing.JTextField txtB;
java.util.logging.Logger.getLogger(Problema15 private javax.swing.JTextField txtC;
.class.getName()).log(java.util.logging.Level.SE private javax.swing.JTextField txtD;
VERE, null, ex); private javax.swing.JTextField txtE;
} catch private javax.swing.JTextField txtResultado;
(javax.swing.UnsupportedLookAndFeelExcepti // End of variables declaration//GEN-END:
on ex) { variables
}
INTEGRAL

Este programa en c ++ resuelve la integral definida seno(x)coseno(x)dx utilizando el mtodo del


trapecio.

CODIGO EN C++

#include<iostream> cin>>b;
#include<math.h> cout<<endl<<endl;cout<<endl;
using namespace std;
main () delta = (b-a)/n;
{ s=0;
cout<<endl<<endl;
cout<<" INTEGRALES for (i=1;i<=n;i++)
DEFINIDAS"<<endl; {
s = s + sin(a +
cout<<endl<<endl; cout<<endl; delta * i) * cos(a + delta * i) * delta;
cout<<" }
___________________________________"<<
endl;
cout<<" | b |"<<endl;
cout<<" | ( |"<<endl; cout<<"la integral es :";
cout<<" | ) seno(x)coseno(x)dx | cout<<endl<<endl;
"<<endl; cout<<"
cout<<" | a | "<<endl; ___________________________________
cout<<" "<<endl;
|___________________________________|"< cout<<" | b |"<<endl;
<endl; cout<<" | ( |"<<endl;
cout<<endl; cout<<endl; cout<<" | ) seno(x)coseno(x)dx |
double s,a,b,n,i,delta; = "<<s<<endl;
cout <<"ingrese n el numero de cout<<" | a |"<<endl;
particiones = "; cout<<"
cin>>n; |___________________________________|"<
cout <<"ingrese a el limite inferior = "; <endl;
cin>>a; cout<<endl; cout<<endl;
cout <<"ingrese b el limite superior = "; }
CODIGO EN JAVA

package ejercicio16; System.out.println(" | a


import java.util.Scanner; | ");
public class Ejercicio16 { System.out.println("
static Scanner leer=new Scanner(System.in); |__________________________________|\n")
public static void main(String[] args) { ;
System.out.println("\n"); System.out.println("");
System.out.println(" INTEGRALES double s,a,b,n,i,delta;
DEFINIDAS\n"); System.out.println("ingrese n el numero
System.out.println(" de particiones = ");
___________________________________"); n=leer.nextDouble();
System.out.println(" | b System.out.println("ingrese a el limite
|"); inferior = ");
System.out.println(" | ( a=leer.nextDouble();
|"); System.out.println("ingrese b el limite
System.out.println(" | ) superior = ");
seno(x)coseno(x)dx | "); b=leer.nextDouble();
System.out.println("\n"); System.out.println(" | (
delta = (b-a)/n; |");
s=0; System.out.println(" | )
for (i=1;i<=n;i++) seno(x)coseno(x)dx | = "+s);
{ System.out.println(" | a
s = s + Math.sin(a + delta * i) * |");
Math.cos(a + delta * i) * delta; System.out.println("
} |___________________________________|\n
System.out.println("la integral es :\n"); ");
System.out.println(" System.out.println("");
___________________________________"); }
System.out.println(" | b }
|");

Potrebbero piacerti anche