2015 湘潭大学程序设计比赛(Internet) 全解析 + AC代码

一到搜索卡死人...


1. 两个变量记录等级和经验值就好.

int main(){
    int T;
    scanf("%d",&T);

    while(T--){
        int n,l;
        int jy=0;
        int sum = 0;
        scanf("%d%d",&n,&l);
        for(int i = 0;i<n;i++){
            int t;
            scanf("%d",&t);
            if(l<t) continue;
            jy += 10/(l-t+1);
            sum += 10/(l-t+1);
            if(jy>=100)
                l++,jy -= 100;
        }
        printf("%d %d\n",l,sum);
    }
    return 0;
}

2. 数字对应相加,注意进位处理和最后全部处理完成后跳出循环的进位

char str[100];
int main(){
    int T;
    scanf("%d",&T);

    while(T--){
        int n,num[10];
        scanf("%d",&n);
        int len = 0;
        int m = n;
        while(n)
            num[len++] = n%10,n/=10;
        if(len==1 || m==0){
            printf("%d\n",m);
            continue;
        }
        int add = 0;
        int length = 0;
        if(len&1){
            str[length++]=num[len/2]+'0';
        }
        for(int i = len/2-1;i>=0;i--){
            int t = num[i] + num[len-i-1] + add;
            str[length++] = t%10 + '0';
            add = t/10;
        }
        if(add)
            str[length++] = add + '0';
        for(int i = length-1;i>=0;i--)
            printf("%c",str[i]);
        printf("\n");
    }
    return 0;
}


3. 第n个数字是多少,这个要注意 

(1) 个位单独处理. 

(2),就是整除情况.  这个在数字规律中经常容易犯错

(3) 用递推要开long long

int main(){
    int T;
    scanf("%d",&T);
    long long mm[20];
    long long num[20];
    num[1] = 9;
    mm[1]=1;
    mm[0]=num[0]=0;
    for(long long i = 2,t=9;i<20;i++){
        t = t*10;
        num[i] = i*t + num[i-1];
        mm[i] = mm[i-1]*10;
    }
    mm[1]=0;
    while(T--){
        int n;
        scanf("%d",&n);
        if(n<10) {
            printf("%d\n",n);
            continue;
        }
        int loc = 1;
        for(int i = 1;i<20;i++)
        if(num[i]>=n) {loc = i; n -= num[loc-1];break;}
        //else if(num[i]==n){loc = i; n -= num[loc];break;}
        int tag = 0;
        if(n%loc==0) tag=-1;
        long long ans = mm[loc] + n/loc +tag;
        for(int i = 0;i<loc;i++){
            if((loc-i)%loc==n%loc){
                printf("%d\n",ans%10);
                break;
            }ans/=10;
        }

    }
    return 0;
}

4. 直接贪心,从当前位置向后min(m,len(str)) 找到最小的换过来. 题意是要求输出前导0的,因为这个wa了很久

int main(){

    int T;
    scanf("%d",&T);

    while(T--){
        int m;
        getchar();
        scanf("%s%d",s,&m);
        memset(ans,0,sizeof(ans));
        int len = strlen(s);
        for(int i = 0;i<len;i++){
            int k = 0;
            int n = 0;
            int b = 0;
            for(;k<len;k++) if(s[k]!=-1) break;
            for(int j = k;n<=m && j<len ;n++,j++){
                if(s[j]==-1) {n--;continue;}
                if(i==0 && s[j]=='0') continue;
                if(s[k] > s[j]) k = j,b=n;
            }
            ans[i] = s[k];
            s[k] = -1;
            m -= b;
        }
        printf("%s\n",ans);
    }
    return 0;
}

5. 矩阵累计和......

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int s[1005][1005],l[1005][1005],h[1005][1005];
int T,n,m,q;
int main(){
    #ifndef ONLINE_JUDGE
    //freopen("in.txt","r",stdin);
    #endif

    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&n,&m,&q);
        memset(s,0,sizeof(s));
        memset(l,0,sizeof(l));
        memset(h,0,sizeof(h));
        int tmp;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%d",&tmp);
                l[i][j]=l[i][j-1]^tmp;
                h[i][j]=h[i-1][j]^tmp;
                s[i][j]=s[i-1][j-1]^tmp^l[i][j-1]^h[i-1][j];
            }
        }
        for(int i=1;i<=m;i++){
            //printf("%d %d\n",s[1][i],l[1][i]);
        }
        int x1,y1,x2,y2;
        while(q--){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            int sum=0;
            sum^=s[x1-1][y1-1];
            sum^=s[x1-1][m]^s[x1-1][y2];
            sum^=s[x2][y2]^s[x1-1][y1-1]^(s[x1-1][y2]^s[x1-1][y1-1])^(s[x2][y1-1]^s[x1-1][y1-1]);
            sum^=s[n][y1-1]^s[x2][y1-1];
            sum^=s[n][m]^s[x2][y2]^(s[x2][m]^s[x2][y2])^(s[n][y2]^s[x2][y2]);
            printf("%d\n",sum);
        }

    }
}

6.  这个搜索不知道哪里有问题....用优先队列和vis[x][y][l]判重.可以循环走,取模即可

