1.打印0~100000之间的水仙花数(自幂数)
#include<stdio.h>
#include<math.h>
int main()
{
int i = 0;
for (i = 0; i < 100000; i++)
{
//判断i是否为水仙花数(自幂数)
//1.判断i有几位数
int n = 1;
int tmp = i;
int sum = 0;
while (tmp/=10)
{
n++;
}
//2.计算出i的每位的n次方
tmp = i;
while (tmp)
{
sum += pow(tmp % 10, n);
tmp /= 10;
}
//3.判断i是否为水仙花数
if (i == sum)
{
printf("%d ", i);
}
}
return 0;
}
2.喝汽水,一瓶汽水1元,两个空瓶换一瓶汽水,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.设计代码,实现使数组中所有奇数元素都位于偶数元素左边
move(int arr[], int sz)
{
int left = 0;
int right = sz - 1;
while (left < right)
{
while (left < right&&arr[left] % 2 == 1)//左边找偶数
{
left++;
}
while (left < right&&arr[right] % 2 == 0)//右边找奇数
{
right--;
}
if (left < right)//交换
{
int tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
left++;//左边下一个数
right--;//右边下一个数
}
}
}
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.实现一个函数,可以左旋字符串中k个字符
#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";旋转后的字符串
#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后面追加一个str1
//strcat(str1, str1);strcat函数无法追加自己
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.杨氏矩阵,要求时间复杂度小于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;
}
设计完成,但此函数无法带回找到的数的坐标;
优化代码:
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;
//返回型参数
int ret = FindNum(arr, k, &x,&y);
if (ret == 1)
{
printf("找到了,坐标为:(%d,%d) ",x,y);
}
else
{
printf("找不到 ");
}
return 0;
}
答案:C
答案:D 数组地址不能存放在一个整型指针里
答案:C print函数传入的是一个二维数组的首元素地址,这个二维数组的首元素是一个一维数组,故传入的是一维数组的地址,一维数组地址类型为一维数组的指针int(*arr)[5]
智力题1.赛马问题
有36匹马,6个跑道,没有计时器,请赛马确定,三十六匹马中的前三名
最少需要赛几次?
智力题2.烧香问题
有一种香,材质不均匀,但是每一根燃烧完恰好一小时
给你两根香,确定一个十五分钟的时间段
0
本文摘自 :https://blog.51cto.com/u