[置顶] 挑战编程

第一章 入门

Chapter 1

100 - The 3n + 1 problem


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=1111111;

int f[maxn];

int main()
{
    int x,y;
    int ans,ret;
    memset(f,0,sizeof(f));
    while (~scanf("%d%d",&x,&y))
    {
        ans=0;
        for (int i=min(x,y);i<=max(x,y);i++)
        {
            if (f[i]) ret=f[i];
            else
            {
                ret=1;
                int n=i;
                while (n!=1)
                {
                    if (n&1) n=3*n+1;
                    else n/=2;
                    ret++;
                }
                f[i]=ret;
            }
            if (ret>ans) ans=ret;
        }
        printf("%d %d %d\n",x,y,ans);
    }
    return 0;
}


10189 - Minesweeper


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int direct[8][2]={ {1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1} };

char s[111][111];
int f[111][111];
int n,m;

bool check(int x,int y)
{
    if (x>=1&&x<=n&&y>=1&&y<=m) return true;
    return false;
}

int main()
{
    int cnt=0;
    while (~scanf("%d%d",&n,&m))
    {
        if (n==0&&m==0) break;
        memset(f,0,sizeof(f));
        for (int i=1;i<=n;i++)
        {
            scanf("%s",s[i]+1);
        }
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                if (s[i][j]=='*')
                {
                    for (int k=0;k<8;k++)
                    {
                        int x=i+direct[k][0];
                        int y=j+direct[k][1];
                        if (check(x,y)) f[x][y]++;
                    }
                }
            }
        }
        if (cnt>0) puts("");
        printf("Field #%d:\n",++cnt);
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                if (s[i][j]=='*') printf("*");
                else printf("%d",f[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}



10137 - The Trip


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=111111;
double a[maxn];

int main()
{
    int n;
    double sum,avg,ans1,ans2;
    while (~scanf("%d",&n))
    {
        if (n==0) break;
        sum=0;
        for (int i=0;i<n;i++)
        {
            scanf("%lf",&a[i]);
            sum+=a[i];
        }
        avg=sum/n;
        ans1=0;
        ans2=0;
        for (int i=0;i<n;i++)
        {
            if (a[i]<avg) ans1+=double(int((avg-a[i])*100))/100.0;
            if (avg<a[i]) ans2+=double(int((a[i]-avg)*100))/100.0;
        }
        printf("$%0.2f\n",max(ans1,ans2));
    }
    return 0;
}



10267 - Graphical Editor

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

using namespace std;

const int direct[4][2]={ {0,1},{1,0},{0,-1},{-1,0} };
char map[333][333];
int n,m;

void set(char c)
{
    for (int i=1; i<=n; i++)
    {
        for (int j=1; j<=m; j++)
        {
            map[i][j]=c;
        }
    }
}

void print(int x1,int y1,int x2,int y2,char c)
{
    for (int i=y1;i<=y2&&i<=n;i++)
    {
        for (int j=x1;j<=x2&&j<=m;j++)
        {
            map[i][j]=c;
        }
    }
}

struct POINT{
    int x;
    int y;
};


int main()
{
    char c,chr;
    int x,y;
    int x1,x2,y1,y2;
    char name[333];
    while (cin>>c)
    {
        if (c=='X') break;
        else if (c=='I')
        {
            cin>>m>>n;
            set('O');
        }
        else if (c=='C')
        {
            set('O');
        }
        else if (c=='L')
        {
            cin>>x>>y>>chr;
            map[y][x]=chr;
        }
        else if (c=='V')
        {
            cin>>x>>y1>>y2>>chr;
            if (y1>y2) swap(y1,y2);
            print(x,y1,x,y2,chr);
        }
        else if (c=='H')
        {
            cin>>x1>>x2>>y>>chr;
            if (x1>x2) swap(x1,x2);
            print(x1,y,x2,y,chr);
        }
        else if (c=='K')
        {
            cin>>x1>>y1>>x2>>y2>>chr;
            if (x1>x2) swap(x1,x2);
            if (y1>y2) swap(y1,y2);
            print(x1,y1,x2,y2,chr);
        }
        else if (c=='F')
        {
            cin>>x>>y>>chr;
            queue<POINT>que;
            while (!que.empty()) que.pop();
            POINT p;
            p.x=y;
            p.y=x;
            char tel=map[p.x][p.y];
            if (tel==chr) continue;
            que.push(p);
            map[p.x][p.y]=chr;
            while (!que.empty())
            {
                POINT tmp=que.front();
                que.pop();
                for (int i=0;i<4;i++)
                {
                    p.x=tmp.x+direct[i][0];
                    p.y=tmp.y+direct[i][1];
                    if (p.x>=1&&p.x<=n&&p.y>=1&&p.y<=m&&map[p.x][p.y]==tel)
                    {
                        map[p.x][p.y]=chr;
                        que.push(p);
                    }
                }
            }
        }
        else if (c=='S')
        {
            cin>>name;
            cout<<name<<endl;
            for (int i=1;i<=n;i++)
            {
                for (int j=1;j<=m;j++)
                {
                    cout<<map[i][j];
                }
                cout<<endl;
            }
        }
        else
        {
            continue;
        }
    }
    return 0;
}



第二章 数据结构

Chapter 2


10038 - Jolly Jumpers

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=3333;
bool v[maxn];
int a[maxn];
int n;
int sum;

int main()
{
    while (cin>>n)
    {
        sum=0;
        memset(v,0,sizeof(v));
        for (int i=1;i<=n;i++) cin>>a[i];
        for (int i=2;i<=n;i++)
        {
            int t=abs(a[i]-a[i-1]);
            if (!v[t]&&t>=1&&t<=n-1)
            {
                v[t]=true;
                sum++;
            }
        }
        if (sum==n-1) puts("Jolly");
        else puts("Not jolly");
    }
    return 0;
}



第三章 字符串

Chapter 3


10010 - Where's Waldorf?


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <ctype.h>
using namespace std;

const int direct[8][2]= { {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };

char map[55][55];
int n,m;
bool v[55][55];
char s[55];
int len;

bool check(int x,int y)
{
    if (x>=1&&x<=n&&y>=1&&y<=m) return true;
    return false;
}

bool dfs(int x,int y,int deep,int g)
{
    if (deep>=len) return true;
    if (!check(x,y)) return false;
    if (map[x][y]!=s[deep]) return false;
    int dx=x+direct[g][0];
    int dy=y+direct[g][1];
    if (dfs(dx,dy,deep+1,g)) return true;
    return false;
}

int main()
{
    int T,k;
    cin>>T;
    while (T--)
    {
        cin>>n>>m;
        for (int i=1; i<=n; i++) cin>>(map[i]+1);
        for (int i=1; i<=n; i++) for (int j=1; j<=m; j++)
            if (islower(map[i][j])) map[i][j]=toupper(map[i][j]);
        cin>>k;
        //for (int i=1; i<=n; i++) cerr<<(map[i]+1)<<endl;
        while (k--)
        {
            int i,j,g;
            bool ok=false;
            cin>>s;
            len=strlen(s);
            for (i=0; i<len; i++)
                if (islower(s[i])) s[i]=toupper(s[i]);
            //cerr<<s<<endl;
            for (i=1; i<=n; i++)
            {
                for (j=1; j<=m; j++)
                {
                    for (g=0; g<8; g++)
                    {
                        if (dfs(i,j,0,g))
                        {
                            ok=true;
                            cout<<i<<" "<<j<<endl;
                            break;
                        }
                    }
                    if (ok) break;
                }
                if (ok) break;
            }
        }
        if (T) cout<<endl;
    }
    return 0;
}

10188 - Automated Judge Script


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

vector<string>ac,sub;
int n,m;

bool accepted()
{
    for (int i=0;i<n;i++)
    {
        if (ac[i]!=sub[i]) return false;
    }
    return true;
}

bool PE()
{
    string num;
    string ans;
    num.clear();
    ans.clear();
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<ac[i].size();j++)
        {
            if (isdigit(ac[i][j])) num+=ac[i][j];
        }
    }
    for (int i=0;i<m;i++)
    {
        for (int j=0;j<sub[i].size();j++)
        {
            if (isdigit(sub[i][j])) ans+=sub[i][j];
        }
    }
    if (ans==num) return true;
    /*
    cerr<<"---"<<ans<<"---"<<num<<"---";
    if(ans==num) cerr<<"same"<<endl;
    else cerr<<"not"<<endl;
    */
    return false;
}

