第一次组队

7 篇文章 0 订阅

Problem Description

编写程序,产生由2,3,5这3个数字符号所构成、长度为n的字符串,并且在字符串中对于任何一个子串而言,都不会有相邻的、完全相同的子串;

Input

字符串长度n(1<=n<=15);

Output

无相邻重复子串的所有字符串,每个字符串换行输出。

Sample Input

4

Sample Output

2325
2352
2353
2523
2532
2535
3235
3252
3253
3523
3525
3532
5232
5235
5253
5323
5325
5352

 

#include <stdio.h>
#include <string.h>
char ch[3]={'2','3','5'};//产生由2,3,5的字符串
char a[20],t;
int pandu(char c[20],int n)
{
    int i,j,m,x,y,nn;
    y=1;
    for(i=0;i<n;i++){
        //if(y){
		for(j=i+1;j<n;j++){
			x=0;
			if(c[i]==c[j]){
				m=i;
				nn=j;
				while(1){
					if(nn<n && c[m]==c[nn])
					{
						m++;
						nn++;
						x++;
					}
					else
						break;
				}
			}
			if(x==j-i){
				return 0;
                y=0;
                break;}
			//else return 1;
		} 
        //}
    }
    return 1;
}
void f(int n,char c)//开始填入第n个字符,上一个字符是c
{
    int i,j,k;
    if (n>=t)//如果长度合适
    {
        if (pandu(a,n))//判断“字符串中对于任何一个子串而言,都不会有相邻的、完全相同的子串”
        {
            for (i=0;i<n;i++)//满足条件输出
                printf("%c",a[i]);
            puts("");
        }
        return;
    }
    for (i=0;i<3;i++)//可能填入ch[0]、ch[1]、ch[2]这三个字符
    {
        if (ch[i]!=c)//填入的字符与上一个字符不同的话
        {
            a[n]=ch[i];//第n个字符为ch[i]
            f(n+1,ch[i]);//填入下一个字符(n+1个),当前填入的是ch[i]
            a[n]=0;//可不写
        }
    }
}
int main()
{
    int n,i,j;
    while (scanf("%d",&t)!=EOF)
    {
        memset(a,0,sizeof(a));
        f(0,'r');
        //f(0);
    }
    return 0;
}


 

 

 

 

 

作为学校编程队的教练,如果我要从n名选手中选择两个组成团队,且他们的智商总和需要达到或超过m,这个问题可以转化为组合数学中的二维背包问题或者有序数组的两数之和问题的变种。 我们可以使用哈希表(unordered_map)来解决这个问题。首先遍历每个选手,对于每一个选手,我们检查他的智商加上哈希表中当前目标值(m - x[i])是否已经存在。如果存在,那么就找到了一对满足条件的组合;如果不存在,我们将选手的智商添加到哈希表中,继续寻找下一个选手。 当找到所有可能的组合后,统计哈希表的大小就是我们有多种选择。因为哈希表中的键是另一个选手的智商,值是对应的选手编号,所以每个键只会在哈希表中出现一次,除非能找到它的配对。 以下是简单的 C++ 代码示例: ```cpp #include <iostream> #include <unordered_map> int findTeams(int n, int m, std::vector<int>& x) { std::unordered_map<int, int> pairs; int count = 0; for (int i = 0; i < n; ++i) { if (pairs.find(m - x[i]) != pairs.end()) { count += pairs[m - x[i]]; // Remove the found pair from map to avoid duplicates pairs.erase(m - x[i]); } // Add current player's IQ to the map with its index pairs[x[i]] = i + 1; } return count; } int main() { int n, m; std::cin >> n >> m; std::vector<int> x(n); // Read IQs of all players int teamCombinations = findTeams(n, m, x); std::cout << "共有 " << teamCombinations << " 种选择组合。\n"; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值