上面两幅图中有六道经典C语言面试题,真的可以认真看看!!!
直接贴代码:
#include<stdio.h>
#include<windows.h>
#include<assert.h>
void bit_set(unsigned char *p_data, unsigned char position, int flag)
{
assert(p_data!=NULL);
if ((position >= 1) && (position <= 8))//指定位置1-8
{
if (0 == flag)
{
(*p_data) &= ~(1 << (position - 1));
}
else if (1 == flag)
{
(*p_data) |= (1 << (position - 1));
}
}
}
void test()
{
unsigned char i = 15;
bit_set(&i, 3, 0);
printf("%d\n", i);
}
void rightloopmove(char *str, unsigned short steps)
{
assert(str != NULL);
int i = 0;
int j = 0;
int len = strlen(str) - 1;
char tmp = 0;
while (steps--)
{
tmp = str[len];
for (j = len - 1; j >= 0; j--)
{
str[j + 1] = str[j];
}
str[0] = tmp;
}
}
void test1()
{
char p[] = "abcdef";
rightloopmove(p, 2);
printf("%s\n", p);
}
enum State
{
legal,
illegal
};
enum State state = illegal;
int my_atoi(const char *str)
{
assert(str);
int flag = 1;
long long ret = 0;
if (*str == '\0')
{
return 0;
}
while (isspace(*str))
{
str++;
}
if (*str == '+')
{
str++;
}
if (*str == '-')
{
flag = -1;
str++;
}
while ((*str!='\0')&&(isdigit(*str)))
{
state = legal;
ret = ret * 10 + flag*(*str - '0');
if ((ret > INT_MAX) || (ret < INT_MIN))
{
state = illegal;
return 0;
}
str++;
}
if ((*str!='\0')&&!(isdigit(*str)))
{
state = illegal;
}
return (int)ret;
}
void test2()
{
int ret = my_atoi(" -123");
if (state == legal)
{
printf("%d\n", ret);
}
}
#define tableSize 256 //创建一个哈希表,因为ASCII码表中只有0~255共256个字符。
char First_Char(char* pString,int num)
{
assert(pString);
int hashTable[tableSize] = {0};
//确定字符串中每个字符出现的次数
char* pHashKey = pString;
while (*(pHashKey) != '\0')
{
hashTable[*(pHashKey++)]++;
}
//找到字符串中只出现k次的那个字符
pHashKey = pString;
while (*pHashKey != '\0')
{
if (hashTable[*pHashKey] == num)
return*pHashKey;
pHashKey++;
}
//如果这个字符串为空,或者字符串中的每个字符都至少出现k+1次
return 0;
}
void test3()
{
char str[1000]; //这个函数是在字符串特别大时建议使用,故定义一个大小为1000的数组
printf("请输入字符串:");
gets(str);
int k = 3;
if (First_Char(str,k) == 0)
printf("输入字符串中没有找到第一个只出现k次的字符!\n");
else
printf("输入字符串中第一个只出现k次的字符为:%c\n", First_Char(str,k));
}
int reverse(int num)
{
int ret = 0;
while (num)
{
ret = ret * 10 + num % 10;
num = num / 10;
}
return ret;
}
void test4()
{
int num = -123;
printf("%d\n",reverse(num));
}
void leftloopmove(int *str, unsigned short steps,int sz)
{
assert(str != NULL);
int i = 0;
int j = 0;
int len = sz - 1;
int tmp = 0;
while (steps--)
{
tmp = str[0];
for (j = 0; j < len; j++)
{
str[j] = str[j+1];
}
str[len] = tmp;
}
}
int New_Binary_Search(int arr[], int num, int sz)
//使用二分查找,每次都把数组分成两半,判别哪一半是排过序的。
//如果待查元素在排过序的那一半,那么用二分查找。
//如果在另一半,那么我们递归地在这一半里继续查找。
{
int left = 0;
int right = sz - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (arr[mid] == num)
{
return mid;
}
if (arr[left] <= arr[mid])
{ // 左边是排好序的
if (arr[left] <= num && num < arr[mid])
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
else
{
if (arr[mid] < num && num <= arr[right])
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
}
return -1;
}
void test5()
{
int arr[] = { 3, 4, 5, 6, 7,8, 9, 1, 2 };
int i = 0;
int sz = (sizeof(arr) / sizeof(arr[0]));
//leftloopmove(arr, 2, sz);
for (i = 0; i < sz; i++)
{
printf("%d",arr[i]);
}
printf("\n");
int ret = New_Binary_Search(arr, 1, sz);
if (ret != -1)
{
printf("下标为%d",ret);
}
else
{
printf("没找到!");
}
}
int main()
{
//test();////指定位置1或置0
//test1();//右旋
//test2();//模拟实现atoi
//test3();//首次出现K次
//test4();//翻转整型
test5();//旋转已排序数组查找数字
system("pause");
return 0;
}
test()效果图:
test1()效果图:
test2()效果图:
test3()效果图:
test4()效果图:
test5()效果图: