博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法基础
阅读量:7008 次
发布时间:2019-06-28

本文共 5872 字,大约阅读时间需要 19 分钟。

  hot3.png

围圈报数

    题目:m个人围成一圈,从1开始报数,报到n的人退出,下一位继续从1开始报数,问最后剩下的是几号?

    实现思路:

  • 初始化一个大小为m的数组,默认值都为1,代表该位置有人

  • 报数开始,位置有人时,报数count++

  • 当报数count==n时,该位置设为0,count重置为0,退出人数quitPerson+1

  • 当报数至最后位置的人时,数组下标重置为0     

  • 当退出人数quitPerson等于m-1时,报数循环结束

    实现代码:

#include 
int main(void) {    int persons, number, index = 0;    // 围圈人数、退出时报数、数组下标    scanf("%d%d", &persons, &number);    int quitPerson = 0, count = 0;     // 退出人数、目前报数    // 初始化数组    int arr[persons];    for (int i = 0; i < persons; i++) {        arr[i] = 1;    }            // 退出人数不等于persons-1    while (quitPerson != persons-1) {        // 如果当前位置有人,报数+1        if (arr[index]) {            count++;        }        // 目前报数满足退出要求时,位置清人,重置目前报数,退出人数加1        if (count == number) {            a[index] = 0;            count = 0            quitPerson++;        }        // 下个报数位置        index++;        // 已报数到最后时,下标置为0,即从头再报        if (index == persons) {            index = 0;        }    }            // 打印最后的位置    for (int i = 0; i < persons; i++) {        if (a[i]) {            printf("%d\n", i+1);        }    }    return 0;}

喝汽水

    题目:1元能买一瓶汽水,喝完后n个空瓶可以换一瓶汽水,假如现在拥有钱数m元,问最后能喝到多少瓶汽水?

    思路与上一题基本一致:

  • 存在未喝汽水drinks时,进入循环

  • 喝完一支汽水,非空瓶drinks-1,空瓶nullBottle+1

  • 当空瓶数等于兑换条件时,空瓶数nullBottle置0,非空瓶drinks+1,总瓶数sumOfBottle+1

  • 当非空瓶drinks为0时,退出循环

    实现代码:

#include 
int sumOfSoftDrink(int money, int n) {    int drinks = money;    // 非空瓶    int sumOfBottle = money, nullBottle = 0;    // 总瓶数、空瓶数    while(drinks) {        drinks--;        nullBottle++;        // 满足兑换条件        if (nullBottle == n) {            nullBottle = 0;            drinks++;            sumOfBottle++;        }    }    return sumOfBottle;}int main(void) {    int money, n;    scanf("%d%d", &money, &n);    printf("%d\n", sumOfSoftDrink(money, n));    return 0;}

螺旋矩阵

    题目:从终端接收m和n,如输入:5 16,实现以下效果:

                               1   2  3   4  5

                             16   0  0   0  6

                             15   0  0   0  7

                             14   0  0   0  8

                             13 12 11 10  9

    这道题的实现思路,自己感觉有点笨,不过看起来比较直观,大概可以理解为一只往迷宫中心走的老鼠,只要一碰到墙就转弯:

  • 初始化m*m的数组,全部置0

  • 设置4个方向标识位,分别是right、down、left和up,代表不同的行走方向

  • 判断下一步是否遇到墙,是则转弯

    实现代码:

#include 
#define RIGHT 1#define DOWN  2#define LEFT  3#define UP    4int main(void) {    int n, max;    scanf("%d%d", &n, &max);    // 初始化数组    int a[n][n];    for (int i = 0; i < n; i++) {        for (int j = 0; j < n; j++) {            a[i][j] = 0;        }    }    // 初始为向右走    int start  = 1, direction = RIGHT;    int row = 0, col = 0;    while(start <= max) {        // 赋值        a[row][col] = start;        switch(direction) {            case RIGHT: {    // 向右                col++;                if (col==n || a[row][col]){                    direction = DOWN;                    col--;                    row++;                }                break;               }            case DOWN: {    // 向下                row++;                if (row==n || a[row][col]) {                    direction = LEFT;                    row--;                    col--;                }                break;               }            case LEFT: {    // 向左                col--;                if (col==-1 || a[row][col]) {                    direction = UP;                    row--;                    col++;                }                break;               }            case UP: {    // 向上                row--;                if (row==-1 || a[row][col]) {                    direction = RIGHT;                    col++;                    row++;                }                break;               }        }        // 自增赋值         start++;    }     // 打印最终数组    for (int i = 0; i < n; i++) {        for (int j = 0; j < n; j++) {             printf("%3d", a[i][j]);         }        printf("\n");    }    return 0;}

分解整数

    题目:输入一个整型参数,如果这个整数可以分解成连续的自然数相加,则把所有的可能输出,否则输出无法分解。

如:15=1+2+3+4+5,15=4+5+6,15=7+8。

    思路:

  • 当i<=number/2时,进行循环

  • 当i+(i+1)...+(i+n)等于number时,输出该范围的数

    实现代码:

#include 
// 打印结果函数void printSplitNumber(int min, int max) {    for (int i = min; i < max; i++) {        printf("%d+", i);    }    printf("%d\n", max);}// 分解整数函数void splitNumber(int num) {    int n = (num+1)/2;    int i = 1, flag = 0;    while (i <= n) {        int sum = 0;                        // 每次循环都初始化和        for (int j = i; j <= n; j++) {            sum += j;            if (sum == num) {               // 当和等于number时                flag = 1;                printf("%d=", num);                printSplitNumber(i, j);                break;                      // 直接break,无须继续for循环            }         }        i++;    }    // 是否无法分解    if (!flag)        printf("无法分解\n");}int main(void) { int number; scanf("%d", &number); splitNumber(number); return 0;}

面试题

返回连续字符串

    题目:输入一个字符串,然后返回连续最大的字符串。

    比如:

    1,3,3,3,4,4,4,4,4,0,0,0,4,4,4,4:此例中由5个连续的4为最大连续子串,返回结果为44444;

    1,3,3,3,5,5,5,5,5,4,4,4,4,4,0,0,0,0,0,0,4,4,4,4:返回000000;

    听说是一道OC的面试题,思路:

  • 通过string.h库中的strtok将字符串进行分割,并把分割后的字符串保存到一个数组

  • 遍历数组,统计相同元素的个数,输出个数最多的元素

#include 
#include 
int main(void) {    char str[64];    scanf("%s", str);    int len = strlen(str), i = 0;    int count = 0, max = 0, index;    char *p = str;    char *numbers[32];    // 分割字符串    while(1) {        p = strtok(p, ",");        if (!p)            break;        numbers[i++] = p;        p = NULL;    }    // 找出个数最多的元素    int j = 0;    while (j < i-1) {        if (!strcmp(numbers[j], numbers[j+1]))            count++;        else            count = 0;        if (max < count) {            max = count;            index = j;        }        j++;    }    // 打印结果    for (i = index-max+1; i <= index+1; i++)        printf("%s", numbers[i]);      return 0;}

将字符串倒序

    题目:将"You are a cheap man"倒叙输出:"man cheap a are You"。

    思路:一样是通过strtok函数将字符串进行分割,保存到一个数组,再逆序输出。

    实现代码跟上面的基本差不多,修改下打印代码就行了。这里只贴Python代码,好吧,其实只要一行代码就够了。

>>> str = "You are a cheap man">>> ' '.join(str.split(' ')[::-1])'man cheap a are You'

    这题的背景有点狗血,据说是面试官丢给一位刚培训出来的应聘者,说只要答出来了,就给7K,最后应聘者没有答出来~

    Python短短的一行代码,不禁让我想起那句话:人生苦短,我用Python。

转载于:https://my.oschina.net/ljunb/blog/495827

你可能感兴趣的文章