qq_45735316 2020-04-19 16:38 采纳率: 94.1%
浏览 224
已采纳

代码始终写不出来,这题要用多少次循环?该怎么循环?

图片说明图片说明

#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
int main()
{
    int m,n;
    cin>>m>>n;
    int a[n^m][m+1];
    memset(a,1,sizeof(a));
    for(int i=0;i<n^m;i++)
    {
        for(int j=0;j<m;j++)
        {

        }
    }
    for(int i=0;i<n^m;i++)
    {
        for(int j=1;j<m;j++)
        {
            if(j%2)           //奇数项
            {
                if(a[i][j]<a[i][j-1])
                     a[i][m]=0;
            }
            else
            {
                if(a[i][j]>a[i][j-1])
                     a[i][m]=0;
             } 
        }
     } 
     for(int i=0;i<n^m;i++)
     {
        if(a[i][m])
        {
            for(int j=0;j<m;j++)
            {
                cout<<a[i][j]<<" ";
             }
         }
         cout<<endl;
     }
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-04-20 19:36
    关注

    拿C#给你写一个

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Q1064385
    {
        class Program
        {
            static IEnumerable<List<int>> foo(List<int> seed, int seedn, int remain, List<int> range)
            {
                if (remain == 0)
                {
                    yield return seed;
                }
                else if (seed == null)
                {
                    foreach (var item in foo(Enumerable.Repeat(0, remain).ToList(), 0, remain, range))
                        yield return item;
                }
                else 
                {
                    Func<int, bool> cond1 = x => x > range.Min();
                    Func<int, bool> cond2 = x => x > seed[seedn - 1];
                    Func<int, bool> cond3 = x => x < seed[seedn - 1];
                    foreach (var i in range.Where(seedn == 0 ? cond1 : (seedn % 2 == 0 ? cond2 : cond3)))
                    {
                        var newlist = seed.ToList();
                        newlist[seedn] = i;
                        foreach (var item in foo(newlist, seedn + 1, remain - 1, range))
                            yield return item;
                    }
                }
            }
            static void Main(string[] args)
            {
                string s = Console.ReadLine();
                int m = int.Parse(s.Split(' ')[0]);
                int n = int.Parse(s.Split(' ')[1]);
                var result = foo(null, 0, m, Enumerable.Range(1, n).ToList());
                foreach (var item in result)
                    Console.WriteLine(string.Join(" ", item.Select(x => x.ToString())));
                Console.WriteLine(result.Count());
            }
        }
    }
    
    

    3 4
    2 1 2
    2 1 3
    2 1 4
    3 1 2
    3 1 3
    3 1 4
    3 2 3
    3 2 4
    4 1 2
    4 1 3
    4 1 4
    4 2 3
    4 2 4
    4 3 4
    14
    Press any key to continue . . .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 9月25日

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 请教一下c语言的代码里有一个地方不懂
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了