大数相乘

来源:互联网 发布:mac隐藏windows分区 编辑:程序博客网 时间:2024/05/20 22:30


红色笔是代表下标


/*大数相乘问题*/#include<iostream>#include<string>using namespace std;void LargeNumbersOperate(string &, string &);int main(){string str1, str2;while(cin >> str1 >> str2)LargeNumbersOperate(str1, str2);return 0;}void LargeNumbersOperate(string & lOperand, string & rOperand){int x = 0;int i, j;string result(lOperand.size() + rOperand.size(), '0');for ( i = lOperand.size() - 1; i >= 0; i--){for ( j = rOperand.size() - 1; j >= 0; j--){//把上次进的数x加上x = x + (lOperand[i] - '0')*(rOperand[j] - '0');//int p=result[i + j + 1] - '0' + x % 10;x = x / 10;//判断是不是大于10if (p >= 10){result[i + j + 1] = p - 10 + '0';x = x + 1;}else{result[i + j + 1] = p + '0';}}//注意这个位置为什么加1呢?如果没有1,运行就会报错,越界的错,那是因为//for ( j = rOperand.size() - 1; j >= 0; j--)//一个回合后,j已经是-1,所以加1是把j变成0result[i+j+1] = x + '0';x = 0;//一定要是0哦,因为一个i的循环已经结束}//输出结果    i = (result[0] == '0') ? 1 : 0;for (i; i < result.size(); i++)cout << result[i];cout << endl;}


1 0
原创粉丝点击