zhangyikai321 2015-04-10 04:22 采纳率: 0%
浏览 1539

为什么就算不出来?是一道算法问题 感到纠结

今有7对数字:两个1,两个2,两个3,...两个7,把它们排成一行。
要求,两个1间有1个其它数字,两个2间有2个其它数字,以此类推,两个7之间有7个其它数字。如下就是一个符合要求的排列:

17126425374635

当然,如果把它倒过来,也是符合要求的。

请你找出另一种符合要求的排列法,并且这个排列法是以74开头的。

注意:只填写这个14位的整数,不能填写任何多余的内容,比如说明注释等。#include
#include
int a[14],c[14];
void paixu(int x){
if(x==0||x==1||x==8||x==6){paixu(x+1);}
if(x>=14){
for(int i=0;i int b=i+1+a[i];
if(b>=14){ b=i-a[i]-1;
if(a[i]!=a[b]) return ;
}
if(b<14) if(a[i]!=a[b]) return ;

        }
       for(i=0;i<14;i++)
           printf("%d",a[i]);

}

for(int b=1;b<=14;b++)
    for(int i=1;i<=7;i++){
    if(!c[b]){
    a[x]=i;
    c[b]=1;
    paixu(x+1);
    c[b]=0;
    }
}   

}

int main(){
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
a[0]=7,a[1]=4,a[8]=7,a[6]=4;
c[0]=1,c[1]=1,c[8]=1,c[6]=1;
paixu(1);

return 0;
}


  • 写回答

1条回答 默认 最新

  • threenewbee 2015-04-10 04:52
    关注

    又是那个傻桥出的馊题,用C#写一个,反正你只要答案

     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                foreach (var item in foo("74"))
                    Console.WriteLine(item);
            }
            static IEnumerable<string> foo(string current)
            {
                if (current.Length == 14)
                {
                    yield return current;
                }
                else
                {
                    var query = Enumerable.Range(1, 7).Select(x => current + x.ToString())
                        .Where(y =>
                        {
                            var chars = y.Select((z, i) => new { z, i })
                                .GroupBy(z => z.z);
                            return chars.All(z => z.Count() < 3 && (z.Last().i - z.First().i - 1) == (z.Count() == 2 ? z.Key - '0' : -1));
                        });
                    foreach (string s in query)
                        foreach (string s1 in foo(s))
                            yield return s1;
                }
            }
        }
    }
    
    

    74151643752362
    Press any key to continue . . .

    评论

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