上面兩幅圖中有六道經典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()效果圖: