1.python代ç 大å
¨ç®åï¼
2.怎样通过三个按钮控制页面上的源码源码一段文字放大,缩小,以及恢复到原始大小的例文源代码
3.[stl 源码分析] std::list::size 时间复杂度
python代ç å¤§å ¨ç®åï¼
pythonæ趣çç¼ç¨ä»£ç
class?Point:
row=0
col=0
def?__init__(self,?row,?col):
self.row=row
self.col=col
def?copy(self):
return?Point(row=self.row,?col=self.col)
#åå§æ¡æ¶
import?pygame
import?random
#åå§å
pygame.init()
W=
H=
ROW=
COL=
size=(W,H)
window=pygame.display.set_mode(size)
pygame.display.set_caption('è´ªåè')
bg_color=(,,)
snake_color=(,,)
head=Point(row=int(ROW/2),?col=int(COL/2))
head_color=(0,,)
snakes=[
Point(row=head.row,?col=head.col+1),
Point(row=head.row,?col=head.col+2),
Point(row=head.row,?col=head.col+3)
]
#çæé£ç©
def?gen_food():
while?1:
pos=Point(row=random.randint(0,ROW-1),?col=random.randint(0,COL-1))
#
is_coll=False
#æ¯å¦è·è碰ä¸äº
if?head.row==pos.row?and?head.col==pos.col:
is_coll=True
#è身å
for?snake?in?snakes:
if?snake.row==pos.row?and?snake.col==pos.col:
is_coll=True
break
if?not?is_coll:
break
return?pos
#å®ä¹åæ
food=gen_food()
food_color=(,,0)
direct='left'?#left,right,up,down
#
def?rect(point,?color):
cell_width=W/COL
cell_height=H/ROW
left=point.col*cell_width
top=point.row*cell_height
pygame.draw.rect(
window,?color,
(left,?top,?cell_width,?cell_height)
)
pass
#游æ循ç¯
quit=True
clock=pygame.time.Clock()
while?quit:
#å¤çäºä»¶
for?event?in?pygame.event.get():
if?event.type==pygame.QUIT:
quit=False
elif?event.type==pygame.KEYDOWN:
if?event.key==?or?event.key==:
if?direct=='left'?or?direct=='right':
direct='up'
elif?event.key==?or?event.key==:
if?direct?==?'left'?or?direct?==?'right':
direct='down'
elif?event.key==?or?event.key==:
if?direct?==?'up'?or?direct?==?'down':
direct='left'
elif?event.key==?or?event.key==:
if?direct?==?'up'?or?direct?==?'down':
direct='right'
#åä¸è¥¿
eat=(head.row==food.row?and?head.col==food.col)
#éæ°äº§çé£ç©
if?eat:
food?=?gen_food()
#å¤ç身å
#1.æåæ¥ç头ï¼æå ¥å°snakesç头ä¸
snakes.insert(0,?head.copy())
#2.æsnakesçæåä¸ä¸ªå æ
if?not?eat:
snakes.pop()
#移å¨
if?direct=='left':
head.col-=1
elif?direct=='right':
head.col+=1
elif?direct=='up':
head.row-=1
elif?direct=='down':
head.row+=1
#æ£æµ
dead=False
#1.æå¢
if?head.col0?or?head.row0?or?head.col=COL?or?head.row=ROW:
dead=True
#2.æèªå·±
for?snake?in?snakes:
if?head.col==snake.col?and?head.row==snake.row:
dead=True
break
if?dead:
print('æ»äº')
quit=False
#渲æââç»åºæ¥
#èæ¯
pygame.draw.rect(window,?bg_color,?(0,0,W,H))
#è头
for?snake?in?snakes:
rect(snake,?snake_color)
rect(head,?head_color)
rect(food,?food_color)
#
pygame.display.flip()
#设置帧é¢ï¼é度ï¼
clock.tick(8)
#æ¶å°¾å·¥ä½
è¿æ¯ä¸ä¸ªç®æçè´ªåèç代ç ï¼è½ç¶ç»æç®åï¼ä½æ¯è¯¥æçåè½é½æ¯å®æ´çï¼å¯ç©æ§ä¹ä¸é
æ±pythonæ°æ®æ åå代ç ï¼ä½¿ç¨pythonæ ååæ°æ®ç代ç å¦ä¸ï¼
fromsklearnimportpreprocessing
importnumpyasnp
X=np.array([[1.,-1.,2.],[2.,0.,0.],[0.,1.,-1.]])
X_scaled=preprocessing.scale(X)
print(X_scaled)
pythonå£è¯æ 代ç ç®å```python#Pythonå£è¯æ 代ç foriinrange(1,6):forjinrange(1,i+1):print('*',end='')print('\n')```
æå±ï¼å¦ææ³æ¹åæ çå½¢ç¶ï¼å¯ä»¥å¨ä»£ç ä¸æ·»å æ´å¤çæ§å¶åæ°ï¼å¦å¨æ¯ä¸è¡ä¸æ·»å ä¸åçç©ºæ ¼æ°ï¼ä½¿å¾æ çå½¢ç¶ä¸ä¸æ ·ãåæ¶å¯ä»¥éè¿æ·»å HTMLæ ç¾ï¼ä½¿ç¨CSSæ ·å¼æ¥æ¹åå£è¯æ çé¢è²ãèæ¯åæåççã
个æç®python代ç ï¼æ¿èµ°å³ç¨
Helloï¼å¤§å®¶å¥½ï¼ææ¯ç¨åºæ±ªå°æ~
è½ç¶pythonæ¯ä¸ä¸ªæå ¥é¨çè¯è¨ï¼ä½æ¯å¾å¤äººä¾ç¶è¿æ¯ä¼é®å°åºæä¹æ ·å¦Pythonææå¿«ï¼çæ¡å½ç¶æ¯å®æåç§å°é¡¹ç®ï¼åªæèªå·±å»æ³ä¸åï¼æè®°å¾ä½è§åãæ¬æåçæ¯ä¸ªæç®ä»»å¡ï¼åå¦è å¯ä»¥å°è¯çèªå·±å®ç°ï¼æ¬æåæ ·ä¹æ¯æ®µä»£ç ï¼Pythonå¼åè ä¹å¯ä»¥ççæ¯ä¸æ¯æ没æ³å°çç¨æ³ã
以ä¸æ¹æ³å¯ä»¥æ£æ¥ç»å®å表æ¯ä¸æ¯åå¨éå¤å ç´ ï¼å®ä¼ä½¿ç¨set()å½æ°æ¥ç§»é¤ææéå¤å ç´ ã
ç»å®å ·ä½ç大å°ï¼å®ä¹ä¸ä¸ªå½æ°ä»¥æç §è¿ä¸ªå¤§å°åå²å表ã
è¿ä¸ªæ¹æ³å¯ä»¥å°å¸å°åçå¼å»æï¼ä¾å¦ï¼Falseï¼Noneï¼0ï¼ââï¼ï¼å®ä½¿ç¨filter()å½æ°ã
æ们常ç¨For循ç¯æ¥éåæ个å表ï¼åæ ·æ们ä¹è½æ举å表çç´¢å¼ä¸å¼ã
å¦ä¸ä»£ç 段å¯ä»¥å°æå 好çæ对å表解å¼æ两ç»ä¸åçå ç»ã
该æ¹æ³å°éè¿éå½çæ¹å¼å°å表çåµå¥å±å¼ä¸ºå个å表ã
该æ¹æ³å°è¿å第ä¸ä¸ªå表çå ç´ ï¼ä¸ä¸å¨ç¬¬äºä¸ªå表å ãå¦æåæ¶è¦åé¦ç¬¬äºä¸ªå表ç¬æçå ç´ ï¼è¿éè¦å ä¸å¥set_b.difference(set_a)ã
å¦ä¸ä»£ç åå¯ä»¥ç¨æ¥è®¡ç®æ§è¡ç¹å®ä»£ç æè±è´¹çæ¶é´ã
该ç®æ³ä¼æä¹±å表å ç´ ç顺åºï¼å®ä¸»è¦ä¼éè¿Fisher-Yatesç®æ³å¯¹æ°å表è¿è¡æåºï¼
ä¸éè¦é¢å¤çæä½å°±è½äº¤æ¢ä¸¤ä¸ªåéçå¼ã
以ä¸ï¼æ¯æç®åå举çå个pythonæç®ä»£ç ï¼æ¿èµ°å³ç¨ï¼å¸æå¯¹ä½ ææ帮å©ï¼
pythonæ°æ代ç æåªäºï¼pythonæ°æ代ç æå¦ä¸ï¼
defnot_emptyï¼sï¼ã
returnsandlenï¼sãstripï¼ï¼ï¼0ã
#returnsandsãstripï¼ï¼ã
#å¦æç´æ¥ååsãstripï¼ï¼é£ä¹så¦ææ¯Noneï¼ä¼æ¥éï¼å 为None没æstripæ¹æ³ã
#å¦æsæ¯Noneï¼é£ä¹Noneandä»»ä½å¼é½æ¯Falseï¼ç´æ¥è¿åfalseã
#å¦æséNoneï¼é£ä¹å¤å®sãtripï¼ï¼æ¯å¦ä¸ºç©ºã
ç¸å ³ç®ä»ã
Python解éå¨æäºæ©å±ï¼å¯ä»¥ä½¿ç¨CæC++ï¼æè å ¶ä»å¯ä»¥éè¿Cè°ç¨çè¯è¨ï¼æ©å±æ°çåè½åæ°æ®ç±»åãPythonä¹å¯ç¨äºå¯å®å¶å软件ä¸çæ©å±ç¨åºè¯è¨ãPython丰å¯çæ ååºï¼æä¾äºéç¨äºå个主è¦ç³»ç»å¹³å°çæºç ææºå¨ç ã
å¹´æï¼è¯è¨æµè¡ææ°çç¼è¯å¨Tiobeå°Pythonå å为æå欢è¿çç¼ç¨è¯è¨ï¼å¹´æ¥é¦æ¬¡å°å ¶ç½®äºJavaãCåJavaScriptä¹ä¸ã
è´¨æ°è¡¨ä»£ç ï¼è´¨æ°è¡¨ä»£ç æ¯æç¨ç¼ç¨è¯è¨çæä¸ç³»åè´¨æ°ç代ç ãè´¨æ°æ¯æåªè½è¢«èªèº«å1æ´é¤çèªç¶æ°ï¼å¦2ï¼3ï¼5ï¼7çã
ä¸åçç¼ç¨è¯è¨æä¸åçå®ç°æ¹æ³ï¼æå¯ä»¥ç»ä½ ä¸äºåèã以ä¸æ¯ä¸äºç½ä¸æç´¢å°çè´¨æ°è¡¨ä»£ç 示ä¾ï¼
Cè¯è¨ï¼
#includestdio.hintmain(){ inti,j;printf("2\n");for(i=3;i=;i+=2)//ä»3å¼å§éåå¥æ°
{ for(j=3;ji;j++)//å¤ææ¯å¦è½è¢«å°äºå®çå¥æ°æ´é¤
{ if(i%j==0)//å¦æè½æ´é¤ï¼åè·³åºå¾ªç¯
break;
}if(j==i)//å¦æ循ç¯æ£å¸¸ç»æï¼å说ææ¯è´¨æ°ï¼æå°åºæ¥
printf("%d\n",i);
}return0;
}
Pythonï¼
#!/usr/bin/python#-*-coding:UTF-8-*-
#è¾åºæå®èå´å çç´ æ°
#ç¨æ·è¾å ¥æ°æ®lower=int(input("è¾å ¥åºé´æå°å¼:"))
upper=int(input("è¾å ¥åºé´æ大å¼:"))
fornuminrange(lower,upper+1):?#ç´ æ°å¤§äº1
ifnum1:?foriinrange(2,num):?if(num%i)==0:?breakelse:?print(num)怎样通过三个按钮控制页面上的一段文字放大,缩小,以及恢复到原始大小的例文源代码
<script language="JavaScript">
function doZoom(objid,size)
{
document.getElementById(objid).style.fontSize=size+'px'
//可以设置字体的大小!
}
</script>
测试:
<input type=button onclick="doZoom('testsize',源码源码)" value="大">
<input type=button onclick="doZoom('testsize',)" value="恢复">
<input type=button onclick="doZoom('testsize',)" value="小">
<span id="testsize" style="font-size:px">可以设置字体的大小!</span>
[stl 源码分析] std::list::size 时间复杂度
在对Linux上C++项目进行性能压测时,源码源码一个意外的源码源码iade钓鱼源码发现是std::list::size方法的时间复杂度并非预期的高效。原来,源码源码这个接口在较低版本的源码源码app棋牌源码群g++(如4.8.2)中是通过循环遍历整个列表来计算大小的,这导致了明显的源码源码性能瓶颈。@NagiS的源码源码提示揭示了这个问题可能与g++版本有关。
在功能测试阶段,源码源码CPU负载始终居高不下,源码源码通过火焰图分析,源码源码std::list::size的源码源码调用占据了大部分执行时间。火焰图的源码源码红包互换红包源码使用帮助我们深入了解了这一问题。
查阅相关测试源码(源自cplusplus.com),源码源码在较低版本的源码源码g++中,std::list通过逐个节点遍历来获取列表长度,windbg源码驱动调试这种操作无疑增加了时间复杂度。然而,对于更新的影视app源码交易g++版本(如9),如_glibcxx_USE_CXX_ABI宏启用后,list的实现进行了优化。它不再依赖遍历,而是利用成员变量_M_size直接存储列表大小,从而将获取大小的时间复杂度提升到了[公式],显著提高了性能。具体实现细节可在github上找到,如在/usr/include/c++/9/bits/目录下的代码。