#include<stdio.h> #include<string.h> int main() { void exchange(char a[], char b[], int n); int i, m, n; char a[81], b[81], c[81], x[81], y[81]; gets(a); gets(b); if (strlen(a) > strlen(b)) { m = strlen(a); n = strlen(b); exchange(a, x, m); exchange(b, y, n); } else { n = strlen(a); m = strlen(b); exchange(b, x, m); exchange(a, y, n); }//数组x为长字符串数 ,m为长字符串的长度 int k = 0;//k代表最前面的0的个数 for (i = m - 1; i >= 0; i--) { if (x[i] == '0') k++; else break; } m = m - k; int count = 0,num = 0,num1,num2;//count代表进位 for (i = n; i < m; i++) y[i] = '0'; for (i = 0; i < m; i++) { num1 = x[i] - 48; num2 = y[i] - 48; num = (num1 + num2 + count) % 10; c[i] = num + 48; count = (num1 + num2 + count) / 10; } if (count == 1)//若一直进位,最终最高位是1 printf("1"); for (i = m; i >= 0; i--)//反序输出 { if (c[i] >= '0' && c[i] <= '9') printf("%c", c[i]); } return 0; } void exchange(char a[], char b[], int n) { int i; for (i = 0; i < n; i++) b[i] = a[n - i-1]; }算法思路
时间: 2024-04-03 15:31:45 浏览: 18
这是一道大整数加法的题目,需要将两个字符串表示的整数相加并输出结果。
首先,需要将长度不同的两个字符串用0补齐,使它们的长度相等。然后,从低位开始模拟手工加法的过程,将对应位上的数字相加并加上进位,得到当前位上的数字,并将进位保存下来。最后,需要判断最高位是否进位,如果进位则需要在最前面输出1。最后,将结果反序输出即可。
注:在代码中,exchange函数用于将字符串反转,方便后续处理。
相关问题
只使用#include <stdio.h> #include <string.h>
这两个头文件都是C语言标准库中的头文件,其中<stdio.h>包含了一些输入输出函数的声明,如printf和scanf;<string.h>包含了一些字符串操作函数的声明,如strcpy和strlen。使用这两个头文件可以完成很多基本的C语言程序设计,例如输出、输入、字符串操作等等。下面给出一个简单的例子:
```
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "hello";
char str2[10] = "world";
printf("%s ", str1);
printf("%s\n", str2);
strcat(str1, str2);
printf("%s\n", str1);
printf("The length of str1 is %d.\n", strlen(str1));
return 0;
}
```
这个程序输出了两个字符串"hello"和"world",然后将它们拼接在一起并输出结果"helloworld",最后输出了拼接后的字符串长度11。
include <stdio.h>#include <stdlib.h>
这是一个C语言的标准头文件的引用,其中<stdio.h>包含了C语言的输入输出函数,而<stdlib.h>包含了一些通用的工具函数和内存分配函数。这些头文件在C语言中非常常见,几乎每个C程序都会用到它们。下面是一个简单的例子,展示了如何使用这两个头文件。
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
char str[100];
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered %d.\n", num);
printf("Enter a string: ");
scanf("%s", str);
printf("You entered %s.\n", str);
return 0;
}
```
上面的程序使用了<stdio.h>中的printf和scanf函数,以及<stdlib.h>中的exit函数。输入一个整数和一个字符串,然后将它们输出到控制台上。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)