COUT和特定的字符串长度算术操作C 的问题

Problems with cout and a specific string length arithmatic operation c++

本文关键字:操作 问题 字符串 COUT      更新时间:2023-10-16

这是我在LCS上的第一次尝试。我的问题是最后一部分。当两个输入字符串是"芒果"answers"人"时,看来COUT不断弄乱'MaxSeq-x.length(('部分。但是,在事先将结果存储在变量中或仅使用printf((中时,似乎可以。我想念什么吗?

#include<bits/stdc++.h>
using namespace std;
int main(){
    string x, y;
    cin >> x >> y;
    int lcs[100][100] = {{0}};
    for(int i = 0; i<y.length(); i++){
        for(int j = 0; j<x.length(); j++){
            if(y[i] == x[j]){
                int ans = 0;
            
                if(i && j){
                    ans = max(1+lcs[i-1][j-1], ans);
                }
                else{
                    ans = max(1, ans);
                }
                lcs[i][j] = ans;
            }
            else{
                int ans = 0;
                if(i){
                    ans = max(lcs[i-1][j], ans);
                }
                if(j){
                    ans = max(lcs[i][j-1], ans);
                }
                lcs[i][j] = ans;
            }   
        }
    }
    int maxseq = lcs[y.length()-1][x.length()-1];
    int z = maxseq-x.length();
    cout << maxseq-x.length() << endl;
    printf("%dn", maxseq-x.length());
    cout << z << endl; 
    return 0;
}

coutmaxseq-x.length()处理为 unsigned 值。(此表达式同时包含签名 unsigned 值,因此结果是 nosigned (

%dprintf中的 maxseq-x.length()中的签名 integer

cout << z处理z AS 签名 Integer。