#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>voidReplaceBlank(char* str,int len){int MLen =0;//计算字符的个数int NumBlank =0;//计算空格的数组int IndexofMLen =0;int Indexofnew =0;int newlen =0;int i =0;//从0号下标开始遍历if(str ==NULL|| len <=0){return;}while(str[i]!='\0'){++MLen;if(str[i]==' '){++NumBlank;}++i;}
newlen = MLen + NumBlank *2;//a%20b%20c%20d a b c dif(newlen > len)//newlen是扩充后数组的大小,所以一定要小于len原来大小,不然放不下{return;}
IndexofMLen = MLen;
Indexofnew = newlen;while(IndexofMLen >=0&& Indexofnew > IndexofMLen){if(str[IndexofMLen]==' '){
str[Indexofnew--]='0';
str[Indexofnew--]='2';
str[Indexofnew--]='%';}else{
str[Indexofnew--]= str[IndexofMLen];}--IndexofMLen;}}intmain(){char str[30]="a b c d";int len =(sizeof(str)/sizeof(str[0]));ReplaceBlank(str, len);printf("%s\n", str);return0;}
3、编写函数,求第n个斐波那契数列的值(非递归)
#include<stdio.h>intFib(int n){int a =1;int b =1;int c =1;while(n >2){
c = a + b;
a = b;
b = c;
n--;}return c;}intmain(){int n =0;scanf("%d",&n);int ret =Fib(n);printf("%d\n", ret);return0;}
4、写一个函数求,求 unsigned int 型变量 x 在内存中二进制 1 的个数
#include<stdio.h>intCntNumOne(int n){int count =0;while(n){++count;
n =(n -1)& n;}return count;}intmain(){printf("%d\n",CntNumOne(-1));return0;}
5、有一个数组 a , 编写函数,求数组中前K个最小的数字
#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>intFindMin(int a[],int k){int min = a[0];int i =0;for(i =0; i < k -1; i++){if(a[i]< min)
min = a[i];}return min;}intmain(){int a[]={5,3,7,8,1,4,9,10,6,2};int k =7;int ret =FindMin(a, k);printf("%d\n", ret);return0;}
6、在字符串中找出第一个只出现一次的字符。如输入 “abaccdeff” ,则输出 ‘b’ 。
#include<stdio.h>#include<string.h>voidfind_string(char* str){int len =strlen(str);int i =0;for(i =0; i < len; i++){int j =0;int cnt =1;for(j =0; j < len; j++){if(i == j)continue;if(str[i]== str[j]){
cnt =0;break;}}if(cnt ==1){printf("%c\n", str[i]);return0;}}}intmain(){char arr[]="abaccdeff";find_string(arr);return0;}
#include<stdio.h>intis_num(int n){int n2 = n;int y =0;while(n2){
y = y *10+ n2 %10;
n2 /=10;}if(y == n)return1;elsereturn0;}intmain(){int n =0;scanf("%d",&n);int ret =is_num(n);if(ret ==1)printf("YES\n");elseprintf("NO\n");return0;}
voidPalindrome(int num){int newed =0;int n = num;while(num >0){
newed = newed *10+ num %10;
num /=10;}if(n == newed){printf("Yes\n");}else{printf("No\n");}}
11、模拟实现函数 pow(x,y) , 即实现运算x^y(x的y次方), 这里x和y都为整数。
#include<stdio.h>longlongPow(int x,int y){if(y ==0)return1;elseif(y ==1)return x;else{longlong i =0;longlong x2 =1;for(i =0; i < y; i++){
x2 *= x;}return x2;}}intmain(){longlong a =2;longlong b =4;longlong ret =Pow(a, b);printf("%lld", ret);}
/*函数
pow(x, y)实现运算x ^ y,即x的y次方,这里x和y都为整数。
算法的基本思想是,减少乘法次数,重复利用结算结果,例如:
x ^ 4,如果逐个相乘的话,需要四次乘法。
如果我们这样分解(x ^ 2) * (x ^ 2)就只需要2两次乘法,
因为x ^ 2的结果我们可以重复利用。所以我们最好做对称的分解指数y,
然后求x ^ (y / 2)的平方。
具体算法如下:
1 如果y为偶数,直接计算mypow(x, y / 2) * mypow(x, y / 2);
2 如果y为奇数,则y - 1为偶数,回到了第一种情况。
*/intMypow(int x,int y){int result =0;int tmp =0;if(y ==1)return x;
tmp =mypow(x, y /2);if(y &1!=0)//奇数{
result = x * tmp * tmp;}else{
result = tmp * tmp;}return result;}
12、如何判断一个数n是否是2的k次方?注意:不用求K是多少,只需要判断,请编写函数实现。
#include<stdio.h>voidis_power(int n){if((n &(n -1))==0){printf("YES\n");}else{printf("NO\n");}}intmain(){int n =0;scanf("%d",&n);is_power(n);return0;}
intMAX_Arry(int* arr,int sz){int MAX = arr[0];int sum = arr[0];int i =1;if(arr ==NULL|| sz <=1)return0;for(i =1; i < sz; i++){if(sum <0)
sum = arr[i];else{
sum += arr[i];}if(sum > MAX)
MAX = sum;}return MAX;}intmain(){int arr[]={1,-2,3,10,-4,7,2,-5};int len =sizeof(arr)/sizeof(arr[0]);printf("%d\n",MAX_Arry(arr, len));return0;}
#include<stdio.h>//链表结点的定义structNode{int date;//数据域structNode* next;//指针域};intmain(){structNode* list =NULL;//指向链表的指针structNode* tail =NULL;//指向链表尾部元素的指针//输入nint n =0;scanf("%d",&n);int i =0;int m =0;int d =0;//要删除的元素//接收n个数字并尾插到链表中for(i=0; i<n; i++){scanf("%d",&m);structNode* pn =(structNode*)malloc(sizeof(structNode));
pn->date = m;
pn->next =NULL;//插入第一个元素if(list ==NULL){
list = pn;
tail = pn;}else{
tail->next = pn;
tail = pn;}}//接收要删除的元素scanf("%d",&d);//删除链表中指定的元素structNode* cur = list;structNode* prev =NULL;while(cur){//找到了要删除的元素if(cur->date == d){//删除//删除的如果是第一个结点structNode* pd = cur;if(cur == list){
list = list->next;
cur = list;}else{
prev->next = cur->next;
cur = prev->next;}free(pd);
n--;}else{
prev = cur;
cur = cur->next;}}//输出printf("%d\n", n);
cur = list;while(cur){printf("%d ", cur->date);
cur = cur->next;}//释放链表
cur = list;structNode* del =NULL;while(cur){
del = cur;
cur = cur->next;free(del);}
list =NULL;return0;}
1、在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>int FindNumber(int arr[4][4], int k){ int i = 0; for (i = 0; i < 4; i++) { int j = 0;