[英]Problems with cout and a specific string length arithmatic operation c++
這是我第一次嘗試LCS。 我的問題是最后一部分。 當兩個輸入字符串分別是“ mango”和“ man”時,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("%d\n", maxseq-x.length());
cout << z << endl;
return 0;
}
cout
maxseq-x.length()
為無符號值。 (此表達式包含有符號和無符號值,因此結果為無符號 )
printf
%d
maxseq-x.length()
為有符號整數
cout << z
z
作為有符號整數處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.