每周专业学习周记-4

每周专业学习周记-4

标签(空格分隔): 周记


数据结构篇

#include <stdio.h>
#include <stdlib.h>
int search(int arr[],int len,int key)
{
    int mid,low = 1;                   
	int high = len;
	//printf("%d!",high);
	while(low <= high)
	{
		mid = (low+high)/2;  
		//printf("%d ",mid);
		if(key == arr[mid])  
		{
			return mid+1; 
			break;
		}
		else if(key>arr[mid])             
			low = mid+1;
		else                                      
			high = mid-1;
	}
	return 0;  
}

int BSR(int arr[],int key,int low ,int high)
{
    
    if (low > high)
        return 0;
    int mid = low + (high - low)/2;
    if (arr[mid] == key)
        return mid+1;
    else
    if (key < arr[mid])
        return BSR(arr,key,low,mid-1);
    else
        return BSR(arr,key,mid+1,high);
}
int main()
{
    int key;
    int position1,position2;
    int arr[] = {3,5,7,11,13,15};
    printf("请输入要查找的数:");
    scanf("%d",&key);
    //printf("\n(如果这个数存在我将会返回它所对应的位置,否则返回0)");
    printf("------------------------------------------------\n");
    /*
    //数组查找
    position1 = search(arr,sizeof(arr)/sizeof(arr[0]),key);
    if (position2 == 0)
        printf("这个不存在数组中!(0)");
    else
        printf("这个数在数组中的位置是:%d",position2);
    */
    
    //递归查找
    position2 = BSR(arr,key,0,sizeof(arr)/sizeof(arr[0]));
    if (position2 == 0)
        printf("这个不存在数组中!(0)");
    else
        printf("这个数在数组中的位置是:%d",position2);
        
    //printf("%d",sizeof(arr)/sizeof(arr[0]));
    //用sizeof求一个数组的长度,但是arr是一个整形每个元素占
    //4个字节,所以求数组长度的时候要除4,就是除一个字节大小
     return 0;
}

数据结构题目

以上代码题目分别是:
实现折半查找函数,查找指定元素,若元素在列表中存在,则返回序号,若不存在,则返回0,例如,已知序列“3,5,7,11,13,15”,若查找元素7,则返回3,若查找元素9,则返回0,其它具体要求如下:
1. 待查记录为整数序列;
2. 待查记录从数组下标1处开始存储;
3. 待查记录必须有序。

数据结构小结

这个实验我使用了递归和数组两种方法,复习了以前的数组的做法,和新的递归算法。
折半查找的优点是比较次数少,查找速度快,平均性能好。缺点是要求待查表为有序表,且插入删除困难。数组中的待比较元素一定是要有序的。

Python篇

# 7.1 函数input()函数的工作原理
# message = input("Tell me something,and I will repeat it back to you:")
# print(message)

# 7.7.1 编写清晰的程序
# name = input("Please enter you name: ")
# print("Hello,"+name)

# prompt = "If you tell us who you are,we can personalize the message you see."
# prompt += "\nWhat is your first name? "
# name = input(prompt)
# print("\nHello,"+name+"!")

# 7.1.2 用int()来获取数值输入
# height = input("How tall are you,in inches?")
# height = int(height)

# if height >= 36:
# 	print("\nYou're tall enough to ride.")
# else:
# 	print("\nYou'll be able to ride when you're a little older.")

# 7.1.3 求模运算符
# number = input("Enter a number,and I'll tell you if it's even or odd:")
# number = int(number)

# if number % 2 == 0:
# 	print("The number "+str(number)+" is even.")
# else:
# 	print("The number "+str(number)+" is odd.")	

# 7.1.4 在Python 2.7中获取输入
# 如果使用的是python 2.7 应该使用raw_input获取输入
# 同样Python也将读入的值解读为字符型

# 7-1 汽车租赁
# car = input("请问你想要租什么汽车呢?")
# print("让我看看,如果我能找的到"+car+"的话。")

# 7-2 餐馆订位
# table = input("请问你们一共几位呢?")
# table = int(table)
# if table > 8:
# 	print("对不起先生,我们这没有这么大的空桌了。")
# else:
# 	print("我们还有位置,请这边来。")

# 7-3 10的整数倍
# num = input("请输入一个数,我将告诉你这个数是不是十的整数倍。")
# num = int(num)
# if num % 10 == 0:
# 	print(str(num)+"是十的整数倍。")
# else:
# 	print(str(num)+"不是十的整数倍。")

# 7.2 while循环简介
# for循环用于针对集合中的每个元素的一个代码块,而while循环不断的
# 运行,直到指定的条件不满足为止

# 7.2.1 使用while循环
# current_number = 1
# while current_number <= 5:
# 	print(current_number)
# 	current_number += 1

# 7.2.2 让用户选择何时退出
# prompt = "\nTell me something,and I will repeat it back to you:"
# prompt += "\nEnter 'quit' to end the program."
# message = ""

# while message != 'quit':
# 	message = input(prompt)

# 	if message != 'quit':
# 		print(message)

# 7.2.3 使用标志
# prompt =  "\nTell me something,and I will repeat it back to you:"
# prompt += "\nEnter 'quit' to end the program."

