杭电OJ A + B Problem II

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 310934    Accepted Submission(s): 60113


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
 

Author
Ignatius.L
//2016年6月17日11:38:03
# include <stdio.h>
# include <string.h>
int main (void)
{
    int T, i;
    int iSum[1000];
    char cNum1[1000], cNum2[1000];
    int iCount1, iCount2;
    int t, j, g, count;
    scanf ("%d", &T);
    for (i = 0; i < T; i++)
    {
        t = 0;
        j = 0;
        count = 0;
        scanf ("%s %s", cNum1, cNum2);
        printf ("Case %d:\n", i+1);
        printf ("%s + %s = ", cNum1, cNum2);
        iCount1 = strlen(cNum1)-1;
        iCount2 = strlen(cNum2)-1;
        while (iCount1 >= 0 && iCount2 >= 0)
        {
            iSum[j] = cNum1[iCount1]-'0'+cNum2[iCount2]-'0'+t;
            t = iSum[j]/10;
            iSum[j] = iSum[j]%10;
            j++;
            iCount1--;
            iCount2--;
            count++;
        }
        while (iCount1 >= 0)
        {
            iSum[j] = cNum1[iCount1]-'0'+t;
            t = iSum[j]/10;
            iCount1--;
            j++;
        }
        while (iCount2 >= 0)
        {
            iSum[j] = cNum2[iCount2]-'0'+t;
            t = iSum[j]/10;
            iCount2--;
            j++;
        }
        for (g=j-1; g>=0; g--)
        {
            printf ("%d", iSum[g]);
        }
        printf ("\n");
        if (i+1!=T)
            printf ("\n");
    }
    return 0;
}
/*------------------------
2
1 2
Case 1:
1 + 2 = 3

112233445566778899 998877665544332211
Case 2:
112233445566778899 + 998877665544332211 = 111111111111111110
Press any key to continue...
----------------------*/


 
### 杭电OJ C++编程题解与解决方案 杭电OJ是一个重要的编程技能提升平台,它提供了丰富的题目资源,帮助学习者掌握基础算法、数据结构以及高级编程技术[^3]。以下是针对杭电OJ上常见的C++编程问题及其解决方法的一些讨论。 #### 题目类型分析 杭电OJ中的题目通常涉及以下几个方面: 1. **基本控制流**:如`for`, `while`循环的应用。 2. **数组操作**:包括一维数组和二维数组的操作。 3. **字符串处理**:涉及到字符匹配、子串提取等问题。 4. **数学逻辑**:利用数论知识解决问题。 5. **指针应用**:深入理解内存管理机制。 6. **复杂数据结构**:链表、栈、队列等的实际运用。 下面给出几个具体的例子并附带相应的代码实现: #### 示例一:反转整数并与原数值比较 此问题是关于如何逆向构建一个整数,并验证其特定条件下的性质。 ```cpp #include <iostream> using namespace std; // 定义函数用于计算给定数字a的反序形式 int reverseInt(int a){ int res = 0; while(a != 0){ res = res * 10 + a % 10; a /= 10; } return res; } int main(){ bool found = false; for(int i = 1000; i <= 1111; ++i){ if(reverseInt(i) == i * 9){ cout << i << endl; found = true; } } if(!found){ cout << "No such number exists." << endl; } } ``` 上述程序展示了如何遍历一定范围内的所有可能值,并检查它们满足某些特殊关系的能力[^1]。 #### 示例二:双指针法寻找符合条件的一对索引 当面对需要优化时间复杂度的任务时,“双指针”是一种非常有效的策略。 假设我们有一个未排序列表,目标是从其中找到两个元素使得它们之差等于指定常量k,则可以通过如下方式高效求解: ```cpp #include <vector> #include <algorithm> // sort function needed here. using namespace std; pair<int, int> findPairWithDifference(const vector<int>& nums, const int k){ pair<int,int> result(-1,-1); // Sort the array first to apply two pointer technique effectively. vector<int> sortedNums(nums); sort(sortedNums.begin(),sortedNums.end()); size_t left = 0 ; size_t right = 1 ; while( right < sortedNums.size()){ long diff = static_cast<long>(sortedNums[right]) - static_cast<long>(sortedNums[left]); if(diff == k ){ result.first=sortedNums[left]; result.second=sortedNums[right]; break; } if(diff<k )right++; else if(diff>k &&left<right-1)left++; else{ // To avoid infinite loop when both pointers are at same position and difference is zero or negative. right++; left++; } } return result; } ``` 这里采用了先排序再用双指针扫描的方法来快速定位合适的配对项[^5]。 #### 进一步探讨——环形链表检测及入口节点确定 对于更复杂的场景比如判定单链表是否存在闭环并且找出这个闭合点的位置,可以采用快慢指针的技术路线。 ```cpp struct ListNode { int val; ListNode *next; ListNode(int x):val(x), next(NULL){} }; ListNode* detectCycle(ListNode *head){ if(head==NULL || head->next==NULL)return NULL; ListNode *slow=head,*fast=head; do{ slow=slow->next; fast=(fast->next!=NULL)?fast->next->next:NULL; }while(fast&&slow!=fast); if(slow!=fast||!fast)return NULL;// No cycle detected. slow=head; while(slow!=fast){ slow=slow->next; fast=fast->next; } return slow; } ``` 这段代码实现了不仅能够确认是否含有环路而且还能准确定位到入环首结点的功能[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值