获得徽章 0
异常,模块,包
捕获异常
try:
可能发生错误的代码
except:
如果出现异常执行的代码捕获指定异常
e:就是异常信息
try:
可能发生错误的代码
except NameError as e:
print(xxxxx)捕获多个异常
expect (类型一,类型二...) as e:else
不执行except时执行的
finally
无论如何都要执行的
模块
from 模块名 import 功能名
from time import sleep
print(1)
sleep(5)
print(2)用*导入模块全部功能
from time import *
sleep(5) #直接写as别名
import 模块名 as 别名
import time as tt
tt.sleep(2)
print("hello")模块同名
#模块1代码
def my_test(a,b):
print(a+b)
#模块2代码
def my_test(a,b):
print(a-b)
#导入模块
from my_module1 import my_test
from my_module2 import my_test
#my_test调用的是模块2的函数
my_test(1,1)
捕获异常
try:
可能发生错误的代码
except:
如果出现异常执行的代码捕获指定异常
e:就是异常信息
try:
可能发生错误的代码
except NameError as e:
print(xxxxx)捕获多个异常
expect (类型一,类型二...) as e:else
不执行except时执行的
finally
无论如何都要执行的
模块
from 模块名 import 功能名
from time import sleep
print(1)
sleep(5)
print(2)用*导入模块全部功能
from time import *
sleep(5) #直接写as别名
import 模块名 as 别名
import time as tt
tt.sleep(2)
print("hello")模块同名
#模块1代码
def my_test(a,b):
print(a+b)
#模块2代码
def my_test(a,b):
print(a-b)
#导入模块
from my_module1 import my_test
from my_module2 import my_test
#my_test调用的是模块2的函数
my_test(1,1)
展开
评论
点赞
打卡一个acwing铁路与公路的题解吧
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 410,INF = 0x3f3f3f3f;
int n,m;
int f[N][N],g[N][N];
int floyd(int d[][N])
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
return d[1][n];
}
int main()
{
scanf("%d%d",&n,&m);
memset(f,0x3f,sizeof f);
memset(g,0x3f,sizeof g);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
f[a][b] = f[b][a] =1;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i != j && f[i][j]!=1)
g[i][j] =1;
int res = max(floyd(f),floyd(g));
if(res == INF) res = -1;
printf("%d\n",res);
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 410,INF = 0x3f3f3f3f;
int n,m;
int f[N][N],g[N][N];
int floyd(int d[][N])
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
return d[1][n];
}
int main()
{
scanf("%d%d",&n,&m);
memset(f,0x3f,sizeof f);
memset(g,0x3f,sizeof g);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
f[a][b] = f[b][a] =1;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i != j && f[i][j]!=1)
g[i][j] =1;
int res = max(floyd(f),floyd(g));
if(res == INF) res = -1;
printf("%d\n",res);
return 0;
}
展开
评论
点赞
![[流泪]](http://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/img/jj_emoji_6.dde0d83.png)