结果不对求调,求助大佬们
  • 板块学术版
  • 楼主魑魅魍魉唬
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/3/6 18:40
  • 上次更新2024/3/6 20:50:22
查看原帖
结果不对求调,求助大佬们
317218
魑魅魍魉唬楼主2024/3/6 18:40

输入字符串提取其中的数字,然后相加输出 dfd56hj5hjkk9kk7,应该在屏幕上显示: 56+5+9+7=77.但是我的结果不对,输出是56+5+9+7=4234390,我不知道哪里有问题,请大佬们帮忙看看,感谢感谢

#include<bits/stdc++.h>
using namespace std;
int main(){
   string s;
   cin>>s;
   int l=s.size();
   int  a[100]={},co=0,sum;
   for(int i=0;i<l;i++){
   	if(isdigit(s[i])&&isdigit(s[i+1])){
   		a[co++]=(s[i]-'0')*10+(s[i+1]-'0');
   		i++;
   	}
   	else if(isdigit(s[i])&&not isdigit(s[i+1])){
   		a[co++]=s[i]-'0';
   	}
   	else{
   		continue;
   	}
   }
   for(int i=0;i<co;i++){
   	sum=sum+a[i];
   	if(i!=co-1){
   	cout<<a[i]<<"+";}
   	else{
   		cout<<a[i];
   	}
   }
   cout<<"="<<sum<<endl;
   return 0;
}
2024/3/6 18:40
加载中...