简体   繁体   English

c语言中的回文序列

[英]Palindrome sequence in c language

I need help to write a code in c. I need to find palindrome in a given length of an int in a given sequence.我需要帮助在 c 中编写代码。我需要在给定序列中的给定长度的 int 中找到回文。 Like this: The length is 3 And the sequence is 3846401 Here it is 464 Or: Length 4 And sequence: 3400003 Here is 0000像这样:长度是 3 序列是 3846401 这里是 464 或者:长度是 4 序列是:3400003 这里是 0000

I really need some advice here I have tried to do this for hours and could not make it.我真的需要一些建议,我已经尝试了好几个小时,但没能成功。

after edit: yes i understand all the downvotes, I looked for some advice in general.编辑后:是的,我理解所有的反对意见,我总体上寻求一些建议。 but this is my code, my problem is that i found away to arrange the input backward and not as the original and that in causing a problem, if the input of k is 2345 than my array is 5432.但这是我的代码,我的问题是我发现将输入向后排列而不是原始排列,如果 k 的输入是 2345 而我的数组是 5432,这会导致问题。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int s;
int k;
int module;
int last;
int y;
int t=1;
int counter=0;
#define N 10
   
int main() {

    printf("Please enter a length:\n");
    scanf("%d",&s);
    printf("Please enter a sequence:\n");
    scanf("%d",&k);
    int x =k;

    while(x>0) {
        x = x/10;
        y++;
    }

    int sequence[N];
    int last_array[N];
    for(int i=0; i<1;i++){
        last_array[i]=-1;
    }

    for(int i=0; i < y; i++){
        module= k%10;
        sequence[i] = module;
        k=k/10;
    }


    for(int i=0; i < y; i++){
        if( counter<s&&sequence[i]==sequence[i+s-t]){
                last_array[counter]=sequence[i];

                t=2+t;
                counter++;
        }

    }


    if(last_array[0]==-1){
        printf("There is no such palindrome.\n");
    }
    else{
        printf("The requested palindrome is: ");
        for(int i=0; i<s; i++){
            printf("%d",last_array[i]);
        }
    }

    return 0;
}

I get the point of the downvoters, but it seems you are pretty lost.我明白了反对者的观点,但似乎你很迷茫。 Because you haven't posted any code of yours, I won't post any code either, until you do.因为你还没有发布你的任何代码,我也不会发布任何代码,直到你发布。 I will only try to explain how you would approach the task.我只会尝试解释你将如何处理这项任务。

In general there are some ways to determine if a subset of an array is a palindrome.一般来说,有一些方法可以确定数组的子集是否是回文。 One of the most comprehensible ones, in my opinion, is as follows.在我看来,最容易理解的其中之一如下。

Imagine you have an array like this: [3,8,4,6,4,0,1]想象一下你有一个这样的数组: [3,8,4,6,4,0,1]

If you want to find a palindrome of length 3, what you can do is to divide the input array into smaller subarrays of length 3. In the example above you would have 5 possible subarrays with the length of the desired palindrome:如果您想找到长度为 3 的回文,您可以做的是将输入数组划分为长度为 3 的较小子数组。 在上面的示例中,您将有 5 个可能的子数组,其长度为所需的回文:

[3,8,4] [8,4,6] [4,6,4] [6,4,0] [4,0,1] [3,8,4] [8,4,6] [4,6,4] [6,4,0] [4,0,1]

As you can see the 3rd one is a valid palindrome for this example.如您所见,第三个是本示例的有效回文。 Now how do you determine when there is a valid palindrome?现在你如何确定什么时候有一个有效的回文? Basically you iterate through all the possible subarrays, split the current subarray in half and check if all the digits on the left half are the same as the digits on the right.基本上,您遍历所有可能的子数组,将当前子数组分成两半,并检查左半部分的所有数字是否与右半部分的数字相同。 If one digit on the left does not equal the corresponding digit on the right, you know it can't be a palindrome.如果左边的一位数字不等于右边相应的数字,你就知道它不可能是回文。 If, for every digit, this condition (left = right) is true, then it is a palindrome.如果对于每个数字,这个条件(左 = 右)都为真,那么它就是一个回文。

To help you visualise we define the start as "s" and the end as "e":为了帮助您形象化,我们将开头定义为“s”,将结尾定义为“e”:

This is for an odd array, in this case the 3rd subarray of the example.这是一个奇数数组,在本例中是示例的第三个子数组。

1st iteration:第一次迭代:

 s   e
 |   |
 v   v
[4,6,4] start=4, end=4 <- They are both the same

2nd iteration (start and end fall on the same digit if it is uneven):第二次迭代(如果不均匀,开始和结束落在同一个数字上):

  s/e
   |
   v
[4,6,4] start=6, end=6 <- equal again

For an even arbitrary array it would look like this:对于一个任意数组,它看起来像这样:

1st iteration:第一次迭代:

 s         e
 |         |
 v         v
[4,6,1,1,6,4] start=4, end=4 <- equal

2nd iteration:第二次迭代:

   s     e
   |     |
   v     v
[4,6,1,1,6,4] start=6, end=6 <- equal

3rd iteration:第三次迭代:

     s e
     | |
     v v
[4,6,1,1,6,4] start=1, end=1 <- equal and finish

If at some point they are not equal, again, you know it can't be a palindrome.如果在某些时候它们不相等,同样,您知道它不可能是回文。

Programatically you would have two loops.以编程方式,您将有两个循环。 The outer loop iterates through all the possible subarrays and the second loop compares the elements.外循环遍历所有可能的子数组,第二个循环比较元素。 If the second loop at some point determines that s and e are not the same, you know it isn't a palindrome and can continue with the next subarray.如果第二个循环在某个时刻确定 s 和 e 不相同,则您知道它不是回文并且可以继续下一个子数组。 If the s and e in the second loop are always the same, you know for sure, that this subarray is a palindrome.如果第二个循环中的 s 和 e 始终相同,那么您肯定知道这个子数组是回文。 How to continue further after you find a palindrome, is up to the task.找到回文后如何继续,取决于任务。 Either you just return the first palindrome subarray you find or you loop (in the outer loop) through all subarrays to determine if there are any others.要么只返回找到的第一个回文子数组,要么在所有子数组中循环(在外循环中)以确定是否还有其他子数组。

I hope it is a bit clearer now.我希望现在更清楚了。 It would be easier if you could clarify on where you are actually stuck, because no one can know what kind of programming experience you have.如果你能澄清你实际上卡在哪里会更容易,因为没有人知道你有什么样的编程经验。

u can check if there's a palindrome subset in the set, by the following code in python :您可以通过python 中的以下代码检查集合中是否存在回文子集:

nums = list(map(int, input().split()))

flag = False
for j in range(len(nums)):
    for k in range(j + 2, len(nums)):
        if nums[j] == nums[k]:
            flag = True
            break

if flag == True:
    print("YES")
else:
    print("NO")

Explanation: if the first and last items in the list are the same then it'll be a palindrome.解释:如果列表中的第一项和最后一项相同,那么它将是一个回文。

  • For example: if we have a list like that [1,2,2,1]例如:如果我们有一个这样的列表 [1,2,2,1]
  • j = 0 j = 0
  • k = 0+2, to skip the second number between them k = 0+2,跳过它们之间的第二个数字
  • if nums[j]==nums[k] which first and the last item in list , then it'll be palindrome.如果 nums[j]==nums[k]是列表中的第一个和最后一个项目,那么它将是回文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM