http://oj.acm-icpc.top/
a題:三個數字排序
可以利用sort函數排序,或者自己想清楚邏輯自己寫,我給出一個正確邏輯
(拓展冒泡和其他排序參考https://blog.csdn.net/hebtu666/article/details/81434236)
a,b,c=input().split(" ")
a,b,c=int(a),int(b),int(c)
if a>b:a,b=b,a
if b>c:b,c=c,b
if a>b:a,b=b,a
print(a,b,c)
b題:判斷三角形能否組成
很簡單,直接判斷即可
while 1:a,b,c=[int(x) for x in input().split()]if a==0:breakelif a+b>c and a+c>b and b+c>a:print('Great,you are genius!')else:print('oh,my god!')
c題:把1到n中的所有奇數從小到大輸出,再把所有的偶數從小到大輸出。??
利用循環,加上步長,一次性輸出。
或者兩個循環,不加步長,判斷一下再輸出即可。
time=int(input())
while 1:a=int(input())for i in range(1,a+1,2):print(i,end=" ")print("")for j in range(2,a+1,2):print(j,end=" ")print("")time=time-1if time==0:break
d題:已知雞和兔的總數量為n,總腿數為m。輸入n和m,依次輸出雞和兔的數目,如果無解,則輸出“No answer”(不要引號)。
t=int(input())
while t:n,m=input().split()if int(m)%2==0:a=(int(m)-int(n)*2)/2b=int(n)-aif a>=0 and b>=0:print(int(b),int(a))else:print('No answer')t=t-1
e題:簡化版:::找出從自然數1-6中任取3個數的所有組合。
for a in range(6,0,-1):for b in range(6,0,-1):for c in range(6,0,-1):if a>b>c:print(str(a)+str(b)+str(c))
任取n個數:
#include<stdio.h>
int a[15];
int n,r;
void dfs(int c,int x){int i;if(x==r+1){for(int j=1;j<=r;j++)printf("%d",a[j]);printf("\n");}for(i=c;i>0;i--){a[x]=i;dfs(i-1,x+1);}
}
int main(){while(scanf("%d%d",&n,&r)==2){dfs(n,1);}return 0;
}
f題:編寫一個程序實現將字符串中的所有"you"替換成"we"
利用函數:
while 1:a=str(input())print(a.replace("you", "we"))
或遍歷、判斷、替換。
G題:
題目描述
還記得中學時候學過的楊輝三角嗎?具體的定義這里不再描述,你可以參考以下的圖形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
輸入
輸入數據包含多個測試實例,每個測試實例的輸入只包含一個正整數n(1<=n<=30),表示將要輸出的楊輝三角的層數。
輸出
對應于每一個輸入,請輸出相應層數的楊輝三角,每一層的整數之間用一個空格隔開,每一個楊輝三角后面加一個空行。
楊輝三角,就是按照定義來模擬即可。
這里給出python的一點比較神的列表操作
列表生成式知識請看我的python總結里有。
利用定義簡單明了的生成每一行楊輝:
for i in range(1,n):l=[1]+[int(l[j])+int(l[j+1]) for j in range(len(l)-1)]+[1]
?