| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3179 人关注过本帖
标题:求大神出手 连续数字算法 也可以说为顺子算法
只看楼主 加入收藏
softshl1
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2014-5-19
结帖率:0
收藏
已结贴  问题点数:20 回复次数:10 
求大神出手 连续数字算法 也可以说为顺子算法
1 2 3 4 5 6 7 8 9 10 11 12 13
任意取五个或者留个或者七个数字 如果取的是五个数字则可以 从剩余数字里面取出指定的数字2个 如果是取留个数字 则可以从余下的数字里面取出1个 如果取的为7个数字则不能继续取数字
利用取出的数字判断是否可组成连续的五个数字。
例如 1 3 5 8 9 需要继续取出 2和 4组成连续的五个数字 1 2 3 4 5
1 4 5 6 7 13 需要继续取出 4或者7 任意一个
1 3 7 8 12 无论取一个还是两个都不能组成连续的数字
dim a() as integer

Function 连续数字判定(dim a As Variant) as string

连续数字判定=可继续获取数字X,y组成连续数字
End Function
2014-05-19 18:49
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:10 
VB 忘记了,用伪代码描述一下

bool sa[13] = {false};    'VB有bool变量么?
int  n = rand() % 2 + 5;

'随机取出 n 个数,置 sa[n] 为 1
for i = 1 to n
  tmp = rand() % 13
  if (sa[tmp])  i -= 1, continue;
  print i+1
  sa[i] = 1

'使 min max 分别表示最小值和最大值
for min = 0 to 13-1
  if sa[min]  break;
for max = 13-1 to 0 step -1
  if sa[max]  break;

if (max - min <= 7)  TRUE;
else                 FALSE;

'实在忘记怎么写了,代码写的四不像。。。


[fly]存在即是合理[/fly]
2014-05-19 19:06
softshl1
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2014-5-19
收藏
得分:0 
回复 2 楼 azzbcc
思路大概也知道 但逻辑比较复杂 死活写不出来。
先排序 然后根据比较差值 还要判断这个范围内已有的数据
1 3 5 7 12
3 5 7
继续取 4 6 或者 2 4就可以 组成顺子
至少三个数字才行 ABS(1-12)>4  1和12不能同时存在
同理 1 7 不能同时存在
1 5 有可能同时存在 1 和5 加中间数字 总共三个 1 3 5继续取两个就可以组成顺子
但编写起来很麻烦
2014-05-19 21:01
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:0 
看错题了。。。

用 C 实现了一下

程序代码:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX  13

void sort(int a[], int n)
{
    for (size_t i = n-1; i > 0; i--)
    for (size_t j = 0; j < i; j++)
    {
        if (a[j] < a[j+1])
        {
            int tmp;
            tmp    = a[j];
            a[j]   = a[j+1];
            a[j+1] = tmp;
        }
    }
}

int main(int argc, char *argv[])
{
    int sa[7] = { 0 };
    bool flag = false;

    srand(time(NULL));
    
    int n = rand() % 2 + 5;  // n 取值 567 表示随机第一次取 n 个数

    // 随机产生 n 个数
    for (size_t j, i = 0; i < n; i++)
    {
        sa[i] = rand() % MAX; 
        for (j = 0; j < i; j++)
        {
            if (sa[i] == sa[j])  break;
        }
        if (j < i)  { i--;  continue; }
        printf("%d ", sa[i]);
    }
    printf("\n");  //换行

    // 将这 n 个数排序
    sort(sa, n);

    // 主要 判断 过程
    // 以 n = 5 为例,判断 021324 三组数,循环变量为 3
    for (size_t i = 0; i < 3; i++)
    {
        // 如果 第 i 个数 和 第 i+7-n 个数之差 不大于 n-2 说明可解
        // 仍以 n = 5 为例
        // 如果 sa[0] - sa[2] <= 3 ,说明有解
        if (n - 2 >= sa[i] - sa[i + 7 - n])
            flag = true;
    }
    if (flag)  puts("TRUE");
    return 0;
}


