写个算法,对2个小于1000000000的输入,求结果。 特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5

题目:https://www.nowcoder.com/practice/a5edebf0622045468436c74c3a34240f?tpId=60&tqId=29490&tPage=1&ru=/kaoyan/retest/1001&qru=/ta/tsing-kaoyan/question-ranking

第一种方法,将输入当做数字,注意处理得到每一位数值
#include
#include

using namespace std;
int specifmul(int x,int y){
int sum=0;
while(x){
int a=x%10;
x=x/10;
int z=y;
while(z){
int b=z%10;
z=z/10;
sum=sum+a*b;
}
}
return sum;
}
int main(){
int x,y;
scanf("%d%d",&x,&y);
printf("%d",specifmul(x,y));
return 0;
}

第二种方法:
逐一地取余得到每一位的数值,这种做法过于复杂并且不直观,如果把输入当做字符串来处理,那么就会比较方便且直观,还不容易出错。
#include
#include
#include

using namespace std;

int main(){
string str1,str2;
while(cin>>str1>>str2){
int answer=0;
for(int i=0;i<str1.size();++i){
for(int j=0;j<str2.size();j++){
//将字符转化为数字
answer+=(str1[i]-‘0’)*(str2[j]-‘0’);
}
}
printf("%d\n",answer);
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值