zoukankan      html  css  js  c++  java
  • 杭电1002

    #include <iostream>
    #include <string>
    #include <string.h>
    #include <stdlib.h>
    using namespace std;
    
    int main()
    {
    	int a[1002];
    	int b[1002];
    	int n;
    	char aa[1002];
    	char bb[1002];
    	cin>>n;
    	int line = 1;
    	while (line <= n)
    	{	
    		cin>>aa>>bb;
    		memset(a, 0, sizeof(a));       //不要硬编码,采用sizeof,因为是int是4字节,之前自己给自己埋了个坑,写成1002。。。
    		memset(b, 0, sizeof(b));
    
    		int strLenA = strlen(aa);
    		for (int i = 0; i < strLenA; i++)    //数组逆转存入
    		{
    			a[i] = aa[strLenA - 1 - i] - '0';
    		}
    
    		int strLenB = strlen(bb);
    		for (int j = 0; j < strLenB; j++)
    		{
    			b[j] = bb[strLenB - 1 - j] - '0';
    		}
    
    		int maxLen = strLenA > strLenB ? strLenA : strLenB;
    
    		for (int k = 0; k < maxLen; k++)   //先处理高位
    		{
    			a[k+1] += (a[k] + b[k]) / 10;
    			a[k] = (a[k]+ b[k]) % 10;
    		}
    
    		cout<<"Case "<<line<<":"<<endl;
    		cout<<aa<<" + "<<bb<<" = ";
    
    		if (a[maxLen] > 0)                //判断最高位是否有进位
    			cout<<a[maxLen];
    
    		for (int kk = maxLen - 1; kk >= 0; kk--)
    			cout<<a[kk];
    
    		cout<<endl;
    
    		if (line < n)                  //注意最后一个案例不要多输出空行
    			cout<<endl;
    		line++;
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    cf-779E (拆位)
    石子游戏 (SG函数)
    [POI2017] Flappy Bird (思维题)
    Alice and Bob (SG函数)
    Red is good (DP)
    CodeVS-1669 (背包问题)
    GalaxyOJ-468 (LCA)
    BZOJ-1191 (二分图匹配)
    Reinforcement Learning 笔记(4)
    Reinforcement Learning 笔记(3)
  • 原文地址:https://www.cnblogs.com/AC-LG/p/4645514.html
Copyright © 2011-2022 走看看