面试题七 C/C++ 两个字符串由数字组成的相加,最大不超过32bit整形的系统函数--程序员面试题
2017-07-22 11:03
453 查看
面试题:
给定两个由0-9数字组成的最长可到30个字符的字符串,请计算他们对应的整数和。允许使用字符串
转最大不超过32bit整形的系统函数。
当我看到这个面试题的时候,貌似不是第一次,所以就动手写了写。欢迎在下面留言写其他方法。也可以加入QQ群聊:83459374
好了不多说,请看代码:
void calculateAdd()
{
string str1 =
"2142135213214235231323241";
string str2 =
"809327498327413628164321434214";
string result =
"";
cout <<
"str1的长度为:" << str1.length() <<endl;
cout <<
"str2的长度为:" << str2.length() <<endl;
int quotient =
0;
int remainder =
0;
int count =
0;
int strNum1 = str1.length();
int strNum2 = str2.length();
int max = strNum1 > strNum2 ? strNum1: strNum2;
cout <<
"max的长度为:" << max <<endl;
while (max >
0) {
strNum1--;
strNum2--;
max--;
int nStr1 = strNum1 >=
0 ? ((int)(str1.at(strNum1)) -
48):0;
int nStr2 = strNum2 >=
0 ? ((int)(str2.at(strNum2)) -
48):0;
count = nStr1 + nStr2 + quotient;
if (count >
9){
quotient = count /
10;
remainder = count %
10;
}else{
quotient =
0;
remainder = count;
}
result = (char)(remainder+48) + result;
}
if(quotient >
0){
result = (char)(quotient+48) + result;
}
cout <<
"result的长度为:" << result.length() <<endl;
cout<<"----------和为:"<<result.c_str()<<endl;
}
输出结果为:
str1的长度为:25
str2的长度为:30
max的长度为:30
result的长度为:30
----------和为:809329640462626842399552757455
亲测了一下,目测应该是对的,没什么问题,欢迎更多交流!
给定两个由0-9数字组成的最长可到30个字符的字符串,请计算他们对应的整数和。允许使用字符串
转最大不超过32bit整形的系统函数。
当我看到这个面试题的时候,貌似不是第一次,所以就动手写了写。欢迎在下面留言写其他方法。也可以加入QQ群聊:83459374
好了不多说,请看代码:
void calculateAdd()
{
string str1 =
"2142135213214235231323241";
string str2 =
"809327498327413628164321434214";
string result =
"";
cout <<
"str1的长度为:" << str1.length() <<endl;
cout <<
"str2的长度为:" << str2.length() <<endl;
int quotient =
0;
int remainder =
0;
int count =
0;
int strNum1 = str1.length();
int strNum2 = str2.length();
int max = strNum1 > strNum2 ? strNum1: strNum2;
cout <<
"max的长度为:" << max <<endl;
while (max >
0) {
strNum1--;
strNum2--;
max--;
int nStr1 = strNum1 >=
0 ? ((int)(str1.at(strNum1)) -
48):0;
int nStr2 = strNum2 >=
0 ? ((int)(str2.at(strNum2)) -
48):0;
count = nStr1 + nStr2 + quotient;
if (count >
9){
quotient = count /
10;
remainder = count %
10;
}else{
quotient =
0;
remainder = count;
}
result = (char)(remainder+48) + result;
}
if(quotient >
0){
result = (char)(quotient+48) + result;
}
cout <<
"result的长度为:" << result.length() <<endl;
cout<<"----------和为:"<<result.c_str()<<endl;
}
输出结果为:
str1的长度为:25
str2的长度为:30
max的长度为:30
result的长度为:30
----------和为:809329640462626842399552757455
亲测了一下,目测应该是对的,没什么问题,欢迎更多交流!
相关文章推荐
- 面试题--两个字符串数字相加,不能用jdk自带的类型转换方法
- 求两个字符串最大公共字符串(听说是微软几年前面试题)
- [去哪儿面试题] 一组数字,求其组成的最大数
- java两个字符串数字相加
- 高精度计算:最大公约数【两个数字字符串】
- 匹配一个字符串,它由0、1、2组成,并且中间没有两个相邻的数字是相同的(比如,匹配1232101210321020,而不匹配1100123)
- 415. Add Strings--两个用字符串表示的数字相加
- 由数字组成的两个字符串求和
- C++中判断字符串是否全部由数字组成
- LeetCode425——Add Strings(两个字符串中的数字相加(十进制或二进制),输出字符串形式的结果)
- 将由16进制数字组成的字符串转换成与之等价的整形值
- (程序员面试题)字符串处理之查找两个字符串的最大公共子字符串
- 两个数字字符串相加生成新的字符串
- 面试题01 由四个数字组成的一个最大数和最小数,最大数减去最小数正好是这四个数字组成的另外一个数n,求n的值
- 415. Add Strings (计算由两个字符串表示的数字相加之和)
- 字符串中数字串相加;字符串中最大数字串;
- 常见C语言面试题之五:两个字符串的最大公共子字符串
- iOS 算法面试题(打印2-100之间的素数、求两个整数的最大公约数、查找字符串的位置)~demo
- 将由16进制数字组成的字符串转换成与之等价的整形值
- leetcode_415(两个数字字符串相加,模拟大数相加)