[fly]存在即是合理[/fly]
2014-05-19 22:22
lowxiong
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:27
帖 子:653
专家分:3402
注 册:2008-5-7
收藏
得分:10 
用vb做了个,可能开始的思路就决定了算法,也许直接用数组更简洁,有空再换个算法。
程序代码:
Private Function Straight(ByVal Sour As String) As String
  '判断是否为顺子
  Dim b() As String, i As Integer, j As Integer, a As String, f As Boolean
  Straight = ""
  b = Split(Sour, ",")
  For i = 0 To 5
    '对数据进行冒泡法排序
    For j = i + 1 To 6
      If Val(b(i)) > Val(b(j)) Then a = b(i): b(i) = b(j): b(j) = a
    Next
  Next
  For i = 0 To 2
    f = True
    For j = Val(b(i)) + 1 To Val(b(i)) + 4
      If InStr("," & Sour, "," & j & ",") = 0 Then
        f = False
        Exit For
      End If
    Next
    If f Then
      a = ""
      For j = 0 To 6
        a = a & b(j) & ","
      Next
      Straight = a
      Exit Function
    End If
  Next
End Function
Private Sub PermComb(ByVal Sour As String, Result As String)
  '递归排列组合所有
  Dim b() As String, a As String, i As Integer
  b = Split(Sour, ",")
  If UBound(b) = 7 Then
    a = Straight(Sour)
    If a <> "" And InStr(Result, a) = 0 Then Result = Result & a & "|" '输出有顺子的不重复的结果
    Exit Sub
  End If
  For i = 1 To 13
    If InStr("," & Sour, "," & i & ",") = 0 Then PermComb Sour & i & ",", Result
  Next
End Sub
Private Sub Command1_Click()
  Dim a As String, b() As String, c() As String, d As String, i As Integer, j As Integer, s As Integer
  Randomize
  s = Int(Rnd * 3) + 5    '随机数据个数
  a = ""
  j = Int(Rnd * 13) + 1
  For i = 1 To s
    While InStr(a, j) > 0
      j = Int(Rnd * 13) + 1
    Wend
    a = a & j & ","
  Next
  '上述代码产生随机数据在a里
  'a = "10,2,5,3,9,"     '该数据实验用
  'a = "4,1,10,6,13,"   
  Text1 = "原数据:" & Left(a, Len(a) - 1)
  d = ""
  PermComb a, d
  If d <> "" Then
    Text1 = Text1 & ",可以组成顺子。"
    If s < 7 Then
      b = Split(d, "|")
      For i = 0 To UBound(b) - 1
        c = Split(b(i), ",")
        d = ""
        For j = 0 To 6
          If InStr(a, c(j) & ",") = 0 Then d = d & c(j) & ","
        Next
        Text1 = Text1 & d & "|"
      Next
    End If
  Else
    Text1 = Text1 & ",无法组成顺子。"
  End If
End Sub
图片附件: 游客没有浏览图片的权限,请 登录注册

2014-05-20 08:30
softshl1
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2014-5-19
收藏
得分:0 
感谢两位版主 解决办法很完美
2014-05-20 09:14
softshl1
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2014-5-19
收藏
得分:0 
有点小瑕疵 lowxiong 原数据:6,3,11,12,4,可以组成顺子。5,|5,7,| 这个是出来的结果 应该为 2,5,|5,7,| 这个挺难的


有时候还出现 原数据:6,3,11,12,4,可以组成顺子。 没有可组成数字输出
Private Sub Command1_Click()
  Dim a As String, b() As String, c() As String, d As String, i As Integer, j As Integer, s As Integer
  Randomize
  s = Int(Rnd * 3) + 5    '随机数据个数
  a = ""
  j = Int(Rnd * 13) + 1
  For i = 1 To s
    While InStr(a, j) > 0
      j = Int(Rnd * 13) + 1
    Wend
    a = a & j & ","
  Next
  '上述代码产生随机数据在a里
  'a = "10,2,5,3,9,"     '该数据实验用
  a = "6,3,11,12,4,"
  Text1 = "原数据:" & Left(a, Len(a) - 1)
  d = ""
  PermComb a, d
  If d <> "" Then
    Text1 = Text1 & ",可以组成顺子。"
    If s < 7 Then
      b = Split(d, "|")
      For i = 0 To UBound(b) - 1
        c = Split(b(i), ",")
        d = ""
        For j = 0 To 6
          If InStr(a, c(j) & ",") = 0 Then d = d & c(j) & ","
         
        Next
        Text1.Text = Text1.Text & d & "|"
      Next
    End If
  Else
    Text1 = Text1 & ",无法组成顺子。"
  End If
