每周专业学习周记-2
每周专业学习周记-2
标签(空格分隔): 周记
数据结构篇
#include<stdio.h>
#include<limits.h>
#include<stdlib.h>
#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20
#define Error -1
#define OK 1
typedef enum {DG, DN,UDG,UDN} GraphKind;
typedef int AdjType;
typedef struct {
AdjType adj;
//OtherInfo info;
} ArcNode;
/*图(邻接矩阵)的定义*/
typedef char VertexData;
typedef struct{
GraphKind kind;
int vexnum, arcnum;
VertexData vertex[MAX_VERTEX_NUM];
ArcNode arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
}AdjMatrix;
int LocateVex(AdjMatrix *G, VertexData v)
{
int j=Error, k;
for(k=0; k<G->vexnum; k++)
if(G->vertex[k]==v)
{
j=k;
break;
}
return j;
}
void CreateDN(AdjMatrix *G)
{
int i, j, k;
AdjType weight;
VertexData v1, v2;
printf("请输入图的顶点数目:");
scanf("%d", &G->vexnum );
printf("请输入弧的数目:");
scanf("%d", &G->arcnum );
for(i=0; i<G->vexnum; i++)
for(j=0; j<G->vexnum; j++)
G->arcs[i][j].adj=INFINITY;
printf("请输入顶点信息(直接连续输入,不要使用空格或回车间隔,除非空格或回车是顶点存储的元素):");
fflush(stdin);
for(i=0; i<G->vexnum; i++)
scanf("%c", &G->vertex[i]);
printf("构建邻接矩阵,请输入一条弧的起点、终点与权值,例如\"a,b,10\"\n");
for(k=0; k<G->arcnum; k++)
{
printf("第%d-%d条:", G->arcnum, k+1);
fflush(stdin);
scanf("%c,%c,%d", &v1, &v2, &weight);
i=LocateVex(G, v1);
j=LocateVex(G, v2);
G->arcs[i][j].adj=weight;
}
}
int main()
{
AdjMatrix G;
G.kind=DN;
CreateDN(&G);
int i, j;
printf("输出图的顶点:\n");
for(i=0; i<G.vexnum; i++)
printf("%c", G.vertex[i]);
printf("\n");
printf("输出图的邻接矩阵:\n");
for(i=0; i<G.vexnum; i++)
{ for(j=0; j<G.vexnum; j++)
printf("%14d", G.arcs[i][j].adj);
printf("\n");
}
}
数据结构小结
以上代码题目分别是:
采用邻接矩阵存储结构,实现以下赋权有向图的实现与创建,具体要求如下:
(1)要求定义函数,在函数中创建赋权有向图的邻接矩阵
(2)要求输入n个顶点、e条边与各边的权值,n、e及权值由控制台输入
Python篇
#5.1 一个简单的示例
cars = ['audi','bmw','subaru','toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
#5.2 测试条件
car = 'bmw'
if car == 'bmw':
print("True")
else:
print("False")
# Python检查是否相等时是区分大小写的
#如果大小写无关紧要可以使用lower()和upper()将格式转换为统一小写或大写再进行比较
car = 'Audi'
if (car.upper() == 'audi'):
print("True")
#5.2.3 检查是否不相等
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("Hold the anchovies!")
#5.2.4 比较数字
answer = 17
if answer != 42:
print("That is not the correct answer.Please try again!")
#5.2.5 检查多个条件
# 1、使用and检查多个条件
age_0 = 22
age_1 = 18
if (age_0 >= 21 and age_1 >= 21):
print("True")
else:
print("False")
age_1 = 22
if (age_0 >= 21 and age_1 >= 21):
print("True")
else:
print("False")
# 5.2.6 检查特定值是否在列表中
requested_toppings = ['mushrooms','onions','pineapple']
if ('mushrooms' in requested_toppings):
print("True")
else:
print("False")
if ('peeperoni' in requested_toppings):
print("True")
else:
print("Flase")
# 5.2.8 布尔表达式
game_active = True
can_edit = False
# 5-1 条件测试
car = 'subaru'
print("Is car == 'audi'? I predict True.")
print(car == 'audi')
print("\nIs car == 'audi'? I predict True")
print(car == 'audi')
print("\nA == B?")
A = 'apple'
B = 'banana'
print(A == B)
B = 'apple'
print(A == B)
# 5-2 更多的条件测试
testa = 'trandion'
testb = 'Trandion'
print("Are testa and teatb equal?")
if (testa == testb):
print("True")
else:
print("False")
if (testa.lower() == testb.lower()):
print("True")
else:
print("False")
print()
A = 1234
B = 5678
C = 1234
if (A == B):
print("OK")
else:
print("ERROR")
if (A > B):
print("OK")
else:
print("ERROR")
if (A >= B):
print("OK")
else:
print("ERROR")
if (A > B or B > C):
print('YES')
else:
print('NO')
if (A>B and B>C):
print('YESYES')
else:
print('NONO')
A = ['a','b','c','d','e']
if ('b' in A):
print("b in A")
else:
print("b not in A")
if ('f' not in A):
print("f not in A")
else:
print("f in A")
# 5.3.2 简单的if语句
age = 19
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
# if-else语句
age = 17
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
else:
print("Sorry,you are too young to vote.")
print("Please register to vote as soon as you trun 18!")
# 5.3.3 if-elif-else 结构
age = 12
if age < 4:
print("Your admission cost is $0.")
elif age < 18:
print("Your admission cost is $5.")
else:
print("Your admission cost is $10.")
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
else:
price =10
print("Your admission cost is $"+str(price)+".")
# 5.3.4 使用多个elif代码块
age = 33
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
else:
price = 5
print("Your admission cost is $"+str(price)+".")
# 5.3.5 省略else代码块
age = 12
if age < 4:
price = 0
elif age <18:
price = 5
elif age < 65:
price = 10
elif age >= 65:
price = 5
print("Your admission cost is $"+str(price)+".")
# 5.3.6 多个测试条件
# if-elif-else只有一个条件满足的情况
# 有时候会有多个条件为true的情况,这时候就可以用不带elif-else的简单if语句块
requested_toppings = ['mushrooms','extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
print("Adding mushrooms.")
if 'extra cheese' in requested_toppings:
print("Adding mushrooms.")
print("\nFinished making your pizza!")
# 5-3 外星人的颜色 1
alien_color = "green"
if alien_color == "green":
print("Congratulations! you got five points.")
if alien_color == "red":
print("Congratulations! you got five points.")
# 5-4 外星人的颜色 2
alien_color = "green"
if alien_color == "green":
print("Congratulations! you got five points.")
else:
print("Congratulations! you got ten points.")
if alien_color == "red":
print("Congratulations! you got five points.")
else:
print("Congratulations! you got ten points.")
print()
# 5-5 外星人的颜色 3
alien_color = "green"
if alien_color == "green":
print("Congratulations! you got five points.")
elif alien_color == "yellow":
print("Congratulations! you got ten points.")
else:
print("Congratulations! you got fifth points.")
alien_color = "yellow"
if alien_color == "green":
print("Congratulations! you got five points.")
elif alien_color == "yellow":
print("Congratulations! you got ten points.")
else:
print("Congratulations! you got fifth points.")
alien_color = "red"
if alien_color == "green":
print("Congratulations! you got five points.")
elif alien_color == "yellow":
print("Congratulations! you got ten points.")
else:
print("Congratulations! you got fifth points.")
# 5-6 人生的不同阶段
age = 2
if age < 2:
print("She's a baby.")
elif age < 4:
print("She's toddlering.")
elif age < 13:
print("She's a child.")
elif age < 20:
print("She's a teenager.")
elif age < 65:
print("She's an adult.")
else:
print("She is a old man.")
print()
# 5.4.1 检查特殊元素
requested_toppings = ['mushrooms','green peppers','extra cheese']
for requested_topping in requested_toppings:
print("Adding "+requested_topping+".")
print("\nFinished making your pizza.")
print()
requested_toppings = ['mushrooms','green peppers','extra cheese']
for requested_topping in requested_toppings:
if requested_topping == 'green peppers':
print("Sorry we are out of green peppers right now.")
else:
print("Adding "+requested_topping+".")
print("\nFinished making your pizza.")
# 5.4.2 确定列表不是空的
requested_toppings = []
if requested_topping:
for requested_topping in requested_toppings:
print("Adding "+requested_topping+".")
print("Finished making your pizza.")
else:
print("Are you sure you want a plain pizza?")
# 5.4.3 使用多个列表
available_toppings = ['mushrooms','olives','green peppers',
'pepperoni','pineapple','extra cheese']
requested_toppings = ['mushrooms','french fries','extra cheese']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print("Adding "+requested_topping+".")
else:
print("Sorry,we don't have "+requested_topping+".")
print("\nFinished making you pizza.")
# 5-8 以特殊方式和管理员打招呼
users = ['Lili','Lucy','Michale','mike','john','admin']
for user in users:
if user == 'admin':
print("Hello admin,would you like to see a status report?")
else:
print("Hello "+user+",thank you for logging in again.")
# 5-9 处理没有用户的情形
users = []
if users:
for user in users:
if user == 'admin':
print("Hello admin,would you like to see a status report?")
else:
print("Hello "+user+",thank you for logging in again.")
else:
print("We need to find some users!")
# 5-10 检查用户名
current_users = ['Lili','Lucy','Michale','mike','john','admin']
new_users = ['Lili','lucy','Herbert','Baron','Danae']
for user in new_users:
if user.title() or user.lower() or user.upper() in current_users:
print(user+" Sorry,this user name has been used.")
else:
print(user+" This user name has not been used.")
# 5-11 序数
number = range(1,10)
for val in number:
if val == 1 or val == 2 or val ==3:
print(val)
else:
print(str(val)+"th")
Python小结
在本章中,学习了如何编写结果要么为True要么为False的条件测试。学习了如何编写简单的if语句、if-else语句和if-elif-else语句结构,在程序中,使用了这些结构来测试特定的条件,以确定这些条件是否满足。学习了在如何利用高效的for循环的同时以不同于其他元素的方式对特定的列表元素进行处理。再次学习了Python就代码格式方面提出的建议,这可确保即便你边写的程序越来越复杂,其代码依然易于阅读和理解。