大华笔试

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Strive_0902/article/details/82663737

1 给定一个长度为n的数组data,计算出data[i]-data[j]的最大值,0<=i<j<=n

 分析:

对于减法的计算,要是差值最大,必须要让被减数尽可能大,减数尽可能小,所以,把数组遍历一遍,存下当前最大值,并用最大值减去新进来的元素,保存当前最大的差值,更新当前最大值和当前最大的差值

def max_min(array):
    max_value = array[0]
    res = array[0]-array[1]
    for each in array[1:]:
        if each>max_value:
            max_value = each
        if res < max_value - each:
            res = max_value - each
    return res


def old_way(array):
    res = 0
    for i in range(len(array)-1):
        for j in range(i+1,len(array)):
            if array[i]-array[j]>res:
                res = array[i]-array[j]
    return res

if __name__ =='__main__':
    x = [int(i) for i in input().split()]
    print(max_min(x))
    print(old_way(x))

对比两种算法

发现第一种方法算法复杂为O(n)

第二种方法的时间复杂度为O(n^2)

2 字符串反转 'i am a student' 转化成 ' student a am i'

python实现:列表转化成字符串需要" ".join(list)

if __name__ == "__main__":
    s = input().split()
    def revstring(s):
        if not s:
            return 0
        else:
            res = ""
            s.reverse()
            res = " ".join(s)
        return res
        

结果如下

runfile('C:/Users/xu/Desktop/python/xiushiqi.py', wdir='C:/Users/leilxu/Desktop/python')

i am a student

s
Out[12]: ['i', 'am', 'a', 'student']

print(revstring(s))
student a am i

C语言实现

#include<stdafx.h>
#include <stdio.h>
#include <string.h>

//反转一个单词[from,to]区间内的字母
void reserveString(char arr[], int from, int to)
{
	while (from < to)
	{
		char tmp = arr[from];
		arr[from] = arr[to];
		arr[to] = tmp;
		from++;
		to--;
	}
}

//反转一句话,以'\0'结尾
void reserve(char ch[], int len)
{
	int i = 0;
	int from = 0;
	int to = 0;
	while (i <= len)//数组中每个字符都要判定,包括'\0'
	{
		if (ch[to] == ' ' || ch[to] == '\0')
		{
			reserveString(ch, from, to - 1);   //先反转每个单词,[from,to-1]
			from = ++to;                    //寻找下一个单词。
		}
		else
		{
			to++;
		}
		i++;
	}
	reserveString(ch, 0, len - 1);  //再整体反转
}

int main()
{
	char ch[] = "i am a student.";
	printf("%s\n", ch);
	reserve(ch, strlen(ch));
	printf("%s\n", ch);
	return 0;
}

大华电话面试

1 C语言里面的野指针,垃圾内存啥的

2 存储过程和触发器

3 修饰器和类的区别

猜你喜欢

转载自blog.csdn.net/Strive_0902/article/details/82663737
今日推荐