如何在以下程序中获取字符数组的用户输入? (C ++) [英] How to get user input for character array in the following program? (C++)
本文介绍了如何在以下程序中获取字符数组的用户输入? (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在main函数中,如何获取char X和char Y的用户输入数组?即。而不是将值赋值为char作为Char X [] =AGGTAB或char Y [] =GXTXAYB,我如何将其作为输入?
例如:
输入:
AGGTAB
GXTXAYB
输出:
长度为4
我尝试过:
/ * Naive递归实现LCS问题* /
#include < ; bits / stdc ++。h >
int max( int a, int b);
int lcs( char * X, char * Y, int m, int n)
{
if (m == 0 || n == 0 )
return 0 ;
if (X [m- 1 ] == Y [n- 1 ])
return 1 + lcs( X,Y,m- 1 ,n- 1 );
else
return max(lcs(X,Y,m,n-) 1 ),lcs(X,Y,m- 1 ,n));
}
int max( int a , int b)
{
return (a> b)? a:b;
}
int main()
{
char X [] = AGGTAB;
char Y [] = GXTXAYB 跨度>;
int m = strlen(X);
int n = strlen(Y);
printf( LCS的长度为%dn,lcs(X ,Y,m,n));
return 0 ;
}
解决方案
嘿伙计,它是C ++
!< br $> b $ b
#include < span class =code-keyword>< iostream >
#include < 字符串 >
int lcs( const std :: string& X, const std :: string& Y, size_t m, size_t n)
{
if (m == 0 || n == 0 )
ret urn 0 ;
if (X [m- 1 ] == Y [n- 1 ])
return 1 + lcs( X,Y,m- 1 ,n- 1 );
else
return std :: max(lcs(X,Y,m) ,n- 1 ),lcs(X,Y,m- 1 ,n));
}
int main()
{
std :: string X,Y;
getline(std :: cin,X);
getline(std :: cin,Y);
std :: cout<< LCS的长度为<< lcs(X,Y,X.size(),Y.size())<<的std :: ENDL;
}
使用获取,_getws [ ^ ]
In the main function, how do i get user input array for char X and char Y? ie. instead of assigning value to char as Char X[ ]= "AGGTAB" or char Y[ ]= "GXTXAYB", how can i give it as an input?
eg:
INPUT:
AGGTAB
GXTXAYB
OUTPUT:
The length is 4
What I have tried:
/* A Naive recursive implementation of LCS problem */
#include<bits/stdc++.h>
int max(int a, int b);
int lcs( char *X, char *Y, int m, int n )
{
if (m == 0 || n == 0)
return 0;
if (X[m-1] == Y[n-1])
return 1 + lcs(X, Y, m-1, n-1);
else
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
}
int max(int a, int b)
{
return (a > b)? a : b;
}
int main()
{
char X[] = "AGGTAB";
char Y[] = "GXTXAYB";
int m = strlen(X);
int n = strlen(Y);
printf("Length of LCS is %dn", lcs( X, Y, m, n ) );
return 0;
}
解决方案
Hey man, it isC++
!
#include <iostream> #include <string> int lcs( const std::string & X, const std::string & Y, size_t m, size_t n ) { if (m == 0 || n == 0) return 0; if (X[m-1] == Y[n-1]) return 1 + lcs(X, Y, m-1, n-1); else return std::max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n)); } int main() { std::string X,Y; getline(std::cin, X); getline(std::cin, Y); std::cout << "Length of LCS is " << lcs(X,Y, X.size(), Y.size()) << std::endl; }
Use gets, _getws[^].
这篇关于如何在以下程序中获取字符数组的用户输入? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文