# active = True
# while active:
# 	message = input(prompt)

# 	if message == 'quit':
# 		active = False
# 	else:
# 		print(message)

# 7.2.4 使用break退出循环
# prompt = "\nPlease enter the name of a city you have visited:"
# prompt += "\nEnter 'quit' when you are finished."

# while True:
# 	city = input(prompt)

# 	if city == 'quit':
# 		break
# 	else:
# 		print("I'd love to go to "+city.title()+"!")

# 7.2.5 在循环中使用continue
# curren_number = 0
# while curren_number < 10:
# 	curren_number += 1
# 	if curren_number % 2 != 0:
# 		print(curren_number)

# 7.2.6 避免无限循环
# x = 1
# while x < 5:
# 	print(x)
# 	x += 1

# while 循环必须有一个能够控制循环退出的条件,否则陷入无限循环

# 7-4  披萨配料
# message = "请输入要添加的披萨配料:"
# enter = ""
# while enter != 'quit':
# 	enter = input(message)
# 	print("我们将会为你在披萨中添加:"+enter)

# 7-5 电影票
# message = "我们根据年龄不同收取的费用也不同,请告诉我你的年龄:"
# age = ""
# while age != '0':
# 	age = input(message)
# 	age = int(age)
# 	if age < 3:
# 		print("免费")
# 	elif age <= 12:
# 		print("票价为12元")
# 	else:
# 		print("票价为15元")
# 	print("-------------------")

# 7-6 三个出口
# 披萨配料
# message = "请输入你要添加的配料:"
# active = ""
# while True:
# 	active = input(message)
	
# 	if active == 'quit':
# 		break
# 	print("我们将为你添加:"+active)

# 电影票
# message = "我们会根据年龄不同收取的费用也不同,请你告诉我你的年龄:"
# active = ""
# while True:
# 	active = input(message)
# 	if str(active) == 'quit':
# 		break
# 	active = int(active)
# 	if active < 3:
# 		print("免费")
# 	elif active <= 12:
# 		print("12元的票价")
# 	else:
# 		print("15元票价")

# 7.3.1 在列表之中移动元素
# unconfirmed_users = ['aliece','brain','candace']
# confirmed_users = []
# while unconfirmed_users:
# 	current_user = unconfirmed_users.pop()
# 	print("Verifying user: "+current_user)
# 	confirmed_users.append(current_user)
# print("\nThe following users have been confirmed:")
# for confirmed_user in confirmed_users:
# 	print(confirmed_user)

# 7.3.2 删除包含特定值的所有元素列表
# pets = ['dog','cat','goldfish','cat','ribbit','cat']
# print(pets)

# while 'cat' in pets:
# 	pets.remove('cat')
# print(pets)

# 7.3.3使用用户输入来填充字典
# respones = {}
# while True:
# 	name = input("请问你的名字是什么:")
# 	respone = input("你常去爬的那座山叫什么名字:")

# 	respones[name] = respone

# 	repeat = input("请问你是否还要继续输入?(yes/no)")
# 	if repeat == 'no':
# 		break
# print("\n---Poll Results---")
# for name,respone in respones.items():
# 	print(name+"最常爬的山是:"+respone) 

# 7-8 熟食店
# sandwich_orders = ['chicken sandwich','vegetable sandwich','banana sandwich']
# finished_sandwichs = []

# while sandwich_orders :
# 	sandwich = sandwich_orders.pop()
# 	print("I made your "+sandwich)

# 	finished_sandwichs.append(sandwich)
# print("\n-----Comptele----")
# for finished_sandwich in finished_sandwichs:
# 	print(finished_sandwich)

# 7-9 五香烟熏牛肉买完了
# print("五香烟熏牛肉都买完了")
# sandwich_orders = ['五香烟熏牛肉三明治','五香烟熏牛肉三明治','火腿三明治','鸡肉三明治','五香烟熏牛肉三明治','蔬菜水果三明治']
# while '五香烟熏牛肉三明治' in sandwich_orders:
# 	sandwich_orders.remove('五香烟熏牛肉三明治')
# print(sandwich_orders)

# 7-10 梦想的度假胜地
message = {}
while True:
	name = input("What your name?  ")
	question1 = "If you could visit one place in the world"
	question1 += ",where would you go?  "
	place = input(question1)
	message[name] = place

	question2 = input("\nWould you like another respond?(yes/no)  ")
	if question2 == 'no':
		break
print("\n-----Conplete-----")
for name,place in message.items():
	print(name.title()+" want to travel to "+place.title()+".")

Python小结

说明:上面的代码都用#注释掉的原因是input()每次输入都会停住等待输入,因为我把这章的代码都写到了一块,所以不注释掉之前的代码块就会影响到后面代码块的测试。
在本章中学习到了如何使用input()来让用户提供信息,如何处理文本和数字输入,以及如何使用while循环让程序按用户的要求不断地运行,多种控制while循环方式:设置活动标志、使用break语句以及continue语句,如何使用while循环在列表之间移动元素,以及如何使用列表中删除所有包含特定值的元素,如何结合使用while循环和字典。

posted @ 2020-05-31 23:15  稍微有点好看  阅读(244)  评论(0编辑  收藏  举报