- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在做的作业是:找出字符串中最大的回文。回文是一个向后读取与向前读取相同的序列。赛车、前夕、皮划艇就是一些例子。我的问题是我的字符串没有打印出整个输出。我在这方面还很陌生,所以我知道的不多,但我认为打印有问题。如果有人能帮助我,我将非常高兴。
#include <stdio.h>
#include <string.h>
int palindromelength(char *str, int i, int j);
char str[100];
int main()
{
int i,j,len,n;
printf("Enter a string ");
fgets(str,sizeof(str),stdin);//takes user input
str[strcspn(str, "\n")] = 0;
len=strlen(str);
palindromelength(str, 0, len-1);//function call
return 0;
}
int palindromelength(char *str, int i, int j)//compare
{
int len=strlen(str);
i=0,j=len-1;//i starts from first letter and j starts from the last letter
while(i<=j && j!=0)//edit:&& j!=0 because i=0->str[0] and j=0->str[0] is the same first letter
{
if(str[i]==str[j])
{
printf("%c%c\n",str[i],str[j]);//edit:added str[j] but it just prints a letter twice
printf("if: i=%d j=%d str[i]=%c str[j]=%c\n",i,j,str[i],str[j]);/*edit:new
printf to check the i and j values and the corresponding letters under if*/
i++;//increment i
j--;//decrement j
}
if(str[i]!=str[j])//if letters aren't same
{
printf("if: i=%d j=%d str[i]=%c str[j]=%c\n",i,j,str[i],str[j]);/*edit:new
printf to check the i and j values and the corresponding letters under the other if statement*/
i=0;//i_initial?
j--;//only decrement j
}
}
return 0;
}
output//edit to print the new printf statements
Enter a string abcbade //expected:abcba
length is 7
if: i=0 j=6 str[i]=a str[j]=e
if: i=0 j=5 str[i]=a str[j]=d
aa
if: i=0 j=4 str[i]=a str[j]=a
bb
if: i=1 j=3 str[i]=b str[j]=b
cc
if: i=2 j=2 str[i]=c str[j]=c
output 2:
Enter a string dabae //expected:aba
length is 5
if: i=0 j=4 str[i]=d str[j]=e
if: i=0 j=3 str[i]=d str[j]=a
if: i=0 j=2 str[i]=d str[j]=b
if: i=0 j=1 str[i]=d str[j]=a
output 3:
Enter a string abcbacdcbaab //expected:abcba
length is 12
if: i=0 j=11 str[i]=a str[j]=b
aa
if: i=0 j=10 str[i]=a str[j]=a
if: i=1 j=9 str[i]=b str[j]=a
if: i=0 j=8 str[i]=a str[j]=b
if: i=0 j=7 str[i]=a str[j]=c
if: i=0 j=6 str[i]=a str[j]=d
if: i=0 j=5 str[i]=a str[j]=c
aa
if: i=0 j=4 str[i]=a str[j]=a
bb
if: i=1 j=3 str[i]=b str[j]=b
cc
if: i=2 j=2 str[i]=c str[j]=c
最佳答案
它没有打印完整的回文,因为你已经把你的 printf 放在支票里了。
if(str[i]==str[j])//if letters are same
{
printf("%c",str[i]);//print palindrome letter
/*is there a way I can store all the palindrome
characters in a string and print that string?*/
i++;//increment i
j--;//decrement j
}
如您所见,索引“i”和“j”是回文的一部分,但您只打印“i”。所以只有回文的前半部分会被打印出来。
除此之外,我认为您还需要处理许多其他情况,例如有多个回文,您需要选择最大的一个。例如:abcxyzyxdefabcdedcbaxyz
所以我的建议是,
1) 实现一个返回最大回文的函数 字符串的索引 i(如果存在)。
2) 在循环中调用这个函数 从索引 i=0 到原始字符串的长度 2。
3) 存储 当前最大的回文索引和长度。在每个循环中更新它。
4) 最后打印出最大的回文。
函数 palindromelength() 正在做我在步骤 #1 中提到的任何事情。但是您不应打印字母,而应从中返回最大回文的长度。
在 main() 中,循环调用 palindromelength() 并更新索引,如下所示。
for(i=0; i<(len-1); i++)
{
palinrdomeLen = palindromelength(str, i, len-1);
if(palinrdomeLen > largestPalindromeLen)
{
largestPalindromeLen = palinrdomeLen;
largestPalindromeIdx = i;
}
}
除了回文长度(),
1) 您需要更新函数以返回回文的长度。
2) 当 str[i]!=str[j]
时,回文搜索应该从索引首字母“i”开始。
if(str[i]!=str[j])//if letters aren't same
{
i = i_initial; //i_initial is the "i" value passed to palindromelength()
j--;//only decrement j
}
关于c - 在 C 中,我打印字符串中最大回文的代码没有打印整个回文字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55486920/
“用 Haskell 进行函数式思考”中的练习之一是使用融合定律使程序更加高效。我在尝试复制答案时遇到了一些麻烦。 部分计算要求您将 maximum (xs++ map (x+) xs) 转换为 ma
我正在尝试获得 R 中最大/最小的可表示数字。 输入“.Machine”后 我有: $double.xmin [1] 2.225074e-308 $double.xmax [1] 1.797693e+
有没有办法更改浏览器验证消息 请检查所附图片。 我目前正在使用 wooCommerce 目前它显示小于或等于 X 个数字,我想更改为请求超过 X 个项目的报价。 请多多指教 最佳答案 您需要使用oni
我正在尝试将解决方案从 Excel 求解器复制到 R 中,但不知道从哪里开始。 问题: 每小时选择 5 个选项(5 行),以最大化“分数”的总和,而无需在多个小时内选择同一组 2 次。 换句话说: 最
Haskell 中是否有这样的功能: max_of_type :: (Num a) => a 所以: max_of_type :: Int == 2 ^ 31 - 1 // for example,
我有这两个表示时间范围(秒)的输入字段,我需要这样设置,以便“from/min”字段不能高于“to/max”,反之亦然。 到目前为止我得到了这个: jQuery(document).ready(fun
我有一个看起来像这样的表: http://sqlfiddle.com/#!9/152d2/1/0 CREATE TABLE Table1 ( id int, value decimal(10,
我会尝试尽可能简单地解释它: 首先是一些带有虚拟数据的数据库结构。 结构 tb_spec_fk feature value ----------------- 1 1 1
我有两个表。 表 1: +---------+---------+ | Lead_ID | Deal_ID | +---------+---------+ | 2323 | null |
我的数据库中有一个字段可以包含数字,例如8.00 或范围编号,例如8.00 - 10.00。 如果您将每个数字作为单独的数字,我需要从表中获取 MIN() 和 MAX()。例如当范围为 8.00 -
max(float('nan'), 1) 计算结果为 nan max(1, float('nan')) 计算结果为 1 这是预期的行为吗? 感谢您的回答。 max 在 iterable 为空时引发异常
我想问一下如何在 CSS 中创建一个页脚栏,它具有最小宽度(比如 650 像素),并且会根据窗口大小进行拉伸(stretch),但仅限于某个点(比如 1024 像素)。 我的意思是当窗口大小为例如 1
我尝试调整表格列宽(下一个链接上的“作者”列 http://deploy.jtalks.org/jcommune/branches/1?lang=en)。我已将最小/最大属性添加到 .author-c
在 C# 中,是否有用于将最小值和最大值存储为 double 值的内置类? 此处列出的要点 http://msdn.microsoft.com/en-us/library/system.windows
问题: 每个任务队列是否可以每秒处理超过 500 个任务? 每个 GAE 应用是否可以每秒处理超过 50,000 个任务? 详细信息: Task queue quota文档说: Push Queue
我想知道是否允许最大或最小堆树具有重复值?我试图仅通过在线资源查找与此相关的信息,但一直没有成功。 最佳答案 是的,他们可以。您可以在“算法简介”(Charles E. Leiserson、Cliff
首先,我是 .NET 开发人员,喜欢 C# 中的 LINQ 和扩展方法。 但是当我编写脚本时,我需要相当于 Enumerable extension methods 的东西 任何人都可以给我任何建议/
这是一个检查最大 malloc 大小的简单程序: #include std::size_t maxDataSize = 2097152000; //2000mb void MallocTest(vo
我想找到我的数据的最小值和最大值。 我的数据文件: 1 2 4 5 -3 -13 112 -3 55 42 42 而我的脚本: {min=max=$1} {if ($1max) {max=$1}
我想查询我的Elastic-Search以获取仅具有正值的最低价格价格。我的价格也可以为零和-1;所以我不希望我的最小聚合返回0或-1。我知道我应该向查询(或过滤器)添加脚本,但是我不知道如何。我当前
我是一名优秀的程序员,十分优秀!