10 条题解

  • 3
    @ 2023-10-8 19:56:10

    写在前面 这道题不难,我用这道题学习了一下字符串拼接和字符串加法进位,其实还想学习一下atoi函数和stoi函数,后来也没用。我这道题有很多细节在一开始并没有注意,导致出现了很多问题,想把代码分享出来,希望之后的同学注意细节。

    #include <math.h>
    #include <string.h>
    
    #include <iostream>
    
    int main() {
      int T = 0;
      std::cin >> T;
      for (int i = 0; i < T; i++) {
        int jinWei = 0;  // 这里只可能是0或1其他的情况都是错的
    
        std::string count1;
        std::string count2;
        std::string result;
        std::cin >> count1 >> count2;
    
        std::string result1 = count1;//16 17两行是用来保存输入的字符串不被前导零覆盖,输出使用
        std::string result2 = count2;
        int length1 = std::max(count1.length(), count2.length());
    
        // 22 到 32 部分是用来比较两字符串长度并且变为等长
        int lenMin = count1.length();
        int lenMax = count2.length();
        std::string m = "";
        for (int j = 0; j < std::abs(lenMax - lenMin); j++) {
          m = m + "0";
        }
        if (count1.length() > count2.length()) {
          count2 = m + count2;
        } else if (count1.length() < count2.length()) {
          count1 = m + count1;
        }
    
        for (int j = length1 - 1; j >= 0; j--) {
          int tempstr;
          int temp1 = 0;
          int temp2 = 0;
          temp1 = count1[j] - '0';
          temp2 = count2[j] - '0';
          int sum = temp1 + temp2 + jinWei;
          if (sum >= 10) {
            jinWei = 1;
          } else {
            jinWei = 0;
          }
          int yuShu = (sum) % 10;
          tempstr = yuShu;
          result = std::to_string(tempstr) + result;
        }
    
        std::cout << "Case " << i + 1 << ":" << std::endl;
        std::cout << result1 << " + " << result2 << " = ";
        if (jinWei == 1) {
          std::cout << "1";
        }
        std::cout << result << std::endl;
      }
    }
    
    • 2
      @ 2025-10-6 15:34:30

      c

      #include<stdio.h>
      #include<string.h>
      int main()
      {
          char a[1010],b[1010],s[1100];
          int t,l1,l2,lm,A,B,i;
          scanf("%d",&t);
          for(i=1;i<=t;i++){
              scanf("%s%s",a,b);
              l1=strlen(a);
              l2=strlen(b);
              if(l1>l2){//确定循环次数
                  lm=l1;
              }else{
                  lm=l2;
              }
              int sum=0,jin=0;
              int temp[1100]={0};
              for(int k=0;k<lm;k++){
                  if(k<l1){
                      A=a[l1-1-k]-'0';
                  }else{
                      A=0;
                  }if(k<l2){
                      B=b[l2-1-k]-'0';
                  }else{
                      B=0;
                  }sum=A+B+jin;
                  temp[k]=sum%10;//将相加数倒序存入
                  jin=sum/10;
              }int x=lm;
              if(jin>0){
                  temp[x]=jin;
                  x++;
              }for(int j=0;j<x;j++){
                  s[j]=temp[x-1-j]+'0';//再倒序存入(即为正序)
                  s[x]='\0';//确保字符串正常结束(不然会WA   /(ㄒoㄒ)/~~)
              }printf("Case %d:\n",i);
              printf("%s + %s = %s\n",a,b,s);
          }
          return 0;
      }
      
      • 1
        @ 2025-12-2 18:12:41
        #include <iostream>
        #include <algorithm>
        using namespace std;
        signed main() {
        	int t;
        	cin >> t;
        	int ans = 0;
        	while (t--) {
        		string a, b;
        		cin >> a >> b;
        		int aa[10005] = {0};
        		int bb[10005] = {0};
        		int cc[10005] = {0};
        		int len1 = a.length();
        		int len2 = b.length();
        		int mx = max(len1, len2);
        		for (int i = 0; i < len1; i++) {
        			aa[i] = a[len1 - 1 - i] - '0';
        		}
        		for (int i = 0; i < len2; i++) {
        			bb[i] = b[len2 - 1 - i] - '0';
        		}
        		int ss = 0;
        		for (int i = 0; i < mx; i++) {
        			cc[i] = aa[i] + bb[i] + ss;
        			ss = cc[i] / 10;
        			cc[i] %= 10;
        		}
        		if (ss != 0) {
        			cc[mx] = ss;
        			mx++;
        		}
        		cout << "Case " << ++ans << ":" << '\n';
        		cout << a << " + " << b << " = ";
        		for (int i = mx - 1; i >= 0; i--) {
        			cout << cc[i];
        		}
        		cout << '\n';
        	}
        	return 0;
        }
        
        • 1
          @ 2025-11-13 19:12:20
          #include <stdio.h>
          #include <string.h>
          int main(){
              int n,i=1;
              scanf("%d",&n);
              while(n--){
                  int lena,lenb,jud=0,carry=0,sum=0;
                  char a[1024]={0},b[1024]={0},c[1025]={0};
                  scanf("%s %s",a,b);
                  lena=strlen(a);
                  lena--;
                  lenb=strlen(b);
                  lenb--;
                  if(lena>lenb)jud=1;
                  for(;lena>=0||lenb>=0;){
                      if(jud==1){
                          sum=carry;
                          if(lena>=0) sum+=a[lena]-'0';
                          if(lenb>=0) sum+=b[lenb]-'0';
                          c[lena]=(sum%10)+'0';
                          carry=sum/10;
                      }
                        if(jud==0){
                          sum=carry;
                          if(lena>=0) sum+=a[lena]-'0';
                          if(lenb>=0) sum+=b[lenb]-'0';
                          c[lenb]=(sum%10)+'0';
                          carry=sum/10;
                      }
                      if(lena>=0)lena--;
                      if(lenb>=0)lenb--;
                  }
                  printf("Case %d:\n",i);
                  if(carry>0){
                      printf("%s + %s = 1%s\n",a,b,c);
                  }
                  else printf("%s + %s = %s\n",a,b,c);
                  i++;
              }
          }
          
          • 1
            @ 2025-11-2 9:51:14

            #include<stdio.h> #include<string.h> int main() { int t; scanf("%d",&t); int cnt = 1; while (t--) { int a[1000]={0}; int b[1000]={0}; int sum[1005] = {0}; char a1[1000]; char b1[1000]; scanf("%s %s",a1,b1); printf("Case %d:\n",cnt); printf("%s + %s = ",a1,b1); int len1 = strlen(a1); int len2 = strlen(b1); for( int i = 0;i<len1;i++) { a[len1-i-1] = a1[i]-'0'; } for ( int i =0;i<len2;i++) { b[len2-i-1] = b1[i]-'0'; } int len = len1; if (len2>len) { len = len2; } for ( int i = 0;i<len;i++) { sum[i] = a[i]+b[i]; } for ( int i= 0;i<len;i++) { if (sum[i]>=10) { sum[i+1] = sum[i+1] +1; sum[i]%=10; } } if (sum[len]!= 0) { len++; } for ( int i =len-1;i>=0;i--) { printf("%d",sum[i]); } printf("\n"); cnt++; } return 0; }

            • 1
              @ 2025-8-20 0:52:16
              #include <bits/stdc++.h>
              using namespace std;
              const int N = 1010;
              
              int a[N], b[N];
              int al, bl;
              
              void add(int a[], int &al, int b[], int &bl) {
              int t = 0;
              al = max(al, bl);
              for (int i = 0; i < al; i++) {
              t += a[i] + b[i];
              a[i] = t % 10;
              t /= 10;
              }
              if (t) a[al++] = 1;
              if(al > 1 && a[al - 1] == 0) al--;
              }//到底是改用while还是if有待考究
              
              int main() {
              int T;
              cin >> T;
              for (int k = 1; k <= T; k++) {
              memset(a, 0, sizeof(a));
              memset(b, 0, sizeof(b));
              al = 0;
              bl = 0;
              
              string x, y;
              cin >> x >> y;
              
              for (int i = x.size() - 1; i >= 0; i--) a[al++] = x[i] - '0';
              for (int i = y.size() - 1; i >= 0; i--) b[bl++] = y[i] - '0';
              
              // 执行加法
              add(a, al, b, bl);
              cout << "Case " << k << ":" << endl;
              cout << x << " + " << y << " = ";
              for (int i = al - 1; i >= 0; i--) cout << a[i];
              cout << endl;
              if (k < T) cout << endl;
              
              }
              return 0;
              }
              

              有大佬用一个数组就可以存储,用空间换时间,但是我比较笨,不会简便。

              • 0
                @ 2025-12-1 19:39:54
                #include<iostream>
                #include<string>
                using namespace std;
                string add(string a,string b){
                	string result = "";
                	int i=a.length()-1;
                	int j = b.length()-1;
                	int carry=0;
                	while(i>=0||j>=0||carry>0){
                		int sum=carry;
                		if(i>=0){
                			sum+=a[i]-'0';
                			i--;
                		}
                		if(j>=0){
                			sum+=b[j]-'0';
                			j--;
                		}
                		carry=sum/10;
                		result=char(sum%10+'0')+result;
                	}
                	return result;	
                }
                int main()
                {
                	int t;
                	cin>>t;
                	for(int c=1;c<=t;c++){
                		string a,b;
                		cin>>a>>b;
                		string sum=add(a,b);
                		cout<<"Case"<<" "<<c<<":"<<endl;
                		cout<<a<<" "<<"+"<<" "<<b<<" "<<"="<<" "<<sum<<endl;
                	}
                	return 0;
                 } 
                
                • @ 2025-12-1 19:40:47

                  记得打空格😕

              • 0
                @ 2025-9-13 23:35:20
                from math import sqrt,ceil,gcd,log;re=lambda:map(int,input().strip().split())
                t, = re()
                for i in range(t):
                    a, b = re()
                    print(f"Case {i + 1}:")
                    print(f"{a} + {b} = {a + b}")
                
                • 0
                  @ 2025-5-2 9:05:11

                  感谢Python的无限大int😄

                  for i in range(int(input())):
                      print("Case %d:"%(i+1))
                      a,b=input().split()
                      print(a+" + "+b+" = ",end='')
                      print(int(a)+int(b))
                  
                  • 0
                    @ 2024-11-27 20:40:47
                    可能有点写的不太好,有错误可指出
                    #include<stdio.h>
                    #include<string.h>
                    
                    void jinwei(char *num1,char *num2,char*sum)
                    {
                    	//为了方便进行进位加法,先将字符串进行反转
                    
                    	//反转之前先计算长度
                    	int len1 = strlen(num1);
                    	int len2 = strlen(num2);
                    
                    	//对数组num1进行反转
                    	for(int i=0;i<len1/2;i++)
                    	{
                    		char temp = num1[i];
                    		num1[i] = num1[len1-1-i];
                    		num1[len1-i-1] = temp;
                    	}
                    	//对数组num2进行反转
                    
                    	for(int i=0;i<len2/2;i++)
                    	{
                    		char temp = num2[i];
                    		num2[i] = num2[len2-i-1];
                    		num2[len2-i-1] = temp;
                    	}
                    
                    	//下面进行加法进位
                    
                    	//加法之前先算出和的数的大概位数
                    	int maxlen = (len1 > len2)?len1:len2;
                    	int yushu = 0;
                    	for(int i=0;i<maxlen;i++)
                    	{
                    		int dight1 = (i<len1)?num1[i] - '0':0;
                    		int dight2 = (i<len2)?num2[i] - '0':0;
                    
                    		int su = dight1 + dight2 + yushu;
                    		sum[i] = (su%10) + '0';
                    		yushu = su/10;
                    	}
                    	if(yushu>0)
                    	{
                    		sum[maxlen] = yushu + '0';
                    		maxlen++;
                    	}
                    
                    	//把结果字符串进行反转
                    	for(int i=0;i<maxlen/2;i++)
                    	{
                    		char temp = sum[i];
                    		sum[i] = sum[maxlen-i-1];
                    		sum[maxlen-i-1] = temp;
                    	}
                    		sum[maxlen] = '\0';
                    }
                    int main()
                    {
                    	int n;
                    	scanf("%d",&n);
                    	int x = 1;
                    	while(n--)
                    	{
                    		char a[1000] = {0};
                    		char b[1000] = {0};
                    		char result[1000] = {0};
                    		scanf("%s",a);
                    		scanf("%s",b);
                    		char c[1000] = {0};
                    		char d[1000] = {0};
                    		int len1 = strlen(a);
                    		int len2 = strlen(b);
                    		strcpy(c,a);
                    		strcpy(d,b);
                    		c[len1] = '\0';
                    		d[len2] = '\0';
                    		jinwei(a,b,result);
                    		printf("Case %d:\n",x);
                    		x++;
                    		printf("%s + %s = %s\n",c,d,result);
                    	}
                    }
                    
                    • 1

                    信息

                    ID
                    165
                    时间
                    3000ms
                    内存
                    128MiB
                    难度
                    8
                    标签
                    (无)
                    递交数
                    1628
                    已通过
                    210
                    上传者