#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;
const int maxn=1100;
const int INF=0x3f3f3f3f;
int N,K,L;
int fx[4][2]={-1,0, 0,1, 1,0, 0,-1};
int vis[55][55][110];
int jump[55][55][2];
char mp[55][55];
struct P{
    P(){}
    P(int x,int y ,int l,int s):x(x),y(y),l(l),s(s){}
    int x,y,l,s;
    friend bool operator < (const P& a,const P&b) {
        return a.s>b.s;
    }
}st,ed;
int solve(){

    priority_queue<P> q;
    q.push(st);
    vis[st.x][st.y][st.l] = 1;
    while(q.size()){
        P now = q.top();q.pop();
        if(mp[now.x][now.y] =='$') return now.s;
        int jx = jump[now.x][now.y][0];
        int jy = jump[now.x][now.y][1];
        if(jx!=-1 && jy!=-1 && mp[jx][jy] != '#' && now.l>0 && vis[jx][jy][now.l-1]==0){
            vis[jx][jy][now.l-1]=1;
            q.push(P(jx,jy,now.l-1,now.s));
        }
        for(int i = 0;i<4;i++){
            jx = (now.x + fx[i][0]+N)%N;
            jy = (now.y + fx[i][1]+N)%N;
            if(mp[jx][jy]=='#' || vis[jx][jy][now.l]) continue;
            vis[jx][jy][now.l]=1;
            q.push(P(jx,jy,now.l,now.s+1));
        }
    }

    return INF;
}
int main(){

    while(~scanf("%d%d%d",&N,&K,&L)){
        memset(jump,-1,sizeof(jump));
        memset(vis,0,sizeof(vis));
        for(int i = 0;i<K;i++){
            int a,b,c,d;
            scanf("%d%d%d%d",&a,&b,&c,&d);
            a--,b--,c--,d--;
            jump[a][b][0]=c;
            jump[a][b][1]=d;
        }
        getchar();
        for(int i = 0;i<N;i++){
            gets(mp[i]);
            for(int j = 0;j<N;j++){
                if(mp[i][j]=='@') st.x = i,st.y = j,st.s = 0,st.l = L;
                if(mp[i][j]=='$') ed.x = i,ed.y = j;
            }
        }
        int ans = solve();
        if(ans!=INF) printf("%d\n",ans);
        else printf("Poor man!\n");
    }
    return 0;
}



7. 直接递推就搞定,注意取模

using namespace std;
const int maxn=1100;
const int INF=0x3f3f3f3f;
int N,K,L;
int values[510][510];
int path[510][510];
int mp[510][510];

int main(){
    while(~scanf("%d",&N)){
        for(int i = 0;i<N;i++)
            for(int j = 0;j<N;j++)
                scanf("%d",mp[i]+j);
        path[0][0]=1;
        values[0][0] = mp[0][0];
        for(int i = 1;i<N;i++)
            values[0][i] = values[0][i-1] + mp[0][i],
            path[0][i] = 1;
        for(int i = 1;i<N;i++)
            values[i][0] = values[i-1][0] + mp[i][0],
            path[i][0] = 1;
        for(int i = 1;i<N;i++)
        for(int j = 1;j<N;j++){
            if(values[i-1][j] > values[i][j-1])
                values[i][j] = values[i-1][j] + mp[i][j],path[i][j] = path[i-1][j];
            else if(values[i-1][j] == values[i][j-1])
                values[i][j] = values[i-1][j] + mp[i][j],path[i][j] = (path[i-1][j] + path[i][j-1])%123456;
            else
                values[i][j] = values[i][j-1] + mp[i][j],path[i][j] = path[i][j-1];
        }
        printf("%d\n",path[N-1][N-1]%123456);

    }
    return 0;
}

8. 括号匹配,用个STL 加上累计到该位置的累计成功匹配括号数即可 

ps: 字符串长度千万不要在循环里面,因为这个TEL无数次 ...还有就是注意输入的可能是右括号位置.

char str[maxn];
int match[maxn];
int tree[3][maxn];
void insert(int *a,int x){
    while(x<maxn){
        a[x] ++;
        x += lowbit(x);
    }
}
int get_sum(int *a,int x){
    int res = 0;
    while(x>0){
        res += a[x];
        x -= lowbit(x);
    }
    return res;
}
int main(){

    while(~scanf("%s",str+1)){
        stack<int> ss;
        int s,m,l,n;
        s=m=l=0;
        memset(tree,0,sizeof(tree));
        m=strlen(str+1);
        for(int i = 1;i<=m;i++){
            if(str[i]=='(' || str[i]=='[' || str[i]=='{')
                ss.push(i);
            else{
                int t = ss.top();
                ss.pop();
                match[t]=i;
                match[i]=t;
                if(str[i]==')') insert(tree[0],t);
                else if(str[i]==']') insert(tree[1],t);
                else if(str[i]=='}') insert(tree[2],t);
            }

        }
        scanf("%d",&n);
        for(int i = 0;i<n;i++){
            int t;
            scanf("%d",&t);
            int c=match[t];
            printf("%d",c);
            if(match[t]<t) c=t,t=match[t];
            printf(" %d %d %d\n",get_sum(tree[0],c)-get_sum(tree[0],t),get_sum(tree[1],c)-get_sum(tree[1],t),get_sum(tree[2],c)-get_sum(tree[2],t));
        }
        printf("\n");
    }

    return 0;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。