现在的位置: 首页 > 综合 > 正文

HDU 1230 火星A+B 字符串

2013年07月18日 ⁄ 综合 ⁄ 共 1108字 ⁄ 字号 评论关闭

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1230

一道基本的字符串,做多了这类的题目,各种思路都可以出来。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;

/*
freopen("input.txt",  "r", stdin);  //读数据
freopen("output.txt", "w", stdout); //注释掉此句则输出到控制台
*/
int prime[30],num[100];
void xiaohao()
{
    memset(num,0,sizeof(num));
    int t=1,i,j;
    for(i=2;i<100;i++)
        if((!num[i])&&t<30)
        {
            prime[t++]=i;
            for(j=i;j<100;j+=i)
                num[j]=1;
        }
}

int xh[30],a[30],b[30];

int main()
{
    int i1,i2,n,i,j,temp,k,mmax;
    char c;
    xiaohao();//求素数
    while(520)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        i1=0;
        while(520)
        {
            scanf("%d%c",&a[++i1],&c);
            if(c==' ')
                break;
        }
        i2=0;
        while(520)
        {
            scanf("%d%c",&b[++i2],&c);
            if(c=='\n')
                break;
        }
        if((a[1]+b[1])==0)  break;
        for(i=1;i<=i1/2;i++)
            swap(a[i],a[i1-i+1]);
        for(i=1;i<=i2/2;i++)
            swap(b[i],b[i2-i+1]);
        mmax=i1>i2?i1:i2;
        temp=0;k=0;
        for(i=1;i<=mmax;i++)
        {
            k++;
            xh[k]=a[i]+b[i]+temp;
            temp=xh[k]/prime[i];
            xh[k]%=prime[i];
        }
        while(temp)
        {
            k++;
            xh[k]=temp%prime[k];
            temp/=prime[k];
        }
        for(i=k;i>1;i--)
            printf("%d,",xh[i]);
        printf("%d\n",xh[1]);
    }
    return 0;
}

抱歉!评论已关闭.