• UVaOJ 112道题目-入门


    1、110102/10189 Minesweeper (扫雷)

    题意:给出N*M的方块内容,计算每个安全方块内的四周八个点共有多少地雷

    扫描四周八个点时可以用到方向数组

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    char str[105][105];
    int n,m;
    int dire[8][2]=
    {
        {-1,-1},
        {-1,0},
        {-1,1},
        {0,-1},
        {0,1},
        {1,-1},
        {1,0},
        {1,1}
    };
    int Search(int x,int y,int k)
    {
        x+=dire[k][0];
        y+=dire[k][1];
        if(x>=n||x<0||y>=m||y<0)
        {
            return 0;
        }
        if(str[x][y]=='*')
        {
            return 1;
        }
        return 0;
    }
    int main()
    {
        
        int T=1;
        while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
        {
            int i,j;
            getchar();
            if(T!=1)
            {
                printf("
    ");
            }
            for(i=0;i<n;i++)
            {
                gets(str[i]);
            }
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    int cnt=0;
                    if(str[i][j]=='*')
                    {
                        continue;
                    }
                    else
                    {
                        int k;
                        for(k=0;k<8;k++)
                        {
                            cnt+=Search(i,j,k);
                        }
                        str[i][j]=cnt+'0';
                    }
                }
            }
            printf("Field #%d:
    ",T);
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    printf("%c",str[i][j]);
                }
                printf("
    ");
            }
            T++;
        }
        return 0;
    }
    View Code

     2、110103/10137 The Trip (旅行)

    注意:整个计算过程中的数只精确到百分位,所以总花费的平均数只精确到百分位,可以转化为整数计算,题目要求误差within one  cent,即每人付钱的差值不能超过 one  cent,需要慎重考虑

    将所有花费从小到大排列,遍历小于平均数的几个数就可以了

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    int dig[1005];
    bool cmp(int a,int b)
    {
        return a<b;
    }
    int main()
    {
        int a,b,n;
        while(scanf("%d",&n)!=EOF&&n)
        {
            int i;
            int sum=0;
            for(i=0;i<n;i++)
            {
                scanf("%d.%d",&a,&b);
                dig[i]=a*100+b;
                sum+=dig[i];
            }
            sort(dig,dig+n,cmp);
            int avg=sum/n;
            int m=sum%n;
            int tot=0;
            for(i=0;i<n;i++)
            {
                if(avg>=dig[i])
                {
                    tot+=avg-dig[i];
                }
                else
                {
                    if(n-i<m)
                    {
                        tot+=m-(n-i);
                    }
                    break;
                }
            }
            printf("$%d.%02d
    ",tot/100,tot%100);
        }
        return 0;
    }
    /*
    4
    9.00
    9.00
    9.01
    9.02
    */
    View Code

    3、 110104/706  LC-Display

    排版题

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    int s,n;
    int str[10][10];
    void Init()
    {
        int i,j;
        for(i=0;i<=6;i++)
        {
            for(j=0;j<=9;j++)
            {
                if(i==0&&j!=2&&j!=3&&j!=7&&j!=1)
                {
                    str[i][j]=1;
                }
                else if(i==1&&j!=1&&j!=4)
                {
                    str[i][j]=1;
                }
                else if(i==2&&j!=5&&j!=6)
                {
                    str[i][j]=1;
                }
                else if(i==3&&j!=2)
                {
                    str[i][j]=1;
                }
                else if(i==4&&j!=1&&j!=4&&j!=7)
                {
                    str[i][j]=1;
                }
                else if(i==5&&j!=3&&j!=4&&j!=5&&j!=7&&j!=9&&j!=1)
                {
                    str[i][j]=1;
                }
                else if(i==6&&j!=0&&j!=1&&j!=7)
                {
                    str[i][j]=1;
                }
            }
        }
    }
    int main()
    {
        memset(str,0,sizeof(str));
        Init();
        int T=0;
        while(scanf("%d%d",&s,&n)!=EOF&&(s||n))
        {
    
             int i,j;
             int tmp[20];
             i=0;
             while(1)
             {
                tmp[i]=n%10;
                n/=10;
                if(n==0)
                {
                    break;
                }
                i++;
             }
             int len=i;
             int t;
             for(i=0;i<2*s+3;i++)
             {
                for(j=len;j>=0;j--)
                {
                    int c=tmp[j];
                    if(i==0||i==s+1||i==2*s+2)
                    {
                        int x=1;
                        if(i==s+1)
                        {
                            x=6;
                        }
                        else if(i==2*s+2)
                        {
                            x=4;
                        }
                        else
                        {
                            x=1;
                        }
                        printf(" ");
                        if(str[x][c]==1)
                        {
                            for(t=0;t<s;t++)
                            {
                                printf("-");
                            }
                        }
                        else
                        {
                            for(t=0;t<s;t++)
                            {
                                printf(" ");
                            }
                        }
                        printf(" ");
                    }
                    else
                    {
                        if(i>=1&&i<=s)
                        {
                            if(str[0][c]==1)
                            {
                                printf("|");
                            }
                            else 
                            {
                                printf(" ");
                            }
                            for(t=0;t<s;t++)
                            {
                                printf(" ");
                            }
                            if(str[2][c]==1)
                            {
                                printf("|");
                            }
                            else 
                            {
                                printf(" ");
                            }
                        }
                        else if(i>=s+2&&i<2*s+2)
                        {
                            if(str[5][c]==1)
                            {
                                printf("|");
                            }
                            else 
                            {
                                printf(" ");
                            }
                            for(t=0;t<s;t++)
                            {
                                printf(" ");
                            }
                            if(str[3][c]==1)
                            {
                                printf("|");
                            }
                            else 
                            {
                                printf(" ");
                            }
                        }
                    }
                    if(j)
                    {
                        printf(" ");
                    }
                    else
                    {
                        printf("
    ");
                    }
                }
             }
             printf("
    ");
        }
        return 0;
    }
    View Code

     4、110101/100  The 3n+1 problem 

    第一:题目中说处理过程不超过32位数,可是没有说是有符号数,所以为了防止溢出,仍旧要用64位整数表示。第二:考虑i>j的情况,输出时 要与输入同顺序

    此题也可以先递推求出1~1000000的数

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    int dig[1000005];
    const int INF=1000000;
    typedef long long lld;
    int Calc(lld x)
    {
        int tot=1;
        while(x!=1)
        {
            if(x%2==0)
            {
                x/=2;
            }
            else
            {
                x=3*x+1;
            }
            if(x<=INF)
            {
                if(dig[x]==0)
                    dig[x]=1;
            }
            tot++;
        }
        return tot;
    }
    int main()
    {
        int a,b;
        int Max;
        while(scanf("%d%d",&a,&b)!=EOF)
        {
            memset(dig,0,sizeof(dig));
            int i,x=0;
            Max=0;
            lld tmp;
            bool flag=true;
            if(a>b)
            {
                int t=a;
                a=b;
                b=t;
                flag=false;
            }
            for(i=b;i>=a;i--)
            {
                if(dig[i]==1)
                {
                    continue;
                }
                tmp=i;
                x=Calc(tmp);
                if(x>Max)
                {
                    Max=x;
                }
            }
            if(flag==false)
                swap(a,b);
            printf("%d %d %d
    ",a,b,Max);
        }
        return 0;
    }
    View Code

    (2)

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    int dig[1000005];
    const int INF=1000000;
    typedef long long lld;
    void Init()
    {
        int i;
        memset(dig,0,sizeof(dig));
        dig[1]=1;
        for(i=2;i<=INF;i++)
        {
            lld x=i;
            int tot=1;
            while(x!=1)
            {
                if(x%2==0)
                {
                    x/=2;
                }
                else
                {
                    x=3*x+1;
                }
                if(x<=INF&&dig[x]!=0)
                {
                    tot+=dig[x];
                    break;
                }
                else
                {
                    tot++;
                }
            }
            dig[i]=tot;
        }
        return ;
    }
    int main()
    {
        Init();
        int a,b;
        while(scanf("%d%d",&a,&b)!=EOF)
        {
            bool flag=true;
            if(a>b)
            {
                flag=false;
                swap(a,b);
            }
            int Max=0,i;
            for(i=a;i<=b;i++)
            {
                if(dig[i]>Max)
                {
                    Max=dig[i];
                }
            }
            if(flag==false)
            {
                swap(a,b);
            }
            printf("%d %d %d
    ",a,b,Max);
        }
        return 0;
    }
    View Code

     5、110105/10267 Graphical Editor 

    F命令需要考虑死循环的情况,V,H,K命令输入的坐标需要考虑坐标比右边大的情况

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    const int Max=256;
    char rect[Max][Max];
    int N=-1,M=-1;
    int grid[Max][Max];
    void Fill(int x,int y,char c,char cb)
    {
        if(x>N||x<1)
            return;
        if(y>M||y<1)
            return;
        if(grid[x][y]==1)
            return;
        grid[x][y]=1;
        if(rect[x][y]==cb)
        {
            rect[x][y]=c;
            Fill(x-1,y,c,cb);
            Fill(x+1,y,c,cb);
            Fill(x,y+1,c,cb);
            Fill(x,y-1,c,cb);
        }
    }
    void fun(char cmd[])
    {
        int i,j;
        int a,b,c,d;
        char str[100];
        if(cmd[0]=='I')
        {
            scanf("%d%d",&M,&N);
            for(i=1;i<=N;i++)
            {
                for(j=1;j<=M;j++)
                {
                    rect[i][j]='O';
                }
            }
        }
        else if(cmd[0]=='C')
        {
            for(i=1;i<=N;i++)
            {
                for(j=1;j<=M;j++)
                {
                    rect[i][j]='O';
                }
            }
        }
        else if(cmd[0]=='L')
        {
            scanf("%d%d",&a,&b);
            scanf("%s",str);
            rect[b][a]=str[0];
        }
        else if(cmd[0]=='V')
        {
            scanf("%d%d%d",&a,&b,&c);
            scanf("%s",str);
            if(b>c)swap(b,c);
            for(i=b;i<=c;i++)
            {
                rect[i][a]=str[0];
            }
        }
        else if(cmd[0]=='H')
        {
            scanf("%d%d%d",&a,&b,&c);
            scanf("%s",str);
            if(a>b)swap(a,b);
            for(i=a;i<=b;i++)
            {
                rect[c][i]=str[0];
            }
        }
        else if(cmd[0]=='K')
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            if(a>c)swap(a,c);
            if(b>d)swap(b,d);
            scanf("%s",str);
            for(i=b;i<=d;i++)
            {
                for(j=a;j<=c;j++)
                {
                    rect[i][j]=str[0];
                }
            }
        }
        else if(cmd[0]=='S')
        {
            scanf("%s",str);
            printf("%s
    ",str);
            for(i=1;i<=N;i++)
            {
                for(j=1;j<=M;j++)
                {
                    printf("%c",rect[i][j]);
                }
                printf("
    ");
    
            }
        }
        else if(cmd[0]=='F')
        {
            scanf("%d%d",&a,&b);
            scanf("%s",str);
            memset(grid,0,sizeof(grid));
            Fill(b,a,str[0],rect[b][a]);
        }
        else
        {
            gets(str);
            return;
        }
        gets(str);
    }
    int main()
    {
        char cmd[20];
        while(scanf("%s",cmd)!=NULL&&cmd[0]!='X')
        {
            fun(cmd);
        }
        return 0;
    }
    View Code

     6、110106/10033 Interpreter (解释器)

    注意:1.RAM的赋值。2 、需要循环到RAM的长度,而不是程序长度 3、还有可能循环多个RAM的长度,即在某次循环中走到halt,此程序没有涉及到这用例

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    char cmd[1005][5];
    int reg[15];
    int GetNum(char str[])
    {
        int i,len=strlen(str);
        int tot=0;
        for(i=0;i<len;i++)
        {
            tot=tot*10+str[i]-'0';
        }
        return tot;
    }
    void GetStr(int tot,char str[])
    {
        str[0]=tot/100+'0';
        str[1]=tot/10%10+'0';
        str[2]=tot%10+'0';
    }
    void Init()
    {
        int i;
        for(i=0;i<1000;i++)
        {
            strcpy(cmd[i],"000");
        }
    }
    int main()
    {
        int T;
        scanf("%d",&T);
    
        int flag=true;
        while(T--)
        {
            Init();
            memset(reg,0,sizeof(reg));
            int i=0;
            char str[10];
            if(flag==true)
            {
                getchar();
                gets(str);
            }
            flag=false;
            while(gets(cmd[i])!=NULL)
            {
                if(strcmp(cmd[i],"")==0)
                    break;
                i++;
                //if(i>=15)break;
            }
            int len=i;
    
            int cnt=0;
            int s,d,n,tot;
            for(i=0;i<1000;i=(i+1))
            {
                cnt++;
                d=cmd[i][1]-'0';
                s=cmd[i][2]-'0';
                if(strcmp(cmd[i],"100")==0)
                {
                    break;
                }
                else if(cmd[i][0]=='2')
                {
                    reg[d]=s;
                }
                else if(cmd[i][0]=='3')
                {
                    reg[d]=(reg[d]+s)%1000;
                }
                else if(cmd[i][0]=='4')
                {
                    reg[d]=(reg[d]*s)%1000;
                }
                else if(cmd[i][0]=='5')
                {
                    reg[d]=reg[s];
                }
                else if(cmd[i][0]=='6')
                {
                    reg[d]=(reg[d]+reg[s])%1000;
                }
                else if(cmd[i][0]=='7')
                {
                    reg[d]=(reg[d]*reg[s])%1000;        
                }
                else if(cmd[i][0]=='8')
                {
                    int add=reg[s];
                    reg[d]=GetNum(cmd[add]);
                }
                else if(cmd[i][0]=='9')
                {
                    GetStr(reg[d],cmd[reg[s]]);
                }
                else if(cmd[i][0]=='0')
                {
                    if(reg[s]!=0)
                    {
                        i=reg[d];
                        i--;
                    }
                }
            }
            printf("%d
    ",cnt);
            if(T)
            {
                printf("
    ");
            }
        }
        return 0;
    }
    
    /*
    20
    
    299
    492
    495
    399
    492
    495
    399
    283
    279
    689
    078
    100
    000
    000
    000
    
    100
    
    235
    335
    435
    543
    100
    
    205
    215
    315
    255
    915
    050
    000
    000
    000
    000
    100
    
    */
    View Code

     7.110108/10142 Australian Voting (澳大利亚投票)

    题意:每个人给出一个候选人排列,将票投给第一个候选人,若此候选人被淘汰,则将票投给下一个候选人,依次类推;题目描述有些拗口

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    struct Node
    {
        char name[100];
        int v;
        int e;
    }can[25];
    int vote[1005][25];
    int GetNum(char str[],int &r,int len)
    {
        int tot=0;
        
        if(r>=len)
        {
            return -1;
        }
        while(r<len&&str[r]==' ')
        {
            if(r<len)r++;
            else break;
        }
        int i;
        for(i=r;i<len;i++)
        {
            if(str[i]==' ')
                break;
            tot=tot*10+str[i]-'0';
        }
        r=i+1;
        return tot;
    }
    void GetPre(int i,int x,int n)
    {
        int j;
        for(j=x+1;j<n;j++)
        {
            int c=vote[i][j];
            if(can[c].e==1)
            {
                vote[i][n]=j;
                break;
            }
        }
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        getchar();
        char tmp[1000];
        gets(tmp);
        int n,i,j;
        while(T--)
        {
            scanf("%d",&n);
            getchar();
            for(i=1;i<=n;i++)
            {
                gets(can[i].name);
                can[i].v=0;
                can[i].e=1;
            }
            i=0;
            j=0;
            while(gets(tmp)!=NULL)
            {
                if(strcmp(tmp,"")==0)
                {
                    break;
                }
                int r=0;
                int len=strlen(tmp);
                while(1)
                {
                    int tot=GetNum(tmp,r,len);
                    if(tot==-1)
                        break;
                    vote[i][j]=tot;
                    j++;
                    if(j==n)
                    {
                        vote[i][j]=0;
                        i++;
                        j=0;
                    }                
                }
            }
            int len=i,c,x;
            for(i=0;i<len;i++)
            {
                c=vote[i][0];
                can[c].v++;
            }
            continue;
            while(1)
            {
                int max=0,min=n;
                int s;
                int ch=-1;
    
                for(i=0;i<len;i++)
                {
                    x=vote[i][n];
                    c=vote[i][x];
                    if(can[c].e==0)
                    {
                        GetPre(i,x,n);
                        x=vote[i][n];
                        c=vote[i][x];
                        can[c].v++;
                    }
                }
                for(s=1;s<=n;s++)
                {
                    if(can[s].e==0)
                        continue;
                    if(can[s].v>max)
                    {
                        max=can[s].v;
                        ch=s;
                    }
                    if(can[s].v<min)
                    {
                        min=can[s].v;
                    }
                }
                if(max>len/2)
                {
                    printf("%s
    ",can[ch].name);
                    break;
                }
                else
                {
                    if(max==min)
                    {
                        for(s=1;s<=n;s++)
                        {
                            if(can[s].e==0)
                                continue;
                            if(can[s].v==max)
                            {
                                printf("%s
    ",can[s].name);
                            }
                        }
                        break;
                    }
                    else
                    {
                        for(s=1;s<=n;s++)
                        {
                            if(can[s].e==0)
                                continue;
                            if(can[s].v==min)
                            {
                                can[s].e=0;
                            }
                        }
                    }
                }
            }
            if(T)
                printf("
    ");
        }
        return 0;
    }
    /*
    2
    
    3
    John Doe
    Jane Smith
    Sirhan Sirhan
    1 2 3
    2 1 3
    2 3 1
    1 2 3
    3 1 2
    3 2 1
    
    3
    John Doe
    Jane Smith
    Sirhan Sirhan
    1 2 3
    2 1 3
    2 3 1
    1 2 3
    3 1 2
    */
    View Code

     8.110107/10196 Check the Check (将军)

    题意:测试象棋谁被将军,可以得到将军所在位置,遍历所有的能一步到达将军所在的位置的点

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    char ch[10][10];
    char tmp[10];
    struct Point
    {
        int x,y;
    };
    Point B,W;
    int Way[8][2]=
    {
        {-1,0},
        {1,0},
        {0,-1},
        {0,1},
        {-1,-1},
        {-1,1},
        {1,-1},
        {1,1}
    };
    int KWay[8][2]=
    {
        {0,4},
        {0,5},
        {1,6},
        {1,7},
        {2,4},
        {2,6},
        {3,5},
        {3,7}
    };
    bool isValid(int x,int y)
    {
        if(x<0||x>=8)
            return 0;
        if(y<0||y>=8)
            return 0;
        return 1;
    }
    Point Go(Point tmp,int n,int w)
    {
        Point t;
        t.x=0;
        t.y=0;
        if(w>=0&&w<8)
        {
            t.x=tmp.x+Way[w][0]*n;
            t.y=tmp.y+Way[w][1]*n;
        }
        return t;
    }
    bool isRQB(Point tmp,int C)
    {
        int i,j;
        Point t;
        for(i=0;i<8;i++)
        {
            for(j=1;j<8;j++)
            {
                t=Go(tmp,j,i);
                if(isValid(t.x,t.y))
                {
                    char c=ch[t.x][t.y];
                    if(c=='.')
                    {
                        continue;
                    }
                    else
                    {
                         if(C==0)
                         {
                            if(c=='Q')
                                return true;
                            else if(i>=0&&i<4&&c=='R')
                                return true;
                            else if(i>=4&&i<8&&c=='B')
                                return true;
                            else
                                break;
                         }
                         else
                         {
                            if(c=='q')
                                return true;
                            else if(i>=0&&i<4&&c=='r')
                                return true;
                            else if(i>=4&&i<8&&c=='b')
                                return true;
                            else
                                break;
                         }
                    }
                }
                else
                {
                    break;
                }
            }
        }
        return false;
    }
    bool isK(Point tmp,int C)
    {
        int i;
        for(i=0;i<8;i++)
        {
            Point t;
            t.x=tmp.x+Way[i][0];
            t.y=tmp.y+Way[i][1];
            if(isValid(t.x,t.y))
            {
                char c=ch[t.x][t.y];
                if(C==0)
                {
                    if(c=='K')
                        return true;
                }
                else
                {
                    if(c=='k')
                        return true;
                }
            }
        }
        return false;
    }
    bool isP(Point tmp,int C)
    {
        int i,a,b;
        Point t;
        if(C==0)
        {
            a=6;
            b=7;
        }
        else
        {
            a=4;
            b=5;
        }
        for(i=a;i<=b;i++)
        {
            t.x=tmp.x+Way[i][0];
            t.y=tmp.y+Way[i][1];
            if(!isValid(t.x,t.y))
                continue;
            char c=ch[t.x][t.y];
            if(C==0)
            {
                if(c=='P')
                    return true;
            }
            else
            {
                if(c=='p')
                    return true;
            }
        }
        return false;
    }
    bool isN(Point tmp,int C)
    {
        int i;
        int a,b;
        for(i=0;i<8;i++)
        {
            Point t;
            a=KWay[i][0];
            b=KWay[i][1];
            t.x=tmp.x+Way[a][0]+Way[b][0];
            t.y=tmp.y+Way[a][1]+Way[b][1];
            if(!isValid(t.x,t.y))
                continue;
            char c=ch[t.x][t.y];
            if(C==0)
            {
                if(c=='N')
                    return true;
            }
            else
            {
                if(c=='n')
                    return true;
            }
        }
        return false;
    }
    int main()
    {
        int i,j;
        int len=0;
        int T=1;
        while(gets(tmp)!=NULL)
        {
            if(strcmp(tmp,"")==0)
            {
                if(len<8)
                    continue;
                len=0;
                int cnt=0;
                for(i=0;i<8;i++)
                {
                    for(j=0;j<8;j++)
                    {
                        if(ch[i][j]=='k')
                        {
                            B.x=i;
                            B.y=j;
                        }
                        else if(ch[i][j]=='K')
                        {
                            W.x=i;
                            W.y=j;
                        }
                        else if(ch[i][j]=='.')
                        {
                            cnt++;
                        }
                    }                
                }
                if(cnt==64)
                    break;
                if(isRQB(B,0)||isK(B,0)||isP(B,0)||isN(B,0))
                {
                    printf("Game #%d: black king is in check.
    ",T);
                }
                else if(isRQB(W,1)||isK(W,1)||isP(W,1)||isN(W,1))
                {
                    printf("Game #%d: white king is in check.
    ",T);
                }
                else
                {
                    printf("Game #%d: no king is in check.
    ",T);
                }
                T++;
            }
            else
            {
                strcpy(ch[len],tmp);
                len++;
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    thinkinginjava学习笔记07_多态
    thinkinginjava学习笔记06_复用类
    thinkinginjava学习笔记05_访问权限
    thinkinginjava学习笔记04_初始化与清理
    thinkinginjava学习笔记03_基本语法
    thinkinginjava学习笔记02_对象
    关于药物刺激引起的突变
    通路共出现
    关于reference-free去卷积
    一些研究生期间的反思
  • 原文地址:https://www.cnblogs.com/varcom/p/4028122.html
Copyright © 2020-2023  润新知