End Sub

[ 本帖最后由 softshl1 于 2014-5-20 09:50 编辑 ]
2014-05-20 09:39
lowxiong
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:27
帖 子:653
专家分:3402
注 册:2008-5-7
收藏
得分:0 
不要随便叫难嘛。
其实编程真的不难,关键是要沉住气,静下心,全身心投入!
在重复字符判断上有点算法错误,之前意识到,没改到那里,稍作修改后不再出现了。
程序代码:
Private Function Straight(ByVal Sour As String) As String
  '判断是否为顺子
  Dim b() As String, i As Integer, j As Integer, a As String, f As Boolean
  Straight = ""
  b = Split(Sour, ",")
  For i = 0 To 5
    '对数据进行冒泡法排序
    For j = i + 1 To 6
      If Val(b(i)) > Val(b(j)) Then a = b(i): b(i) = b(j): b(j) = a
    Next
  Next
  For i = 0 To 2
    f = True
    For j = Val(b(i)) + 1 To Val(b(i)) + 4
      If InStr("," & Sour, "," & j & ",") = 0 Then
        f = False
        Exit For
      End If
    Next
    If f Then
      a = ""
      For j = 0 To 6
        a = a & b(j) & ","
      Next
      Straight = a
      Exit Function
    End If
  Next
End Function
Private Sub PermComb(ByVal Sour As String, Result As String)
  '递归排列组合所有
  Dim b() As String, a As String, i As Integer
  b = Split(Sour, ",")
  If UBound(b) = 7 Then
    a = Straight(Sour)
    If a <> "" And InStr(Result, a) = 0 Then Result = Result & a & "|" '输出有顺子的不重复的结果
    Exit Sub
  End If
  For i = 1 To 13
    If InStr("," & Sour, "," & i & ",") = 0 Then PermComb Sour & i & ",", Result
  Next
End Sub
Private Sub Command1_Click()
  Dim a As String, b() As String, c() As String, d As String, i As Integer, j As Integer, s As Integer
  Randomize
  s = Int(Rnd * 3) + 5    '随机数据个数
  a = ""
  j = Int(Rnd * 13) + 1
  For i = 1 To s
    While InStr(a, j) > 0
      j = Int(Rnd * 13) + 1
    Wend
    a = a & j & ","
  Next
  '上述代码产生随机数据在a里
  'a = "10,2,5,3,9,"     '该数据实验用
  'a = "6,3,11,12,4,"
  's = 5                 '该数据模拟实验用
  Text1 = "原数据:" & Left(a, Len(a) - 1)
  d = ""
  PermComb a, d
  If d <> "" Then
    Text1 = Text1 & ",可以组成顺子。"
    If s < 7 Then
      b = Split(d, "|")
      For i = 0 To UBound(b) - 1
        c = Split(b(i), ",")
        d = ""
        For j = 0 To 6
          If InStr("," & a, "," & c(j) & ",") = 0 Then d = d & c(j) & ","    '这里同前面一样,加个逗号把数字分开即可
        Next
        Text1 = Text1 & d & "|"
      Next
    End If
  Else
    Text1 = Text1 & ",无法组成顺子。"
  End If
End Sub
2014-05-20 09:58
lowxiong
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:27
帖 子:653
专家分:3402
注 册:2008-5-7
收藏
得分:0 
没有输出时因为前面随机数据个数在s里,你模拟时只有5个数,这时必须s=5(如果不改,随机s=7则没有数据输出了)
2014-05-20 10:01
softshl1
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2014-5-19
收藏
得分:0 
非常感谢
2014-05-20 10:10
快速回复:求大神出手 连续数字算法 也可以说为顺子算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022929 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved