### 【哈爾濱理工大學第七屆程序設計競賽初賽（低年級組）】A B C D E F H I

A 藍廋、香菇

``//package FirstTime;import java.util.*;import java.math.*;import java.text.DecimalFormat;public class Main{    public static void main(String[] agrs){         Scanner cin = new Scanner(System.in);         int t=cin.nextInt();          while(cin.hasNext()){             String s=cin.next();             String ans="";             if(s.compareTo("lansou")==0){                 ans+="xianggu";             }             else if(s.compareTo("xianggu")==0) ans+="lansou";             System.out.println(ans);         }    }}``

C Tabris減肥記

``//package FirstTime;import java.util.*;import java.math.*;import java.text.DecimalFormat;public class Main{    public static void main(String[] agrs){         Scanner cin = new Scanner(System.in);         int t=cin.nextInt();         while(cin.hasNext()){              int w,s;w=cin.nextInt();s=cin.nextInt();              int d=w-120;int ans;              if(d<=0)  ans=0;              else  ans=(int)Math.ceil(d*1.0/s);              System.out.println(ans);         }    }}``

D 逃脫
BFS 。

``#include<bits/stdc++.h>using namespace std;const int MAXN = 50+11;const int inf = 0x3f3f3f3f;struct Node{    int x,y,step;};int n,m;int mp[MAXN+2][MAXN+2];int Time[MAXN+2][MAXN+2];bool vis[MAXN+2][MAXN+2];int to[8][2]={1,0,-1,0,0,1,0,-1,-1,-1,-1,1,1,-1,1,1};void Getfire(Node fire){    memset(Time,-1,sizeof(Time));    queue<Node>Q;    Q.push(fire); Time[fire.x][fire.y]=0;    while(!Q.empty()){        Node now = Q.front(); Q.pop();        for(int i=0;i<8;i++){            int nx=now.x+to[i][0]; int ny=now.y+to[i][1];            if(nx<1||nx>n||ny<1||ny>m) continue;            if(Time[nx][ny]!=-1) continue;            Time[nx][ny]=now.step+1;            Node next={nx,ny,now.step+1};            Q.push(next);        }    }   /* for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) printf("%d",Time[i][j]); puts(""); } */}void BFS(Node s){   // for(int i=0;i<4;i++) printf("%d %d \n",to[i][0],to[i][1]);    memset(vis,0,sizeof(vis));    queue<Node>Q; Q.push(s); vis[s.x][s.y]=1;    while(!Q.empty()){        Node now=Q.front(); Q.pop() ;        for(int i=0;i<4;i++){            int nx=now.x+to[i][0]; int ny=now.y+to[i][1];            if(nx<1||nx>n||ny<1||ny>m) continue;            if(vis[nx][ny]||mp[nx][ny]==2) continue;            if(now.step+1<=Time[nx][ny]&&mp[nx][ny]==3)  {                printf("%d\n",now.step+1);                return ; //如果跑到出口的時間正好和着火時間一樣，那樣這個人也可以逃脫            }            if(now.step+1>=Time[nx][ny]) continue;//但是如果是一個普通地區，到達時間和着火時間一樣，肯定GG。            Node next={nx,ny,now.step+1};            vis[nx][ny]=1;            Q.push(next);        }    }    puts("T_T");}char s[100];int main(){    int tt;scanf("%d",&tt);    while(tt--){        memset(mp,0,sizeof(mp));        scanf("%d%d",&n,&m);        Node st,fire;        for(int i=1;i<=n;i++){           scanf("%s",s); int len=strlen(s);            for(int j=0;j<len;j++) {                if(s[j]=='.') mp[i][j+1]=1;                else if(s[j]=='#') mp[i][j+1]=2;                else if(s[j]=='S'){                    mp[i][j+1]=1;                    st={i,j+1,0};                }                else if(s[j]=='E') mp[i][j+1]=3;                else if(s[j]=='*')  {                    mp[i][j+1]=4;                    fire={i,j+1,0};                }            }        }        /* for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++) printf("%d",mp[i][j]); puts(""); } */        Getfire(fire);        BFS(st);    }return 0;}``

E 簡單的加法運算

``//package FirstTime;import java.util.*;import java.math.*;import java.text.DecimalFormat;public class Main{    public static void main(String[] agrs){         Scanner cin = new Scanner(System.in);        String t = cin.nextLine();         while(cin.hasNext()){               String s=cin.nextLine();              // System.out.println("s== "+s+" t"+t);               int ans=0;               int x=0; int len=s.length();               for(int i=0;i<len; ) {                   if(s.charAt(i)>='0'&&s.charAt(i) <='9') {                       x=x*10+s.charAt(i)-'0';                       i++;                   }else {                       ans+=x; x=0; i++;                   }               }               ans+=x;               System.out.println(ans);         }    }}``

F 神奇的數字

``//package FirstTime;import java.util.*;import java.math.*;import java.text.DecimalFormat;public class Main{    static void  f(int j){        String s=""+j;        for(int i=s.length()-1;i>=0;i--) System.out.print(s.charAt(i));    }    public static void main(String[] agrs){         Scanner cin = new Scanner(System.in);         for(int i=1000;i<=9999;i++) {             System.out.print(i);f(i);             System.out.println();         }    }}``

H 魔法陣

``//package FirstTime;import java.util.*;import java.math.*;import java.text.DecimalFormat;public class Main{    public static void main(String[] agrs){         Scanner cin = new Scanner(System.in);         String s,t;         int[] num = new int[100000+11];         String z=cin.next();         while(cin.hasNext()){             Arrays.fill(num, 0);             s=cin.next(); t=cin.next();             for(int i=0;i<s.length();i++) {                 num[s.charAt(i)-'A']++;             }             long ans=0;             for(int i=0;i<t.length();i++){                 ans+=num[t.charAt(i)-'A'];             }             ans*=t.length();             System.out.println(ans);         }    }}``

I 布置會場(I)

``//package FirstTime;import java.util.*;import java.math.*;import java.text.DecimalFormat;public class Main{    public static void main(String[] agrs){        BigInteger[] F = new BigInteger[40];        F[0]=new BigInteger("1");         for(int i=1;i<=32;i++) F[i]=F[i-1].multiply(new BigInteger(""+i));         Scanner cin = new Scanner(System.in);         int t ;t=cin.nextInt();         while(t-->0){             int n; n=cin.nextInt();BigInteger ans=new BigInteger("1");             for(int i=2;i<=n;i+=2){                 int a=n-i;int b=i/2;                 if(b==1) {                     ans=ans.add(new BigInteger(a+1+""));                 }else {                     ans=ans.add(F[a+b].divide(F[a].multiply(F[b])));                 }             }             System.out.println(ans);;         }    }}``

#### 注意！

© 2014-2020 ITdaan.com