#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;
}
#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;
}
//說明:輸入的格式需要提示按輸入,因為要獲取正確的有序對才能進行復合運算
/*
*************輸入格式如: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");
}
#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;
}
參考修改過的代碼哈,歡迎交流,滿意請采納。
思路是先轉成字符串再操作
返回值為計算出的逆序數
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;
}
首先是要知道理論, 有反函數需要的是雙射, 對于有限集到自身的映射來說, 單射和滿射等價, 所以這里只需要判斷是否是滿射就可以了.
然后是技術上的實現. 如果有O(n)的存貯空間(比如開設一個長度為n的數組a[], 初始化成0), 那么遍歷一遍 k = 1,...,n, 置a[f(k)+1] = 1, 最后遍歷一遍 a 就可以判定 f 是否是滿射, 時間復雜度是O(n).
(如果實在沒有辦法開設額外的存貯空間, 那么至少可以按定義去判定 f 是否是單射, 時間復雜度是O(n^2).)
網站欄目:離散數學逆函數c語言代碼,離散數學與c程序設計
轉載來于:http://vcdvsql.cn/article18/hsscdp.html
成都網站建設公司_創新互聯,為您提供App設計、小程序開發、、軟件開發、移動網站建設、網站設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