加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数问题 hdu 1002

发布时间:2020-12-14 03:37:31 所属栏目:大数据 来源:网络整理
导读:大数问题通常用数组来模拟计算。下面给出hdu 1002 的代码。 ? #include stdio.h #include stdlib.h #include string.h #define MAX_LEN 1000 int g_a[MAX_LEN]; int g_b[MAX_LEN]; int g_ans[MAX_LEN]; char g_stra[MAX_LEN + 1]; char g_strb[MAX_LEN + 1];

大数问题通常用数组来模拟计算。下面给出hdu 1002 的代码。

?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LEN 1000
int g_a[MAX_LEN];
int g_b[MAX_LEN];
int g_ans[MAX_LEN];
char g_stra[MAX_LEN + 1];
char g_strb[MAX_LEN + 1];

int main()
{
??? int t,i,j,n;
??? int la,lb;
??? int len;
??? char startOutput;
??? //freopen("test.txt","r",stdin);

??? scanf("%d",&t);
??? for (n = 1; n <= t; ++n)
??? {
??????? scanf("%s",g_stra);
??????? scanf("%s",g_strb);
??????? la = strlen(g_stra);
??????? lb = strlen(g_strb);
??????? j = 0;
??????? for (i = la - 1; i >= 0; --i)
??????? {
??????????? g_a[j++] = g_stra[i] - '0';
??????? }
??????? j = 0;
??????? for (i = lb - 1; i >= 0; --i)
??????? {
??????????? g_b[j++] = g_strb[i] - '0';
??????? }
??????? memset(g_ans,sizeof(g_ans));
??????? for (i = 0,j = 0; i < la && j < lb; ++i,++j)
??????? {
??????????? g_ans[i] += (g_a[i] + g_b[i]);
??????????? if (g_ans[i] >= 10)
??????????? {
??????????????? g_ans[i] -= 10;
??????????????? g_ans[i+1]++;
??????????? }
??????? }
??????? while (i < la)
??????? {
??????????? g_ans[i] += g_a[i];
??????????? if (g_ans[i] >= 10)
??????????? {
??????????????? g_ans[i] -= 10;
??????????????? g_ans[i+1]++;
??????????? }
??????????? ++i;
??????? }
??????? while (j < lb)
??????? {
??????????? g_ans[j] += g_b[j];
??????????? if (g_ans[j] >= 10)
??????????? {
??????????????? g_ans[j] -= 10;
??????????????? g_ans[j+1]++;
??????????? }
??????????? ++j;
??????? }
??????? len = (i > j) ? i : j;
???????
??????? if (g_ans[len] >= 10)
??????? {
??????????? g_ans[len] -= 10;
??????????? g_ans[++len]++;
??????? }
??????? printf("Case %d:n",n);
??????? printf("%s + %s = ",g_stra,g_strb);
??????? startOutput = 0;
??????? while (len >= 0)
??????? {
??????????? if (startOutput)
??????????? {
??????????????? printf("%d",g_ans[len]);
??????????? }
??????????? else if (g_ans[len])
??????????? {
??????????????? startOutput = 1;
??????????????? printf("%d",g_ans[len]);
??????????? }
??????????? --len;
??????? }
??????? if (n != t)
??????? {
??????????? printf("nn");
??????? }
??????? else
??????????? printf("n");
???????
??? }

??? return 0; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读