• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
金大鑫要坚持
博客园    首页    新随笔    联系   管理    订阅  订阅

ACM Coder [T1002] 一直wrong answer,不知道为什么。上代码!就对就对!

忘了改了什么,后来居然对了!做打不死的菜鸟!

#include <stdio.h>
#include <stdbool.h>
#define arrayLength 20
#define bitMax 1000

main(){
    int caseCount = 0;
    scanf_s("%d", &caseCount);
    
        char inputA[arrayLength][bitMax];
        char inputB[arrayLength][bitMax];
        char result[arrayLength][bitMax + 1];

        for (int i = 0; i < caseCount; i++){

            scanf_s("%s", &inputA[i][0], 1000);
            scanf_s("%s", &inputB[i][0], 1000);
        }
        //Calculating... result[0],result[1]
        for (int caseNum = 0; caseNum < caseCount; caseNum++)
        {
            char *ptrA, *ptrB, *ptrResult;
            ptrA = inputA[caseNum];
            ptrB = inputB[caseNum];
            ptrResult = result[caseNum];
            int lengthA = getLength(ptrA);
            int lengthB = getLength(ptrB);
            int lengthMax = lengthA>lengthB ? lengthA : lengthB;
            //Fill A or B with zero
            if (lengthA > lengthB){
                ptrB += lengthB;
                for (int j = 0; j <= lengthB; j++)
                {
                    char temp = *ptrB;
                    *(ptrB + lengthA - lengthB) = temp;
                    ptrB--;
                }
                ptrB = inputB[caseNum];
                for (int i = 0; i < lengthA - lengthB; i++){
                    *ptrB = '0';
                    ptrB++;
                }
            }
            else
            {
                ptrA += lengthA;
                for (int j = 0; j <= lengthA; j++)
                {
                    char temp = *ptrA;
                    *(ptrA + lengthB - lengthA) = temp;
                    ptrA--;
                }
                ptrA = inputA[caseNum];
                for (int i = 0; i < lengthB - lengthA; i++){
                    *ptrA = '0';
                    ptrA++;
                }
            }
            for (int i = 0; i <= lengthMax; i++){
                *ptrResult = '0';
                ptrResult++;
            }
            *ptrResult = '\0';
            bool goAhead = false;
            ptrA = &inputA[caseNum][lengthMax - 1];
            ptrB = &inputB[caseNum][lengthMax - 1];
            ptrResult--;
            //Calculate result[i]
            for (int i = 0; i < lengthMax; i++)
            {

                int bitA = *ptrA - '0';
                int bitB = *ptrB - '0';
                *ptrResult = '0' + (bitA + bitB + goAhead) % 10;
                if (bitA + bitB + goAhead >= 10){
                    goAhead = true;
                }
                else
                {
                    goAhead = false;
                }
                ptrA--;
                ptrB--;
                ptrResult--;
            }
            if (goAhead == true){
                *ptrResult = '1';
            }
            else
            {
                ptrResult++;
            }
            ptrA++;
            ptrB++;
            for (int bitNum = 0; bitNum < lengthMax; bitNum++){
                if (*ptrA == '0')
                    ptrA++;
                else
                {
                    break;
                }
            }

            for (int bitNum = 0; bitNum < lengthMax; bitNum++){
                if (*ptrB == '0')
                    ptrB++;
                else
                {
                    break;
                }
            }
            if (caseNum != caseCount-1)
                printf("Case %d:\n%s + %s = %s\n\n", caseNum + 1, ptrA, ptrB, ptrResult);
            else
            {
                printf("Case %d:\n%s + %s = %s\n", caseNum + 1, ptrA, ptrB, ptrResult);
            }
        }
    
}
//get string length
int getLength(char *ptr){
    int length = 0;
    while (*ptr != '\0')
    {
        ptr++;
        length++;
    }
    return length;
}

 

 

 

Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
posted @ 2015-09-06 13:42  金大鑫要坚持  阅读(436)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3