2016年蓝桥杯预选赛试题(水题)
试题内容:
代码如下:
python实现:
1.杨辉三角
1 # 打印前10行杨辉三角 2 print("1") 3 print("1 1") 4 line = [1, 1] 5 for i in range(2, 10): 6 r = [] 7 for j in range(0, len(line)-1): 8 r.append(line[j]+line[j+1]) 9 line = [1] + r + [1] 10 for item in line: 11 print(item, end=" ") 12 print()
2.猜糖
1 res = 0 2 for i in range(0, 6): 3 for j in range(0, 4): 4 for k in range(0, 8): 5 if i + j + k == 10: 6 print(i, j, k) 7 res += 1 8 9 print("一共有%d种" % res)
3.找搭档
1 # 男: abc 女: xyz 2 # a不和x配对 c不和x配对 c不和z配对 3 4 for i in range(0, 3): 5 for j in range(0, 3): 6 for k in range(0, 3): 7 if i != 0 and k != 0 and k != 2 and i != j and i != k and j != k: 8 print("a,b,c的配对对象是: ", chr(ord("x")+i), chr(ord("x")+j), chr(ord("x")+k))
C++实现:
1.杨辉三角
1 //杨辉三角 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 int i ,j , a[10][10]; 8 9 //初始化第一个值和最后一个值(1)并计算其他值 10 for(i=0;i<10;i++) 11 for(j=0;j<=i;j++) 12 { 13 a[i][0] = 1; 14 a[i][j] = a[i-1][j-1]+a[i-1][j]; 15 a[i][i] = 1; 16 } 17 18 //打印输出 19 for(i=0;i<10;i++) 20 { 21 for(j=0;j<=i;j++) 22 cout << a[i][j] << " "; 23 cout << endl; 24 25 } 26 27 return 0; 28 }
2.猜糖
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int res = 0; 7 int i, j, k; 8 for(i=0;i<=5;i++) 9 for(j=0;j<=3;j++) 10 for(k=0;k<=7;k++) 11 { 12 if(i+j+k==10) 13 { 14 cout << i << " " << j << " " << k << endl; 15 res++; 16 } 17 } 18 cout << "结果是: " << res << endl; 19 20 return 0; 21 }
3.找搭档
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int a, b, c; 7 for(a=1;a<=3;a++) 8 for(b=1;b<=3;b++) 9 for(c=1;c<=3;c++) 10 { 11 if(a!=1&&c!=1&&c!=3&&a!=b&&b!=c&&a!=c) 12 { 13 cout << "a的搭档是" << char('x'+a-1) << endl; 14 cout << "b的搭档是" << char('x'+b-1) << endl; 15 cout << "c的搭档是" << char('x'+c-1) << endl; 16 } 17 } 18 19 return 0; 20 }
too young too simple sometimes native!