2003-1/*====================================================================================*/
/*函数名称:2003_1.c */
/*程序目的:将由整数构成的n(n≥2)阶方阵A就地按顺时针方向旋转90度 */
/*Writen by Apechn ,Soft Lab of JLU */
/*====================================================================================*/
#include <stdio.h>
const int N = 9;
void main()
{
int a[N][N];
int i,j,temp = 1,x = 0,y = N;
for (i = 0;i<N;i++) //赋初值
for (j = 0;j<N;j++)
{
a[i][j] = temp;
temp++;
}
for (i = 0;i<N-1;i++) //核心部分
{
for (j = x;j<y-1;j++) //根据题目要求,设计出如下几条地址变换规律
{
temp = a[i][j];
a[i][j] = a[N-j-1][i];
a[N-j-1][i] = a[N-i-1][N-j-1];
a[N-i-1][N-j-1] = a[j][N-i-1];
a[j][N-i-1] = temp;
}
x++;
y--;
}
for (i = 0;i<N;i++)
for (j = 0;j<N;j++)
printf("%d ",a[i][j]);
}
2003-2/*====================================================================================*/
/*函数名称:2003_2.c */
/*程序目的:将一个实数分解为它的整数和小数部分 */
/*Writen by Apechn ,Soft Lab of JLU */
/*====================================================================================*/
#include <stdio.h>
#define GET_ZHENGSHU 0; //宏定义,便于使用
#define GET_XIAOSHU 1
float fun(float f,int sign);
void main()
{
float a;
printf("Please input a:");
scanf("%f",&a);
printf("整数部分为:%f\n",fun(a,GET_ZHENGSHU));//取整数的使用方法
printf("小数部分为:%f\n",fun(a,GET_XIAOSHU)); //取小数的使用方法
}
float fun(float f,int sign)
{
float a = 0;
if (f>=0)
{
while (a<=f) //枚举
a = a + 1;
a = a - 1;
}
else
{
while (a>=f) //枚举
a = a - 1;
a = a + 1;
}
if (sign == 0)
return a;
else
return (f - a);
}
2003-3/*====================================================================================*/
/*函数名称:2003_3.c */
/*程序目的:对于0<x<1,利用Talor公式,求e^x的近似值,结果精确到10^-8 */
/*Writen by Apechn ,Soft Lab of JLU */
/*====================================================================================*/
#include <stdio.h>
#include <math.h>
double e(int x);
void main()
{
int x;
printf("Please input x: ");
scanf("%d",&x);
printf("\n The result is :%15.8f",e(x));
}
double e(int x)
{
double result1,result2;
double c = 1; //除数
double b = x; //被除数
int m = 1;
result1 = 1 + x;
result2 = 1;
while (fabs(result1 - result2) >= 1E-8) //控制精确度
{
m++;
c = c * m;
b = b * x;
result2 = result1;
result1 = result1 + b/c;
}
return result1;
}
2003-4/*====================================================================================*/
/*函数名称:2003_4.c */
/*程序目的:从输入的字符串中翻译并输出符合该句法的一个数 */
/*Writen by Apechn ,Soft Lab of JLU */
/*====================================================================================*/
#include <stdio.h>
#include <math.h>
int isdigtal(char c);
int todig(char c);
void compile(char* str);
void main()
{
char str[40];
printf("Please input a string: ");
scanf("%s",str);
compile(str);
}
void compile(char* str)
{
double num = 0;
int sign = 0,exp = 0;
char *p = str,c;
c = *p;
while (c != '\0')
{
if (isdigtal(c)) //如果c是数字
{
switch(sign)
{
case 0:
sign = 1;
num = todig(c);
break;
case 1:
num = num * 10 + todig(c);
break;
case 2:
num = num + todig(c) * pow(10,exp);
exp--;
break;
}
}
else if (c == '.') //如果c是小数点
{
if (sign == 1)
{
sign = 2;
exp = -1;
}
}
else //如果c是其它字符
{
if (sign != 0)
{
printf("the number is :%f\n",num);
return;
}
}
p = p + 1;
c = *p;
}
printf("the number is :%f\n",num);
}
2003-5/*====================================================================================*/
/*函数名称:2003_5.c */
/*程序目的:将递归函数转化为非递归函数 */
/*Writen by Apechn ,Soft Lab of JLU */
/*====================================================================================*/
float f1(float x,float y)
{
if (x<0)
return x+y;
return f1(x-1,x+y) + x/y;
}
float f2(float x,float y)
{
float result,a,b;
if (x<0)
return x+y;
result = x/y;
a = x;
b = y;
while (a>=0)
{
b = a + b;
a = a - 1;
result = result + a/b;
}
result = result + (a + b);
return result;
}
2003-6/*=============================================================================*/
/*程序名称:2003_6.c */
/*程序目的:从已知二叉树的前序和中序序列构造该二叉树 */
/*Writen by Apechn ,Soft Lab of JLU */
/*=============================================================================*/
#include <stdio.h>
struct node //定义链表结点结构,在最前面给出
{
int data;
node *left;
node *right;
}
node *bintree(int i,j,u,v;node *s)
{
int k,l;
s = NULL; //根指针初始化,s为空树
if (j >= i)
{
s = node *(malloc(sizeof(node)));//建立根结点
s->data = pre[i];
k = u;
while (ind[k] != pre[i]) //在中序序列中查找根结点
k++;
l = i + k - u; //l为左字树中最右下结点在前序序列中的位置
if (k == u) //构造左子树
s->left = NULL;
else
bintree(i+1,l,u,k-1,s->left);
if (k == v) //构造右子树
s->right = NULL;
else
bintree(l+1,j,k+1,v,s->right);
}
}
2003-7/*====================================================================================*/
/*函数名称:2003_7.c */
/*程序目的:求由100个整数构成的序列L的最大和子序列 */
/*Writen by Apechn ,Soft Lab of JLU */
/*====================================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
const int N = 100;
void main()
{
int A[N],stime;
long ltime;
int a = 0,b = 0,i,j,k,num = 0,max = -100;
ltime = time(NULL);
stime = (unsigned)ltime/2;
srand(stime);
for (i = 0;i<N;i++)
A[i] = rand() % 100 - 50;
printf("\nThe subserial is :\n");
for (i = 0;i<N;i++) //主要部分
for (j = N-1;j>=i;j--)
{ //i和j为子序列的边界
num = 0;
for (k = i;k<=j;k++)
num += A[k]; //求出子序列的和
if (num > max) //如果当前序列的和大于记录,那么记下边界
{
max = num;
a = i;
b = j;
}
}
for (k = a;k<=b;k++)
printf("%3d",A[k]);
}
2003-8/*====================================================================================*/
/*函数名称:2003_8.c */
/*程序目的:编程根据经过合理变换得到的序列B中的数据,依次输出序列A中的数值 */
/*Writen by Apechn ,Soft Lab of JLU */
/*====================================================================================*/
#include <stdio.h>
const int N = 10;
void main()
{
int A[N],b[N] = {0,1,0,3,2,3,2,4,0,4};
int temp[N] = {1,2,3,4,5,6,7,8,9,10}; //临时数组,在确定A时使用
int i,j,k;
for (i = N - 1;i>=0;i--) //对数组B从后向前扫描
{
k = B[i];
j = 0;
while (k != 0) //从临时数组temp中找到k=B[i]个没有确定下来的数
{
if (temp [j] != N + 1)
k--;
j++;
}
while (temp[j] == N + 1) //找到下一个还没有被确定下来的数
j++; //就应该放到A[i]处
A[i] = temp[j];
temp[j] = N + 1;
}
for (i = 0;i<N;i++)
printf("%d ",A[i]);
}