int main()
{
    string s;
    int cnt=0;
    while (cin>>n)
    {
        if (n==0) break;
        ac.clear();
        sub.clear();
        getchar();
        for (int i=0;i<n;i++)
        {
            getline(cin,s);
            ac.push_back(s);
        }
        cin>>m;
        getchar();
        for (int i=0;i<m;i++)
        {
            getline(cin,s);
            sub.push_back(s);
        }
        if (n==m&&accepted())
        {
            cout<<"Run #"<<++cnt<<": Accepted"<<endl;
        }
        else if (PE())
        {
            cout<<"Run #"<<++cnt<<": Presentation Error"<<endl;
        }
        else
        {
            cout<<"Run #"<<++cnt<<": Wrong Answer"<<endl;
        }
    }
    return 0;
}




第四章 排序

Chapter 4


10041 - Vito's Family


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int a[555];
int n;

int main()
{
    int T;
    int ans;
    cin>>T;
    while (T--)
    {
        cin>>n;
        ans=0;
        for (int i=1;i<=n;i++) cin>>a[i];
        sort(a+1,a+n+1);
        int m=n/2+1;
        for (int i=1;i<=n;i++) ans+=abs(a[i]-a[m]);
        cout<<ans<<endl;
    }
    return 0;
}


第五章 算数与代数

Chapter 5


10035 - Primary Arithmetic


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int a[111];
int b[111];
char sa[111];
char sb[111];
int lena,lenb;

int main()
{
    while (cin>>sa>>sb)
    {
        if (strcmp(sa,sb)==0&&strcmp(sa,"0")==0) break;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        lena=strlen(sa);
        lenb=strlen(sb);
        for (int i=0;i<lena;i++) a[i]=sa[lena-i-1]-'0';
        for (int i=0;i<lenb;i++) b[i]=sb[lenb-i-1]-'0';
        int ans=0;
        for (int i=0;i<max(lena,lenb);i++)
        {
            a[i]+=b[i];
            if (a[i]>=10)
            {
                a[i+1]+=1;
                a[i]-=10;
                ans++;
            }
        }
        if (ans==1) cout<<ans<<" carry operation."<<endl;
        else if (ans>1) cout<<ans<<" carry operations."<<endl;
        else cout<<"No carry operation."<<endl;
    }
    return 0;
}




第六章 组合数学

Chapter 6




第七章 数论

Chapter 7




第八章 回溯法

Chapter 8




第九章 图遍历

Chapter 9




第十章 图算法

Chapter 10




第十一章 动态规划

Chapter 11




第十二章 网络

Chapter 12




第十三章 几何

Chapter 13




第十四章 计算几何

Chapter 14





posted on 2013-06-14 11:45  电子幼体  阅读(107)  评论(0编辑  收藏  举报

导航