离散数学逆函数c语言代码,离散数学与c程序设计
C语言写函数fun求一个整数的逆序数,在main中输入两个整数,求其逆序数之和并输出。如输入32 -71 输出6
#include stdio.h
眉县网站建设公司成都创新互联,眉县网站设计制作,有大型网站制作公司丰富经验。已为眉县上千多家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的眉县做网站的公司定做!
int fun(int num);
void main( )
{
int x,y,sum;
sum=0;
printf("请输入两个整数:");
scanf("%d%d",x,y);
sum=fun(x)+fun(y); %因为fun()函数有返回值,这里相当于将两个返回值相加
printf("sum=%d\n",sum);
}
int fun(int num)
{
int a,b; %定义 a,b变量
a=0;
while (num!=0) %判断循环的结束条件 此处num以两位数为例
{
b=num%10; %对10取余,即求出num的个位数赋值给b
a=a*10+b;
num=num/10; %将num的末位数去掉
}
return a;
}
C语言编程:编写一个函数求逆矩阵
#include stdio.h
#include stdlib.h
#include malloc.h
void MatrixOpp(double *A, int m, int n, double* invmat);
void MatrixInver(double *A, int m, int n, double* invmat);
double Surplus(double A[], int m, int n);
int matrix_inv(double* p, int num, double* invmat);
void MatrixOpp(double A[], int m, int n, double* invmat)
{
int i, j, x, y, k;
double *SP = NULL, *AB = NULL, *B = NULL, X;
SP = (double *) malloc(m * n * sizeof(double));
AB = (double *) malloc(m * n * sizeof(double));
B = (double *) malloc(m * n * sizeof(double));
X = Surplus(A, m, n);
X = 1 / X;
for (i = 0; i m; i++)
for (j = 0; j n; j++)
{
for (k = 0; k m * n; k++)
B[k] = A[k];
{
for (x = 0; x n; x++)
B[i * n + x] = 0;
for (y = 0; y m; y++)
B[m * y + j] = 0;
B[i * n + j] = 1;
SP[i * n + j] = Surplus(B, m, n);
AB[i * n + j] = X * SP[i * n + j];
}
}
MatrixInver(AB, m, n, invmat);
free(SP);
free(AB);
free(B);
}
void MatrixInver(double A[], int m, int n, double* invmat)
{
int i, j;
double *B = invmat;
for (i = 0; i n; i++)
for (j = 0; j m; j++)
B[i * m + j] = A[j * n + i];
}
double Surplus(double A[], int m, int n)
{
int i, j, k, p, r;
double X, temp = 1, temp1 = 1, s = 0, s1 = 0;
if (n == 2)
{
for (i = 0; i m; i++)
for (j = 0; j n; j++)
if ((i + j) % 2)
temp1 *= A[i * n + j];
else
temp *= A[i * n + j];
X = temp - temp1;
}
else
{
for (k = 0; k n; k++)
{
for (i = 0, j = k; i m, j n; i++, j++)
temp *= A[i * n + j];
if (m - i)
{
for (p = m - i, r = m - 1; p 0; p--, r--)
temp *= A[r * n + p - 1];
}
s += temp;
temp = 1;
}
for (k = n - 1; k = 0; k--)
{
for (i = 0, j = k; i m, j = 0; i++, j--)
temp1 *= A[i * n + j];
if (m - i)
{
for (p = m - 1, r = i; r m; p--, r++)
temp1 *= A[r * n + p];
}
s1 += temp1;
temp1 = 1;
}
X = s - s1;
}
return X;
}
int matrix_inv(double* p, int num, double* invmat)
{
if (p == NULL || invmat == NULL)
{
return 1;
}
if (num 10)
{
return 2;
}
MatrixOpp(p, num, num, invmat);
return 0;
}
int main()
{
int i, j;
int num;
double *arr=NULL;
double *result=NULL;
int flag;
printf("请输入矩阵维数:\n");
scanf("%d",num);
arr=(double *)malloc(sizeof(double)*num*num);
result=(double *)malloc(sizeof(double)*num*num);
printf("请输入%d*%d矩阵:\n", num, num);
for (i = 0; i num; i++)
{
for (j = 0; j num; j++)
{
scanf("%lf", arr[i * num + j]);
}
}
flag = matrix_inv(arr, num, result);
if(flag==0)
{
printf("逆矩阵为:\n");
for (i = 0; i num * num; i++)
{
printf("%lf\t ", *(result + i));
if (i % num == (num - 1))
printf("\n");
}
}
else if(flag==1)
{
printf("p/q为空\n");
}
else
{
printf("超过最大维数\n");
}
system("PAUSE");
free(arr);
free(result);
return 0;
}
c语言 离散数学集合复合运算的代码,(R。R)的代码实现
//说明:输入的格式需要提示按输入,因为要获取正确的有序对才能进行复合运算
/*
*************输入格式如:a b, #,# 退出***************
输入:a b
输入:b t
输入:t d
输入:s j
输入:j i
输入:c a
*/
#include "stdlib.h"
typedef char Element;
struct Node
{
Element left;
Element right;
struct Node *next;
};
struct Node *CreateLink();
struct Node *Operation(struct Node *R,struct Node *S);
void PrintLink(struct Node *h);
int main()
{
struct Node *hdR,*hdS,*rhd;
printf("请输入第一个集合R的关系\n");
hdR = CreateLink();
PrintLink(hdR);
printf("\n请输入第二个集合S的关系\n");
hdS = CreateLink();
PrintLink(hdS);
rhd = Operation(hdR,hdS);
if (rhd-next == NULL)
{
printf("\nR。S结果为空集\n");
}
else
{
printf("\nR。S结果为:\n");
PrintLink(rhd);
}
return 0;
}
struct Node *CreateLink()
{
struct Node *head, *p;
printf("*************输入格式如:a b, \'#,#\' 退出***************\n");
Element a,b;
head = (struct Node *)malloc(sizeof(struct Node));
head-left = 0;
head-right = 0;
head-next = NULL;
printf("输入:");
scanf("%c %c",a,b);
getchar();
while (a != '#')
{
p = (struct Node *)malloc(sizeof(struct Node));
p-left = a;
p-right = b;
p-next = head-next;
head-next = p;
printf("输入:");
scanf("%c %c",a,b);
getchar();
}
return head;
}
struct Node *Operation(struct Node *R, struct Node *S)
{
struct Node *newHead,*newP,*newQ;
struct Node *pH, *pNext;
newHead = (struct Node *)malloc(sizeof(struct Node));
newHead-left = 0;
newHead-right = 0;
newHead-next = NULL;
newP = newHead;
if (R-next == NULL || S-next == NULL)
{
return newP;
}
char cLeft,cRight;
pH = R;
while (pH-next != NULL)
{
cLeft = pH-next-left;
cRight = pH-next-right;
pNext = S-next;
while(pNext != NULL)
{
//存在可以复合运算的
if (cRight == pNext-left)
{
//在复合运算结果集中插入数据,如果存在相同的二元关系,则不需要插入
newP = newHead;
while (newP-next != NULL)
{
if (cLeft == newP-left cRight == newP-right)
{
break;
}
newP = newP-next;
}
if (newP-next == NULL)
{
newQ = (struct Node *)malloc(sizeof(struct Node));
newQ-left = cLeft;
newQ-right = pNext-right;
newQ-next = NULL;
newP-next = newQ;
// newQ-next = newP-next-next;
}
}
pNext = pNext-next;
}
pH = pH-next;
}
return newHead;
}
void PrintLink(struct Node *h)
{
struct Node *p=h-next;
printf("\n");
while (p != NULL)
{
printf("%c,%c ",p-left,p-right);
p = p-next;
}
printf("\n");
}
c语言求逆序数的代码调试
#includestdio.h
#includemath.h
int reserve(int number){
int i=1,sum=0,n;
while(number/(int)pow(10,i)0){// 这里用(int)pow(10,i)取整数,因为pow函数返回的是浮点型值
++i;}
while(i!=0){
n=number%10;
number=number/10;
sum=sum+n*pow(10,i-1);
i=i-1;
}
return sum;
}
int main(void){
int num,re;
printf("请输入一个整数:");
scanf("%d",num);
re=reserve(num);
printf("逆序数:%d\n",re);
return 0;
}
参考修改过的代码哈,欢迎交流,满意请采纳。
在C语言中如何定义逆序数函数
思路是先转成字符串再操作
返回值为计算出的逆序数
int f(int num)
{
char an[15];
int i, len, t, neg = 0;
if(num 0)
{
num = -num;
neg = 1;
}
sprintf(an, "%d", num);
len = strlen(an);
for(i=0; ilen/2; i++)
{
t = an[i];
an[i] = an[len - 1 -i];
an[len - 1 -i] = t;
}
num = atoi(an);
return (neg?-num:num);
}
刚才没看到你还没学到字符串,再给你个简单点的
int f(int num)
{
int a=0,b;
while (num != 0)
{
b=num%10;
a=a*10+b;
num=num/10;
}
return a;
}
给定一个从{1,2,...,n}到其自身的函数f,判断函数f是否有反函数? 怎么用c语言编程解决啊?
首先是要知道理论, 有反函数需要的是双射, 对于有限集到自身的映射来说, 单射和满射等价, 所以这里只需要判断是否是满射就可以了.
然后是技术上的实现. 如果有O(n)的存贮空间(比如开设一个长度为n的数组a[], 初始化成0), 那么遍历一遍 k = 1,...,n, 置a[f(k)+1] = 1, 最后遍历一遍 a 就可以判定 f 是否是满射, 时间复杂度是O(n).
(如果实在没有办法开设额外的存贮空间, 那么至少可以按定义去判定 f 是否是单射, 时间复杂度是O(n^2).)
网站名称:离散数学逆函数c语言代码,离散数学与c程序设计
转载源于:http://hbruida.cn/article/hsscdp.html