c++ 49,50,,51

49

#include<stdio.h>
int a[]={0,1,2,5,8,7,6,3};     /*指针数组.依次存入矩阵中构成环的元素下标*/
int b[9];                      /*表示3X3矩阵,b[4]为空格*/
int c[9];                      /*确定1所在的位置后,对环进行调整的指针数组*/
int count=0;                   /*数字移动步数计数器*/
void main()
{
   int i,j,k,t;
   void print();
   clrscr();
   puts("*****************************************************");
   puts("*         This is a program to Move Numbers.        *");
   puts("*****************************************************");
   printf(" >> Please enter original order of digits 1~8: ");
   for(i=0;i<8;i++)
      scanf("%d",&b[a[i]]);
               /*顺序输入矩阵外边的8个数字,矩阵元素的顺序由指针数组的元素a[i]控制*/
   printf("The sorting process is as felow:\n");
   print();
   for(t=-1,j=0;j<8&&t==-1;j++)          /*确定数字1所在的位置*/
      if(b[a[j]]==1) t=j;                 /*t:记录数字1所在的位置*/
   for(j=0;j<8;j++)     /*调整环的指针数组,将数字1所在的位置定为环的首*/
      c[j]=a[(j+t)%8];
   for(i=2;i<9;i++)         /*从2开始依次调整数字的位置*/
                        /*i:正在处理的数字,i对应在环中应当的正确位置就是i-1*/
      for(j=i-1;j<8;j++)     /*从i应处的正确位置开始顺序查找*/
if(b[c[j]]==i&&j!=i-1)      /*若i不在正确的位置*/
         {
            b[4]=i;          /*将i移到中心的空格中*/
   b[c[j]]=0;print();     /*空出i原来所在的位置,输出*/
            for(k=j;k!=i-1;k--)    /*将空格以前到i的正确位置之间的数字依次向后移动一格*/
            {
      b[c[k]]=b[c[k-1]];    /*数字向后移动*/
      b[c[k-1]]=0;
               print();
            }
   b[c[k]]=i;         /*将中间的数字i移入正确的位置*/
            b[4]=0;            /*空出中间的空格*/
            print();
            break;
         }
else if(b[c[j]]==i) break;       /*数字i在正确的位置*/
    printf("\n Press any key to quit...");
    getch();
}
void print(void)        /*按格式要求输出矩阵*/
{
   int c;
   printf(" >> Step No.%2d  ",count++);
   for(c=0;c<9;c++)
      if(c%3==2) printf("%2d  ",b[c]);
      else  printf("%2d",b[c]);
   printf("\n");
}


50

#include <stdio.h>
#include <math.h>
#define MAX 50
/* 下面的两个数组可以根据具体要求解的多项式来决定其值*/
static double p[6]={4,-6,3,1,-1,5}; /*表示多项式4x^5 - 6x^4 + 3x^3 + x^2 - x + 5 */
static double q[4]={3,2,-5,1}; /*表示多项式3x^3 + 2x^2 - 5x + 1 */
static double result[9]={0,0,0,0,0,0,0,0,0}; /*存放乘积多项式*/


void npmul(p,m,q,n,s)
int m,n;
double p[],q[],s[];
{
int i,j;
for (i=0; i<=m-1; i++)
for (j=0; j<=n-1; j++)
s[i+j]=s[i+j]+p[i]*q[j]; /*迭带计算各项系数*/
return;
}


double compute(s,k,x) /*计算所给多项式的值*/
double s[];
int k;
float x;
{
int i;
float multip = 1;
double sum = 0;
for (i=0;i<k;i++)
multip = multip * x; /*先求出x的最高次项的值*/
for (i=k-1;i>=0;i--)
{
sum = sum + s[i] * multip; /*依次从高到低求出相对应次项的值*/
if (x!=0)
multip = multip / x;
}
return sum;
}


void main()
{
int i,j,m,n;
double  px[MAX],qx[MAX],rx[MAX];
float x;
clrscr();
for(i=0;i<MAX;i++)
rx[i]=0;
puts("      This is a polynomial multiplication program.");
puts("It calculate the product of two polynomials: P(x) and Q(x)");
puts("       P(x)=Pm-1*x^(m-1)+Pm-2*x^(m-2)+...+P1*x+P0");
puts("       Q(x)=Qn-1*x^(n-1)+Qn-2*x^(n-2)+...+Q1*x+Q0");
printf("\n >> Please input m (>=1): ");
scanf("%d",&m);
printf(" >> Please input P%d, P%d, ... P1, P0 one by one:\n",m-1,m-2);
for(i=0;i<m;i++)
scanf("%f",&px[i]);
printf("\n >> Please input n (>=1): ");
scanf("%d",&n);
printf(" >> Please input Q%d, Q%d, ... Q1, Q0 one by one:\n",n-1,n-2);
for(i=0;i<n;i++)
scanf("%f",&qx[i]);
npmul(p,m,q,n,rx);
printf("\nThe product of two polynomials R(x) is :\n");
for (i=m+n-1,j=0;i>=1;i--) /*逐行逐项打印出结果多项式*/
{
printf(" (%f*x^%d) + ",rx[m+n-1-i],i-1);
if(j==2)
{
printf("\n");
j=0;
}
else
j++;
}
printf("\n");
printf("Input the value of x: ");
scanf("%f",&x);
printf("\nThe value of the R(%f) is: %13.7f",x,compute(rx,m+n-1,x));
puts("\n Press any key to quit...");
getch();
}


51

#include <stdio.h>
#include <sys\timeb.h>
#define Alpha 3.90


double initvalue();


double random(void)/*返回一个(0,1)之间的随机数*/
{
    static double f=-1.0;
    double initvlaue();
    if(f==-1.0) f=initvalue();
    else f=Alpha*f*(1.0-f);
    return f;
}


double initvalue()/*返回随机数序列初值*/
{
    double f0;
    struct timeb *pr;
    for(;;){
ftime(pr);
f0=pr->millitm*0.9876543*0.001;
if(f0<0.001) continue;
break;
    }
    return f0;
}


void main()
{
double test;
int i;
clrscr();
puts("This is a random number generator.");
puts("\n The random number are: ");
for ( i = 0; i < 3; i++ )
{
test = random();
printf ( " >> rand%d:%f\n", i, test );
}
puts("\n Press any key to quit...");
getch();
}

猜你喜欢

转载自blog.csdn.net/sherryhaha123/article/details/72962579