hdu 2100 lovekey

来源:互联网 发布:实时弹幕软件 编辑:程序博客网 时间:2024/06/06 07:00

题意:开始被题目迷惑了,其实很简单就是给你两个26进制的数,求他们的和;

解法:大数加法!!最近在学链表,所以用链表写的,注意前导零不输出,还要注意AAA   AA这组数据是不输出东西的。


 

#include<bits/stdc++.h>using namespace std;int n,m;struct node{    char val;    node *next;    node(char c):val(c),next(NULL){}}*root;void print(node *head){    node *p=head->next;    while(p)    {        if(p->val!='A')            break;        else            p=p->next;    }    while(p)    {        printf("%c",p->val);        p=p->next;    }    printf("\n");}void adds(node *hea,node *hea1){    node *p=hea->next,*q=hea1->next,*hea2,*tmp;    int sum,car=0;    hea2=new node('0');    if(n>m)    {        p=hea1->next;        q=hea->next;    }    while(p)    {        sum=p->val-'A'+q->val-'A'+car;        car=sum/26;        sum=sum%26;        tmp=new node(sum+'A');        tmp->next=hea2->next;        hea2->next=tmp;        p=p->next;        q=q->next;    }    while(q)    {        sum=q->val-'A'+car;        car=sum/26;        sum=sum%26;        tmp=new node(sum+'A');        tmp->next=hea2->next;        hea2->next=tmp;        q=q->next;    }    if(car)    {        tmp=new node('B');        tmp->next=hea2->next;        hea2->next=tmp;    }    print(hea2);}int main(){    char a[205],b[205];    while(scanf("%s%s",a,b)==2)    {        n=strlen(a),m=strlen(b);        node *hea,*hea1,*tmp;        hea=new node('0');        hea1=new node('0');        for(int i=0;i<n;i++)        {            tmp=new node(a[i]);            tmp->next=hea->next;            hea->next=tmp;        }        for(int i=0;i<m;i++)        {            tmp=new node(b[i]);            tmp->next=hea1->next;            hea1->next=tmp;        }        adds(hea,hea1);    }    return 0;}


0 0
原创粉丝点击