Skip to content

Commit 212d7a1

Browse files
committed
将字符串加起来
1 parent 159aea6 commit 212d7a1

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

AddString/AddString.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddString", "AddString\AddString.csproj", "{E06B7500-68AB-49DA-B0A4-88C79D988FA9}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{E06B7500-68AB-49DA-B0A4-88C79D988FA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E06B7500-68AB-49DA-B0A4-88C79D988FA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E06B7500-68AB-49DA-B0A4-88C79D988FA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E06B7500-68AB-49DA-B0A4-88C79D988FA9}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

AddString/AddString/AddString.csproj

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{E06B7500-68AB-49DA-B0A4-88C79D988FA9}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>AddString</RootNamespace>
11+
<AssemblyName>AddString</AssemblyName>
12+
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<None Include="App.config" />
51+
</ItemGroup>
52+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
53+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
54+
Other similar extension points exist, see Microsoft.Common.targets.
55+
<Target Name="BeforeBuild">
56+
</Target>
57+
<Target Name="AfterBuild">
58+
</Target>
59+
-->
60+
</Project>

AddString/AddString/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
5+
</startup>
6+
</configuration>

AddString/AddString/Program.cs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
/**
8+
* 问题描述:
9+
* Given two non-negative numbers num1 and num2 represented as string,
10+
* return the sum of num1 and num2.
11+
12+
Note:
13+
14+
The length of both num1 and num2 is < 5100.
15+
Both num1 and num2 contains only digits 0-9.
16+
Both num1 and num2 does not contain any leading zero.
17+
You must not use any built-in BigInteger library or convert the inputs to integer directly.
18+
*/
19+
namespace AddString
20+
{
21+
class Program
22+
{
23+
static void Main(string[] args)
24+
{
25+
string num1 = "1";
26+
string num2 = "9";
27+
long num_1 = Convert.ToInt64(num1);
28+
long num_2 = Convert.ToInt64(num2);
29+
30+
Console.WriteLine("{0} + {1} = {2}", num_1, num_2, num_1 + num_2);
31+
Console.WriteLine("{0} 和 {1} 相加的结果是:{2}", num1, num2, AddString(num1, num2));
32+
Console.Read();
33+
}
34+
35+
static string AddString(string num1, string num2)
36+
{
37+
int length = (num1.Length > num2.Length ? num1.Length : num2.Length);
38+
char[] table = new char[length + 1];
39+
40+
int i_1 = num1.Length - 1;
41+
int i_2 = num2.Length - 1;
42+
int flag = 0; // 进位
43+
int tmp = 0;
44+
45+
for (int i = length ; i >= 0; i--)
46+
{
47+
if (i_1 < 0 && i_2 < 0) // 只剩进位
48+
{
49+
if (flag == 1) table[0] = (char)(1 + '0');
50+
break;
51+
}
52+
else if (i_1 < 0)
53+
{
54+
tmp = num2[i_2--] - '0' + flag;
55+
}
56+
else if (i_2 < 0)
57+
{
58+
tmp = num1[i_1--] - '0' + flag;
59+
}
60+
else
61+
tmp = ((num1[i_1--] - '0') + (num2[i_2--] - '0')) + flag;
62+
63+
flag = tmp / 10;
64+
table[i] = (char) (tmp % 10 + '0');
65+
}
66+
67+
return table[0] == '\0' ? new string(table,1,length) : new string(table);
68+
}
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// 有关程序集的一般信息由以下
6+
// 控制。更改这些特性值可修改
7+
// 与程序集关联的信息。
8+
[assembly: AssemblyTitle("AddString")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("AddString")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
//将 ComVisible 设置为 false 将使此程序集中的类型
18+
//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
19+
//请将此类型的 ComVisible 特性设置为 true。
20+
[assembly: ComVisible(false)]
21+
22+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23+
[assembly: Guid("e06b7500-68ab-49da-b0a4-88c79d988fa9")]
24+
25+
// 程序集的版本信息由下列四个值组成:
26+
//
27+
// 主版本
28+
// 次版本
29+
// 生成号
30+
// 修订号
31+
//
32+
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
33+
// 方法是按如下所示使用“*”: :
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)