|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 bin554385863 于 2019-5-29 23:07 编辑
- #include <stdio.h>
- #include <stdlib.h>
- /*阶乘函数*/
- long long fact(int n)
- {
- long long fact = 1;
- if (n >= 0)
- {
- if ((n == 0) || (n == 1))
- {
- fact = 1;
- }
- else
- {
- for (int i = 1; i <= n; i++)
- {
- fact = fact * i;
- }
- }
- return (fact);
- }
- else
- {
- return -1;
- }
- }
- /*组合函数*/
- long long comb(int n, int k)
- {
- long long comb = 1;
- if ((n > 0) && (k >= 0))
- {
- if ((n == k) || (k == 0))
- {
- comb = 1;
- }
- else if (n > k)
- {
- comb = fact(n) / (fact(k) * fact(n - k));
- }
- else
- {
- return 1;
- }
- }
- else
- {
- return -1;
- }
- }
- /*用字符e表示一个数字数组的结束,el = endline*/
- #define el "'e'"
- /*数组长度函数*/
- size_t arrlen(size_t arr[])
- {
- size_t i = 1;
- while (*(arr + i) != el)
- {
- i++;
- }
- return i;
- }
- /*冒泡排序函数*/
- void arrsrt(size_t arr[])
- {
- int t;
- for (size_t i = 0; i < arrlen(arr); i++)
- {
- for (size_t j = 0; j < arrlen(arr)-1; j++)
- {
- if (arr[j] >= arr[j + 1])
- {
- t = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = t;
- }
- }
- }
- for (size_t i = 0; i < arrlen(arr); i++)
- {
- printf("%d ", arr[i]);
- }
- }
- /*数组搜索函数*/
- void arrsch(size_t arr[], size_t key)
- {
- size_t temp[arrlen(arr)];
- size_t j = 0;
- for (size_t i = 0; i < arrlen(arr); i++)
- {
- if (*(arr + i) == key)
- {
- temp[j] = i;
- j++;
- }
- }
- if (j >= 1)
- {
- printf("数据%d总共出现了%d次,下标是:", key, j);
- for (size_t i = 0; i < j; i++)
- {
- printf("%02d ", temp[i]);
- }
- }
- else
- {
- printf("NULL");
- }
- /*数字数组拼接函数arrcat(arr1, arr2),请确保arr1有足够的空间接受arr2的数据*/
- void arrcat(size_t arr1[], size_t arr2[])
- {
- size_t temp[arrlen(arr1) + arrlen(arr2)];
- size_t i, j, k;
- for (i = 0; i < arrlen(arr1); i++)
- {
- temp[i] = arr1[i];
- }
- for (j = 0; j < arrlen(arr2); j++)
- {
- temp[i + j] = arr2[j];
- }
- for (k = 0; k < i + j; k++)
- {
- arr1[k] = temp[k];
- arr1[k+1] = el;
- printf("%d ", arr1[k]);
- }
-
- }
复制代码
以上的数字数组函数全都是基于arrlen(size_t arr[])函数,实现
============================================================================
- #include <stdio.h>
- #include "E:\Administrator\Documents\My C\My C Function\Function.c"
- void main()
- {
- int a[14] = {0, 1, 2, 3, el};
- int b[] = {7, 5, 8, 4, el};
- int c[] = {6, 9, 10, 11, el};
- printf("拼接a[]和b[]\n");
- arrcat(a, b);
- printf("\n\n");
- printf("然后拼接c[]\n");
- arrcat(a, c);
- printf("\n\n");
- printf("对数组排序\n");
- arrsrt(a);
- printf("\n\n");
- printf("打印数组长度\n");
- printf("%d", arrlen(a));
- printf("\n\n");
- printf("查找数字8\n");
- arrsch(a , 8);
- }
复制代码
-----------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-asdrue3q.cef --stdout=Microsoft-MIEngine-Out-ply55g5c.jfq --stderr=Microsoft-MIEngine-Error-rxmm5n1v.0hm --pid=Microsoft-MIEngine-Pid-czjei4lx.uno --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
拼接a[]和b[]
0 1 2 3 7 5 8 4
然后拼接c[]
0 1 2 3 7 5 8 4 6 9 10 11
对数组排序
0 1 2 3 4 5 6 7 8 9 10 11
打印数组长度
12
查找数字8
数据8总共出现了1次,下标是:08
E:\Administrator\Documents\My C>
========================================================================== |
|