Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

纯考编程能力的题,两个加数和最终结果都要从前到后扫一遍,再从后往前扫一遍才行

Posted by jhljx at 2015-02-27 12:22:50 on Problem 2116 and last updated at 2015-02-27 13:50:59
防止出现相邻的1
11011001 1101100111这种数据如果只是一个方向处理一遍会跪。。

贴个长代码。。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;

char str1[50],str2[50];
char a[50],b[50],c[50];
int solve(char a[])
{
    int k=50;
    while(k>=0&&a[k]==0) k--;
    while(k>=0)
    {
        if(a[k]>=1)
        {
            while(k-1>=0&&(a[k-1]>=1&&a[k]>=1))
            {
                a[k+1]++,a[k]--,a[k-1]--;
                if(a[k]==0)
                {
                    k--;
                    break;
                }
                else if(a[k-1]==0)
                {
                    k-=2;
                    break;
                }
            }
            if(k-1>=0&&a[k-1]==0)
            {
                while(a[k]>=2)
                {
                    if(k>1)
                        a[k]-=2,a[k+1]++,a[k-2]++;
                    else if(k==1)
                        a[k]-=2,a[k+1]++,a[k-1]++;
                    else
                    {
                        k--;
                        break;
                    }
                }
                k--;
            }
            else if(k>=0) k--;
        }
        else k--;
    }
    k=0;
    while(k<50&&a[k]==0) k++;
    if(k==0)
    {
        while(a[0]>=2)
        {
            a[1]++;
            a[0]-=2;
        }
    }
    while(k<50)
    {
        if(a[k]>=1)
        {
            while(k+1<50&&(a[k+1]>=1&&a[k]>=1))
            {
                a[k+2]++,a[k]--,a[k+1]--;
                if(a[k]==0)
                {
                    k++;
                    break;
                }
                else if(a[k+1]==0)
                {
                    k+=2;
                    break;
                }
            }
            if(k+1<=50&&a[k+1]==0)
            {
                while(a[k]>=2)
                {
                    if(k>1)
                        a[k]-=2,a[k+1]++,a[k-2]++;
                    else if(k==1)
                        a[k]-=2,a[k+1]++,a[k-1]++;
                    else
                    {
                        k++;
                        break;
                    }
                }
                k++;
            }
            else if(k+1>50) k++;
        }
        else k++;
    }
    k=50;
    while(k>=0&&a[k]==0) k--;
    return k;
}

void print(int ka,int shu,int num)
{
    if(num==1) printf("  ");
    if(num==2) printf("+ ");
    if(ka>=0)
    {
        for(int i=0; i<shu-ka; i++)
            printf(" ");
        for(int i=ka; i>=0; i--)
        {
            if(num==1)
                printf("%d",a[i]);
            else if(num==2)
                printf("%d",b[i]);
        }
    }
    else
    {
        for(int i=0; i<shu; i++)
            printf(" ");
        printf("0");
    }
    printf("\n");
}

void print1(int k,int shu)
{
    printf("  ");
    if(shu>=0)
    {
        for(int i=shu; i>=0; i--)
        {
            if(k==1) printf("-");
            else printf("%d",c[i]);
        }
    }
    else
    {
        if(k==1) printf("-");
        else printf("0");
    }
    printf("\n");
    if(k==2) printf("\n");
}

int main()
{
    while(~scanf("%s%s",str1,str2))
    {
        CLR(a);CLR(b);CLR(c);
        int k1=strlen(str1),k2=strlen(str2);
        for(int i=0; i<k1; i++)
            a[i]=str1[k1-1-i]-'0';
        for(int i=0; i<k2; i++)
            b[i]=str2[k2-1-i]-'0';
        int ka=solve(a);
        int kb=solve(b);
        for(int i=50; i>=0; i--)
            c[i]=a[i]+b[i];
        int kc=solve(c);
        int shu=max(max(ka,kb),kc);
        print(ka,shu,1);print(kb,shu,2);print1(1,shu);print1(2,shu);
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator