I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum o
2017-06-28 10:13
633 查看
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string add(string str1, string str2);
int n, i, j;
cin>>n;
string str1, str2, tstr1, tstr2;
for(i = 1; i <= n; i++)
{
cout<<"Case "<<i<<":\n";
cin>>str1>>str2;
tstr1 = str1;
tstr2 = str2;
int len1 = str1.length();
int len2 = str2.length();
if(len1 > len2)
{
for(j = 0; j < len1 - len2; j++)
{
str2 = "0" + str2;
}
}
else if(len1 < len2)
{
for(j = 0; j < len2 - len1; j++)
{
str1 = "0" + str1;
}
}
cout<<tstr1<<" + "<<tstr2<<" = "<<add(str1, str2)<<endl;
if(i != n) cout<<endl;
}
return 0;
}
string add(string str1, string str2)//字符串做算术加法的函数
{
bool flag = false;
string result = "";
int i;
for(i = str1.length() - 1; i >= 0; i--)
{
int a = str1[i] - '0';
int b = str2[i] - '0';
int sum = 0;
if(flag) sum = 1;
sum += (a + b);
if(sum / 10 > 0) flag = true;
else flag = false;
result = char(sum % 10 + 48) + result;
}
if(flag)
result = '1'+result;//如果有进位就在前面加上1
return result;
}
#include<string>
using namespace std;
int main(void)
{
string add(string str1, string str2);
int n, i, j;
cin>>n;
string str1, str2, tstr1, tstr2;
for(i = 1; i <= n; i++)
{
cout<<"Case "<<i<<":\n";
cin>>str1>>str2;
tstr1 = str1;
tstr2 = str2;
int len1 = str1.length();
int len2 = str2.length();
if(len1 > len2)
{
for(j = 0; j < len1 - len2; j++)
{
str2 = "0" + str2;
}
}
else if(len1 < len2)
{
for(j = 0; j < len2 - len1; j++)
{
str1 = "0" + str1;
}
}
cout<<tstr1<<" + "<<tstr2<<" = "<<add(str1, str2)<<endl;
if(i != n) cout<<endl;
}
return 0;
}
string add(string str1, string str2)//字符串做算术加法的函数
{
bool flag = false;
string result = "";
int i;
for(i = str1.length() - 1; i >= 0; i--)
{
int a = str1[i] - '0';
int b = str2[i] - '0';
int sum = 0;
if(flag) sum = 1;
sum += (a + b);
if(sum / 10 > 0) flag = true;
else flag = false;
result = char(sum % 10 + 48) + result;
}
if(flag)
result = '1'+result;//如果有进位就在前面加上1
return result;
}
相关文章推荐
- The Diesel watches is a name which is for the elegance and style you had been craving to have
- This is the first problem for test. Since all we know the ASCII code, your job is simple: Input numb
- What is the best way to calculate a checksum for a file that is on my machine?
- 44.Your database is functional with a peak load for the last one hour. You want to preserve the perf
- Internet Information Services is not installed. You must have Internet Information Services installed in order to use the SharePoint Products and Technologies Configuration Wizard
- You're given a string of lower-case Latin letters. Your task is to find the length of its longest su
- Unable to start debugging on the web server. You do not have permission to debug the application. The URL for this project is in
- Cannot connect to WMI provider.You do not have permission or the server is unreachable.Note that you can only manager SQL Server 2005 and later version with SQL Server Configuration Manager.Invalid namespace [0x8004100e]
- Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created.
- 故障解决:Failure adding assembly to the cache: Access denied. You might not have administrative credentials to perform this task. Contact your system administrator for assistance.
- 40.You have statistics collected for some selected tables. Your requirement is that the statistics
- You’re given an array containing both positive and negative integers and required to find the sub-a
- 160 You want to use the automatic management of backup and recovery operations features for your dat
- Given the array of digits (0 is also allowed), what is the minimal sum of two integers that are made
- sum is equal to the given integer number N and the second number
- VS代码段扩展Snippet Designer is a Visual Studio plug in which allows you to create and search for snippets inside the IDE
- 安装Visual Studio 2010时提示"The location specified for the help content store is invalid or you do not have access to it".
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
- MySql 执行语句错误 Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
- 插入mysql语句报错:1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near