1.打印0~100000之間的水仙花數(shù)(自冪數(shù))
#include<stdio.h>
#include<math.h>
int main()
{
int i = 0;
for (i = 0; i < 100000; i++)
{
//判斷i是否為水仙花數(shù)(自冪數(shù))
//1.判斷i有幾位數(shù)
int n = 1;
int tmp = i;
int sum = 0;
while (tmp/=10)
{
n++;
}
//2.計(jì)算出i的每位的n次方
tmp = i;
while (tmp)
{
sum += pow(tmp % 10, n);
tmp /= 10;
}
//3.判斷i是否為水仙花數(shù)
if (i == sum)
{
printf("%d ", i);
}
}
return 0;
}
2.喝汽水,一瓶汽水1元,兩個(gè)空瓶換一瓶汽水,20元可以喝多少汽水
int main()
{
int money = 0;
int total = 0;
int empty = 0;
scanf("%d", &money);
//買回來的汽水喝掉
total = money;
empty = money;
//換回來的汽水
while (empty >= 2)
{
total += empty / 2;
empty = empty / 2 + empty % 2;
}
printf("total: %d", total);
return 0;
}
3.設(shè)計(jì)代碼,實(shí)現(xiàn)使數(shù)組中所有奇數(shù)元素都位于偶數(shù)元素左邊
move(int arr[], int sz)
{
int left = 0;
int right = sz - 1;
while (left < right)
{
while (left < right&&arr[left] % 2 == 1)//左邊找偶數(shù)
{
left++;
}
while (left < right&&arr[right] % 2 == 0)//右邊找奇數(shù)
{
right--;
}
if (left < right)//交換
{
int tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
left++;//左邊下一個(gè)數(shù)
right--;//右邊下一個(gè)數(shù)
}
}
}
int main()
{
int arr[] = { 1, 3, 7, 10, 6, 1, 3, 5, 3 };
int sz = sizeof(arr) / sizeof(arr[0]);
move(arr, sz);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
4.打印楊輝三角:
int main()
{
int arr[10][10] = { 0 };
int i = 0;
int j = 0;
for (i = 0; i < 10; i++)
{
for (j = 0; j <10; j++)
{
if (j == 0)
{
arr[i][j] = 1;
}
if (i == j)
{
arr[i][j] = 1;
}
if (i>=2 && j >= 1)
{
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
}
}
for (i = 0; i < 10; i++)
{
for (j = 0; j <= i; j++)
{
printf("%d ", arr[i][j]);
}
printf(" ");
}
return 0;
}
5.猜兇手:
int main()
{
int killer = 0;
for (killer = 'a'; killer <= 'd';killer++)
if (((killer != 'a') + (killer == 'c') + (killer == 'd') + (killer != 'd')) == 3)
{
printf("killer:%c ", killer);
}
return 0;
}
6.猜名次
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
for (a = 1; a <= 5; a++)
{
for (b = 1; b <= 5; b++)
{
for (c = 1; c <= 5; c++)
{
for (d = 1; d <= 5; d++)
{
for (e = 1; e <= 5; e++)
{
if ((b == 2) + (a == 3) == 1 &&
(b == 2) + (e == 4) == 1 &&
(c == 1) + (d == 2) == 1 &&
(c == 5) + (d == 3) == 1 &&
(e == 4) + (a == 1) == 1 )
if(a*b*c*d*e==120)
printf("a=%d b=%d c=%d d=%d e=%d ", a, b, c, d, e);
}
}
}
}
}
return 0;
}
7.實(shí)現(xiàn)一個(gè)函數(shù),可以左旋字符串中k個(gè)字符
#include<string.h>
void left_move(char a[], int sz,int k)
{
while (k)
{
int i = 0;
int tmp = 0;
tmp = a[i];
for (i = 0; i < sz-2; i++)
{
a[i] = a[i + 1];
}
a[sz - 2] = tmp;
k--;
}
}
int main()
{
char a[] = "ABCD";
int sz = 0;
int k = 0;
sz = sizeof(a) / sizeof(a[0]);
scanf("%d", &k);
left_move(a, sz,k);
printf("%s", a);
return 0;
}
8.判斷char arr2[] = "cdefab";是否是char arr1[30] = "abcdef";旋轉(zhuǎn)后的字符串
#include<stdio.h>
#include<string.h>
int is_left_move(char* str1, char* str2)
{
int len1 = strlen(str1);
int len2 = strlen(str1);
if (len1 != len2)
{
return 0;
}
//1.在str1后面追加一個(gè)str1
//strcat(str1, str1);strcat函數(shù)無法追加自己
strncat(str1, str1,len); //abcdefabcdef
//2.判斷str2指向的字符串是否是str1指向的字符串的子串
char* ret = strstr(str1, str2);
if (ret == NULL)
{
return 0;
}
else
{
return 1;
}
}
int main()
{
char arr1[30] = "abcdef";
char arr2[] = "cdefab";
int ret = is_left_move(arr1, arr2);
if (ret == 1)
{
printf("Yes ");
}
else
{
printf("No ");
}
return 0;
}
9.楊氏矩陣,要求時(shí)間復(fù)雜度小于o(N)
int FindNum(int arr[3][3],int k,int row, int col)
{
int x = 0;
int y = col - 1;
while (x<=row-1&&y>=0)
{
if (arr[x][y] > k)
{
y--;
}
else if (arr[x][y] < k)
{
x++;
}
else
{
return 1;
}
}
return 0;
}
int main()
{
int arr[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int k = 7;//找7
int ret = FindNum(arr, k, 3, 3);
if (ret == 1)
{
printf("找到了 ");
}
else
{
printf("找不到 ");
}
return 0;
}
設(shè)計(jì)完成,但此函數(shù)無法帶回找到的數(shù)的坐標(biāo);
優(yōu)化代碼:
int FindNum(int arr[3][3],int k,int* px, int* py)
{
int x = 0;
int y = *py - 1;
while (x<=*px-1&&y>=0)
{
if (arr[x][y] > k)
{
y--;
}
else if (arr[x][y] < k)
{
x++;
}
else
{
*px = x;
*py = y;
return 1;
}
}
return 0;
}
int main()
{
int arr[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int k = 7;//找7
int x = 3;
int y = 3;
//返回型參數(shù)
int ret = FindNum(arr, k, &x,&y);
if (ret == 1)
{
printf("找到了,坐標(biāo)為:(%d,%d) ",x,y);
}
else
{
printf("找不到 ");
}
return 0;
}
答案:C
答案:D 數(shù)組地址不能存放在一個(gè)整型指針里
答案:C? print函數(shù)傳入的是一個(gè)二維數(shù)組的首元素地址,這個(gè)二維數(shù)組的首元素是一個(gè)一維數(shù)組,故傳入的是一維數(shù)組的地址,一維數(shù)組地址類型為一維數(shù)組的指針int(*arr)[5]
智力題1.賽馬問題
有36匹馬,6個(gè)跑道,沒有計(jì)時(shí)器,請賽馬確定,三十六匹馬中的前三名
最少需要賽幾次?
智力題2.燒香問題
有一種香,材質(zhì)不均勻,但是每一根燃燒完恰好一小時(shí)
給你兩根香,確定一個(gè)十五分鐘的時(shí)間段
?
0
本文摘自 :https://blog.51cto.com/u