Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
回文向量序列 因此,在C++程序中需要确定最长的顺序回文。下面是一个例子v=3,4,1,5,2,5,1,8,9,6,向我展示5,并记住在这种情况下第一个1和最后一个1的位置 #include<iostream> using namespace std; int main(){ int v[100],n,i,j,max=0,maxi=0,maxj=0,cni,cnj,l; cin>>n; for(i=0;i<n;i++) cin>>v[i]; cnj=n-1; cni=0; while(j!=0) { for(i=0;i<j;i++) { if(v[i]==v[j]) { l=j-i; if(max<j-i) { max=j-i; maxi=i; maxj=j; } j--; } else j=cnj; } j--; cnj=j; } while(i!=n-1) { for(j=n;j>i;j--) { if(v[i]==v[j]) { if(max<j-i) { max=j-i; maxi=i; maxj=j; } i++; } else i=cni; } i++; cni=i; } cout<<maxi<<" "<<maxj; return 0; }_C++ - Fatal编程技术网

回文向量序列 因此,在C++程序中需要确定最长的顺序回文。下面是一个例子v=3,4,1,5,2,5,1,8,9,6,向我展示5,并记住在这种情况下第一个1和最后一个1的位置 #include<iostream> using namespace std; int main(){ int v[100],n,i,j,max=0,maxi=0,maxj=0,cni,cnj,l; cin>>n; for(i=0;i<n;i++) cin>>v[i]; cnj=n-1; cni=0; while(j!=0) { for(i=0;i<j;i++) { if(v[i]==v[j]) { l=j-i; if(max<j-i) { max=j-i; maxi=i; maxj=j; } j--; } else j=cnj; } j--; cnj=j; } while(i!=n-1) { for(j=n;j>i;j--) { if(v[i]==v[j]) { if(max<j-i) { max=j-i; maxi=i; maxj=j; } i++; } else i=cni; } i++; cni=i; } cout<<maxi<<" "<<maxj; return 0; }

回文向量序列 因此,在C++程序中需要确定最长的顺序回文。下面是一个例子v=3,4,1,5,2,5,1,8,9,6,向我展示5,并记住在这种情况下第一个1和最后一个1的位置 #include<iostream> using namespace std; int main(){ int v[100],n,i,j,max=0,maxi=0,maxj=0,cni,cnj,l; cin>>n; for(i=0;i<n;i++) cin>>v[i]; cnj=n-1; cni=0; while(j!=0) { for(i=0;i<j;i++) { if(v[i]==v[j]) { l=j-i; if(max<j-i) { max=j-i; maxi=i; maxj=j; } j--; } else j=cnj; } j--; cnj=j; } while(i!=n-1) { for(j=n;j>i;j--) { if(v[i]==v[j]) { if(max<j-i) { max=j-i; maxi=i; maxj=j; } i++; } else i=cni; } i++; cni=i; } cout<<maxi<<" "<<maxj; return 0; },c++,C++,当我运行代码块时,它停止工作您可以这样做: 如果只有且两个值相等,则可能出现回文 如果我们发现开始和结束还不是回文,但是我们排除了这个间隔之外的所有值,继续检查回文间隔值2乘2,如果两个值不相等,那么它不是回文,所以我们中断并返回,再次搜索另外两个相等的值,开始和结束回文 此代码将检查另一个回文中的所有回文和子回文回文。但是,如果您只需要一个回文,请取消下面的注释中断: #include <iostream> int main() {

当我运行代码块时,它停止工作

您可以这样做:

如果只有且两个值相等,则可能出现回文

如果我们发现开始和结束还不是回文,但是我们排除了这个间隔之外的所有值,继续检查回文间隔值2乘2,如果两个值不相等,那么它不是回文,所以我们中断并返回,再次搜索另外两个相等的值,开始和结束回文

此代码将检查另一个回文中的所有回文和子回文回文。但是,如果您只需要一个回文,请取消下面的注释中断:

    #include <iostream>

    int main()
    {   

        int begIndex = -1, endIndex = -1; // index of begin and end of palindrome if found then they will be set to not -1
        int size; // size of array
        bool IsPalindrome = false; // initialize to flase

        int array[] = {3, 4, 1, 5, 2, 5, 1, 8, 9, 6, 9, 8};
        size = sizeof(array) / sizeof(array[0]); // getting number of elements of array

        for(int i(0); i < size; i++)
        {// for 1
            for(int j(size - 1); j > i; j--) // j begins from the end -1 and stop if it is equal or less than i to ensure not reading out of palindrome interval
            {// for 2
                if(array[i] == array[j]) // comparing if ok set begin and end indexes of palindrome
                {
                    IsPalindrome = true; // if ok then go to the next test
                    begIndex = i; // only save begin and end indexes of palindrome
                    endIndex = j;

                    // checking two by two and any not equal two values will make it non-palindrome and breaks the inner loop and go back again to check
                    for(int k(begIndex), l(endIndex); k < endIndex; k++, l--)
                    {
                        if(array[k] != array[l]) // last check proves it a palindrome so if two elements are not equal here then it's not palindrome break and go back checking again
                        {
                            IsPalindrome = false;
                            begIndex = endIndex = -1; // reset for checking again
                            break;
                        }
                    }
                }
                // note the check is inside second loop to continue checking if next test proves it's not a palindrome
            }// ~for 2

           // now if the last check doesn't prove that it is not a palindrome we print our palindrome:
            if(IsPalindrome)
            {
                for(int m(begIndex); m <= endIndex; m++)
                {
                    std::cout << array[m] << ", ";
                }
                IsPalindrome = false;
                begIndex = endIndex = -1;
               // break; // if you want one palindrome then uncomment break otherwise you'll get all palindomes from biggest to smallest 
                std::cout << std::endl;
            }

        }// ~for 1

        std::cout << std::endl;

        return 0;
    }

我在想这样的事情。代码在问题中。@Michael,它是如何不具体工作的?@πάνταῥεῖ 它停止工作了。我上传了所有的代码。@Michael解决这些问题的正确工具是调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,您应该[编辑]您的问题,以包括一个重现您的问题的示例,以及您在调试器中所做的观察。直接开始:…\main.cpp18:错误C4700:使用了未初始化的局部变量“j”