第六届蓝桥杯大赛个人赛决赛(软件类)真题
生活随笔
收集整理的這篇文章主要介紹了
第六届蓝桥杯大赛个人赛决赛(软件类)真题
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
分號(hào)機(jī)
import java.io.IOException; class MC{public void run(){int cnt = 0;for (int i = 9; i >= 0; i--) {for (int j = 9; j >= 0; j--) {for (int k = 9; k >= 0; k--) {if(i > j && j > k){cnt ++;}}}}//120System.out.println(cnt);} } public class Main {public static void main(String[] args) throws IOException {new MC().run();} }五星填數(shù)
全排列枚舉。最后答案要除10,因?yàn)橐环N情況正面可以轉(zhuǎn)動(dòng)5次,鏡像面可以轉(zhuǎn)到5次,但都只是一種情況
顯示二叉樹(shù)
思路:先看看sv的是什么東西,發(fā)現(xiàn)是要填的數(shù)值;發(fā)現(xiàn)豎線下面是數(shù)字,所以是y+1,然后每個(gè)數(shù)字偏移p2個(gè)位置
class BiTree {private int v;private BiTree l;private BiTree r;public BiTree(int v){this.v = v;}public void add(BiTree the){if(the.v < v){if(l==null) l = the;else l.add(the);}else{if(r==null) r = the;else r.add(the);}}public int getHeight(){int h = 2;int hl = l==null? 0 : l.getHeight();int hr = r==null? 0 : r.getHeight();return h + Math.max(hl,hr);}public int getWidth(){int w = (""+v).length();if(l!=null) w += l.getWidth();if(r!=null) w += r.getWidth();return w;}public void show(){char[][] buf = new char[getHeight()][getWidth()];printInBuf(buf, 0, 0);showBuf(buf);}private void showBuf(char[][] x){for(int i=0; i<x.length; i++){for(int j=0; j<x[i].length; j++)System.out.print(x[i][j]==0? ' ':x[i][j]);System.out.println();}}private void printInBuf(char[][] buf, int x, int y){String sv = "" + v;int p1 = l==null? x : l.getRootPos(x);int p2 = getRootPos(x);int p3 = r==null? p2 : r.getRootPos(p2+sv.length());buf[y][p2] = '|';for(int i=p1; i<=p3; i++) buf[y+1][i]='-';for(int i=0; i<sv.length(); i++) buf[y+1][p2+i]=sv.charAt(i); //填空位置System.out.println();if(p1<p2) buf[y+1][p1] = '/';if(p3>p2) buf[y+1][p3] = '\\';if(l!=null) l.printInBuf(buf,x,y+2);if(r!=null) r.printInBuf(buf,p2+sv.length(),y+2);}private int getRootPos(int x){return l==null? x : x + l.getWidth();} }public class Main {public static void main(String[] args){BiTree tree = new BiTree(500);tree.add(new BiTree(200));tree.add(new BiTree(509));tree.add(new BiTree(100));tree.add(new BiTree(250));tree.add(new BiTree(507));tree.add(new BiTree(600));tree.add(new BiTree(650));tree.add(new BiTree(450));tree.add(new BiTree(510));tree.add(new BiTree(440));tree.add(new BiTree(220));tree.show();} }穿越雷區(qū)
穿越雷區(qū)
//記錄起點(diǎn)和終點(diǎn),bfs搜索,最先到點(diǎn)B即為最短 #include <iostream> #include <cstring> #include <algorithm> #include <queue>using namespace std; typedef pair<int, int> PII;const int N = 110; int pro[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; int dist[N][N]; int g[N][N]; PII A, B;int main(){int n;scanf("%d", &n);for (int i = 1; i <= n; i ++ ){for (int j = 1; j <= n; j ++ ){char c;cin >> c;if(c == '+') g[i][j] = 1;else if(c == 'A') {g[i][j] = 3;A = {i, j};}else if(c == 'B') {g[i][j] = 6;B = {i, j};}else g[i][j] = 0;}}memset(dist, 0x3f, sizeof dist);dist[A.first][A.first] = 0;queue<PII> q;q.push(A);while (q.size() > 0){PII p = q.front();q.pop();int x = p.first, y = p.second;//遍歷鄰點(diǎn)for (int i = 0; i < 4; i ++ ){int tx = x + pro[i][0], ty = y + pro[i][1];//判斷該點(diǎn)合不合法if(tx < 1 || tx > n || ty < 1 || ty > n || dist[tx][ty] != 0x3f3f3f3f || g[x][y] == g[tx][ty]) continue;dist[tx][ty] = dist[x][y] + 1;q.push({tx, ty});}}if(dist[B.first][B.second] == 0x3f3f3f3f) printf("%d", -1);else printf("%d", dist[B.first][B.second]); }表格計(jì)算
總結(jié)
以上是生活随笔為你收集整理的第六届蓝桥杯大赛个人赛决赛(软件类)真题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python爬虫实例--新浪热搜榜[正则
- 下一篇: 必备算法基础——神经网络