显示在这个方案相邻对的总数

编程入门 行业动态 更新时间:2024-10-27 23:28:19
本文介绍了显示在这个方案相邻对的总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给定的输入的char [] 我想找到字符串中的离散对数。因此对于一个输入:

  • DDDD输出2双
  • DDD输出为1对
  • DD输出为1对
  • D输出0对

每对中的字符必须是相邻的。对于ABCA输出仍然是0,因为'A'是不相邻。*

目的是找到字符串中对的总数。所以对于aaxbb输出应为2. *

有关我的输入字符串一个烧焦[] =dppccddd有3个对相邻的字母,但的我的程序的输出为4 。我该如何解决这个问题?

INT I = 0,计数= 0,COUNT1 = 0;对于(i = 0; I< = 6;我++){    如果(A [I] ==一个第[i + 1])    算上++;}的printf(%D,计数);

解决方案

只是为了让你的code稍微好一点,而不是硬编码的6值,可以使用为(i = 0; I<的sizeof(A)/ sizeof的(A [0]) - 1,我++)要获得一个数组的一些元素。

与code的问题是,如果两个字符相匹配,它将再次开始从第二个比较,你需要跳过这个角色,所以增加 I 1

如果(A [I] ==一个[我+ 1]){    ++计数;    ++我;}

Given an input char[] I would like to find the number of discrete pairs in the string. So for an input of:

  • "dddd" the output is 2 pairs
  • "ddd" the output is 1 pair
  • "dd" the output is 1 pair
  • "d" the output is 0 pairs

The characters in each pair must be adjacent. For an input of "abca" the output is still 0 because the 'a's are not adjacent.*

The goal is to find the total number of pairs in the string. So for an input of "aaxbb" the output should be 2.*

For my input string of char a[] = "dppccddd" there are 3 pairs of adjacent letters but my program's output is 4. How do I solve the problem?

int i = 0, count = 0, count1 = 0; for (i = 0; i <= 6; i++) { if (a[i] == a[i + 1]) count++; } printf("%d", count);

解决方案

Just to make your code slightly better, instead of hardcoding value of 6, use for(i = 0; i < sizeof(a) / sizeof(a[0]) - 1; i++) To get a number of elements in an array.

The problem with your code is that if two chars are matched, it will start comparing from the second one again, you need to skip that character, so increase i by 1:

if(a[i] == a[i + 1]) { ++count; ++i; }

更多推荐

显示在这个方案相邻对的总数

本文发布于:2023-11-29 22:15:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647785.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在这个   总数   方案

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!