写一个你所能写的最长的c++代码
时间: 2023-03-19 18:20:55 浏览: 67
#include <stdio.h>
int main(){
int a = 1;
int b = 2;
int c = a + b;
printf("结果是 %d", c);
return 0;
}
相关问题
用c++写一个最长公共子序列的代码
好的,以下是用C语言写的最长公共子序列代码:
```c
#include <stdio.h>
#include <string.h>
int max(int a, int b) {
return a>b?a:b;
}
int lcs(char* s1, char* s2, int len1, int len2) {
int dp[len1+1][len2+1];
int i, j;
for (i=0; i<=len1; i++) {
for (j=0; j<=len2; j++) {
if (i==0 || j==0) {
dp[i][j] = 0;
}
else if (s1[i-1] == s2[j-1]) {
dp[i][j] = dp[i-1][j-1] + 1;
}
else {
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}
return dp[len1][len2];
}
int main() {
char s1[100], s2[100];
printf("请输入第一个字符串:\n");
scanf("%s", s1);
printf("请输入第二个字符串:\n");
scanf("%s", s2);
int len1 = strlen(s1);
int len2 = strlen(s2);
printf("最长公共子序列的长度为:%d\n", lcs(s1, s2, len1, len2));
return 0;
}
```
这段代码可以计算两个字符串的最长公共子序列的长度。用户只需要输入两个字符串就可以得到答案。感谢您的提问!
用c++语言写一个最长公共子序列的代码
下面是一个简单的最长公共子序列的 C 语言代码:
```
#include <stdio.h>
#include <string.h>
#define max(x,y) ((x)>(y))?(x):(y)
void find_lcs(char *a, char *b, char *c)
{
int la=strlen(a), lb=strlen(b);
int f[la+1][lb+1],i,j;
memset(f,0,sizeof(f));
for(i=1;i<=la;i++)
for(j=1;j<=lb;j++)
if(a[i-1]==b[j-1])
{
f[i][j]=f[i-1][j-1]+1;
c[f[i][j]-1]=a[i-1];
}
else
f[i][j]=max(f[i-1][j],f[i][j-1]);
}
int main()
{
char a[]="abcde", b[]="ace";
char c[256]={0};
find_lcs(a,b,c);
printf("Longest common subsequence is %s\n",c);
return 0;
}
```
注意:以上代码实现了两个字符串的最长公共子序列的查找,并将结果存储在字符数组 c 中。重点是,我们使用了一个二维数组 f 来代替递归方法来寻找最长公共子序列。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)