首页 > 试题广场 >

七星不靠-研发

[编程题]七星不靠-研发
  • 热度指数:1019 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解

七星不靠是中国麻将竞赛规则番种,胡牌时由东南西北中发白7张,外加其他花色的147、258、369不相连的牌型,且没有将牌而组成。

--百度百科



七星不靠中的七星是指:东西南北中发白,也就是牌中必须有这七张。而其它牌按下述的来拼全:

东西南北中发白+147万+258饼+369条

东西南北中发白+147万+258条+369饼

东西南北中发白+147条+258万+369饼

东西南北中发白+147条+258饼+369万

东西南北中发白+147饼+258条+369万

东西南北中发白+147饼+258万+369条

由于胡牌时只需要14张牌,而上述组合均有16张,那么除了东西南北中发白必须有外,其它三色可以随便去掉两张,都可以组成七星不靠。


我们的任务是,假设我们的14张牌中已经包含了东西南北中发白这7张牌,另外的牌都是万饼条的序数牌,给出另外的这7张牌,判断是否能组成七星不靠。

数据范围: 数据组数
进阶:每组数据的空间复杂度 ,时间复杂度

输入描述:

输入第一行为一个正整数T(T<=1000),表示有T组数据。

每组数据一行,包含7张牌,每张牌用”XY”表示,X为[1...9]的数字,Y为(“W”,”B”,”T”)中的一个,分别表示万,饼,条。有可能出现同样的"XY"但是数量不会超过4个。

保证数据一定合法。



输出描述:

对于每一组数据,输出YES或者NO,分别表示可以或者不可以组成七星不靠。

示例1

输入

4
1T 4T 7T 2B 5B 8B 9W
1T 2T 3T 4T 5T 6T 7T
1B 2W 3T 4B 5W 6T 8W
2B 8B 5B 2B 6T 7W 4W

输出

YES
NO
YES
NO
def fun():
    temp = set(input().split())
    if len(temp) < 7:
        return 'NO'
    test = [{'1','4','7'}, {'2','5','8'}, {'3','6','9'}]
    a = [set() for _ in range(3)]
    for te in temp:
        if te[1] == 'T':
            a[0].add(te[0])
        elif te[1] == 'W':
            a[1].add(te[0])
        else:
            a[2].add(te[0])
    for b in a:
        for te in test:
            if b | te == te:
                test.remove(te)
                break
        else:
            return 'NO'
    return 'YES'
if __name__ == '__main__':
    T = int(input())
    for _ in range(T):
        print(fun())

发表于 2021-08-24 19:43:28 回复(0)
不跟你整那些花里胡哨的,直接暴力统计。
#include <iostream>
#include <set>
using namespace std;

int main() {
    int N;
    while(cin >> N){
        while(N--){
            int n = 7;
            set<string> st1;
            set<string> st2;
            set<string> st3;
            set<string> st4;
            set<string> st5;
            set<string> st6;
            while(n--){
                string str;
                cin >> str;
                if(str == "1W" || str == "4W" || str == "7W" || str == "2T" || str == "5T" || str == "8T" || str == "3B" || str == "6B" || str == "9B"){
                    st1.insert(str);
                }
                if(str == "1W" || str == "4W" || str == "7W" || str == "2B" || str == "5B" || str == "8B" || str == "3T" || str == "6T" || str == "9T"){
                    st2.insert(str);
                }
                if(str == "1T" || str == "4T" || str == "7T" || str == "2W" || str == "5W" || str == "8W" || str == "3B" || str == "6B" || str == "9B"){
                    st3.insert(str);
                }
                if(str == "1T" || str == "4T" || str == "7T" || str == "2B" || str == "5B" || str == "8B" || str == "3W" || str == "6W" || str == "9W"){
                    st4.insert(str);
                }
                if(str == "1B" || str == "4B" || str == "7B" || str == "2W" || str == "5W" || str == "8W" || str == "3T" || str == "6T" || str == "9T"){
                    st5.insert(str);
                }
                if(str == "1B" || str == "4B" || str == "7B" || str == "2T" || str == "5T" || str == "8T" || str == "3W" || str == "6W" || str == "9W"){
                    st6.insert(str);
                }
            }
            if(st1.size() < 7 && st2.size() < 7 && st3.size() < 7 && st4.size() < 7 && st5.size() < 7 && st6.size() < 7) cout << "NO" << endl;
            else cout << "YES" << endl;
        }
    }
    return 0;
}


发表于 2021-07-18 16:26:36 回复(2)
1.提取独立的数字以及字母存储到列表当中
2.判断独立数字个数是否大于等于7 以及 独立字母个数是否大于等于3
T=int(input().strip())
for j in range(T):
    A=input().strip().split()
    unum=[]
    ualpha=[]
    for i in range(7):
        for j in range(2):
            if A[i][j]>='1' and A[i][j]<='9':
                if A[i][j] not in unum:
                    unum.append(A[i][j])
            else:
                if A[i][j] not in ualpha:
                    ualpha.append(A[i][j])
    if len(unum)>=7 and len(ualpha)>=3:
        print('YES')
    else:
        print('NO')


发表于 2021-05-21 09:10:22 回复(2)
先将不同花色的保存在unordered_map中
1.如果有一个花色的牌数量大于3就排除
2.通过遍历,将所有花色的牌都%3,如果有同一花色的%3不一样的就排除
3.将所所有的%3后的结果统计一下,是否将所有的0,1,2的情况全部遍历得到
#include<iostream>
#include<unordered_map>
#include<vector>
#include<sstream>
#include<cmath>
using namespace std;

int main(){
    int n;
    cin >> n;
    cin.get();
    while(n){
        unordered_map<char, vector<int>> pai;
        int flag = 1;
        string line, word;
        getline(cin, line);
        stringstream ss;
        ss << line;
        while(ss >> word){
            pai[word[1]].push_back(word[0] - '0');
            if(pai[word[1]].size() > 3){
                flag = 0;
                break;
            }
        }
        if(flag){
            int tempflag = 0;
            for(auto &x : pai){
                int i = x.second[0] % 3;
                for(auto y : x.second){
                    if(y % 3 != i){
                        flag = 0;
                        break;
                    }
                }
                if(flag) tempflag += pow(2, i);
            }
            if(tempflag != 7)flag = 0;
        }
        if(flag) cout << "YES" << endl;
        else{
            cout << "NO" << endl;
        }
        --n;
    }
}


编辑于 2022-05-07 16:06:48 回复(0)
1、看了一圈好像很多人都没有做全,只是过了题目测试用例,其中相同花色 假如不是137,258,369这样的排列是不正确的,
2、这里有一个简单的方法判断他们是不是这样的组合,就是和3取余。
import java.util.*;

public class Main{
 public static void main(String []args){
        Scanner s=new Scanner(System.in);
        int len=s.nextInt();
        s.nextLine();

        String inNumber;
        Map<Character, ArrayList<Integer>> map=new HashMap<>();
        int i=0;
      
        while(i<len){
              String uu=s.nextLine();
            boolean chufuFlag=false;
            String[]hang=uu.split(" ");
            for(int j=0;j<hang.length;j++){
                inNumber=hang[j];
                if(inNumber.length()!=2){
                    chufuFlag=true;
                }
                if(!chufuFlag){
                    if(!map.containsKey(inNumber.charAt(1))){
                        map.put(inNumber.charAt(1),new ArrayList<>());
                    }
                    if(map.get(inNumber.charAt(1)).contains(Integer.parseInt(String.valueOf(inNumber.charAt(0))))){
                        chufuFlag=true;
                    }
                    map.get(inNumber.charAt(1)).add(Integer.parseInt(String.valueOf(inNumber.charAt(0))));
                }
            }
            i++;
            if(!chufuFlag){
                assertTrue(map);
            }else{
                System.out.println("NO");
            }
            map.clear();
        }
    }

    private static void assertTrue(Map<Character,ArrayList<Integer>> map){
        if(map.size()>3) {
            System.out.println("NO");
            return;
        }
        for(Map.Entry<Character,ArrayList<Integer>>entry:map.entrySet()){
            int count=0,flag=0;
            boolean flagRe=false;
            for(Integer num:entry.getValue()){
                if(count==0){
                    flag=num%3;
                }else{
                    if((num%3)!=flag){
                        System.out.println("NO");
                        return;
                    }
                }
                count++;
            }

        }
        System.out.println("YES");
    }



编辑于 2021-08-12 13:44:06 回复(0)
其实这题只要不存在相等的值就是答案了,使用set去重,再去判断是否有数被去重掉了。
#include<bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin >> n;
	while (n--) {
		string s;
		set<char>w,t,b;
		for(int i=0;i<7;i++){
			cin>>s;
			if(s[1]=='W'){
				w.insert(s[0]);
			}
			else if(s[1]=='T'){
				t.insert(s[0]);
			}
			else{
				b.insert(s[0]);
			}
		}
		if(w.size()>3||w.size()>3||b.size()>3||w.size()+t.size()+b.size()<7)cout<<"NO"<<'\n';
		else cout<<"YES"<<'\n';
	}
	return 0;
}


发表于 2022-06-17 19:39:55 回复(1)
#include <bits/stdc++.h>

using namespace std;

bool isWin(vector<string> tmp){
    //1T 4T 7T 2B 5B 8B 9W
    //1T 2T 3T 4T 5T 6T 7T
    //1B 2W 3T 4B 5W 6T 8W
    //2B 8B 5B 2B 6T 7W 4W
    //T W B 个数均不能超过3
    int Tnum = 0, Wnum = 0, Bnum = 0;
    unordered_map<int, char> m;
    for(int i = 0; i < tmp.size(); i++){
        string str = tmp[i];
        if(str[1] == 'T') Tnum++;
        else if(str[1] == 'W') Wnum++;
        else /*if(str[0] == 'B')*/ Bnum++;
    }

    if(Tnum <= 3 && Wnum <= 3 && Bnum <= 3){
        return true;
    }
    return false;
}

int main(){
    int rows = 0;
    cin >> rows;
    vector<vector<string>> vec;
    for(int i = 0; i < rows; i++){
        vector<string> tmp;
        for(int j = 0; j < 7; j++){
            string temp;
            cin>>temp;
            //if(getchar() == '_') 
            tmp.push_back(temp);
        }
        vec.push_back(tmp);
    }
    
    for(int i = 0; i < rows; i++){
        bool res = isWin(vec[i]);
        if(res) cout<<"YES"<<endl;
        else cout << "NO" <<endl;
    }

    return 0;
}
发表于 2022-04-16 21:41:28 回复(0)
#include<iostream>
#include<vector>

using namespace std;

void count(vector<int>& nums, vector<int>& numsRec) {
	if (nums.size() < 1)
		return;
	int yu;
	yu = nums[0] % 3;
	for (int i = 0; i < nums.size(); i++) {
		if ((nums[i] % 3) != yu) {
			cout << "NO" << endl;
			return;
		}
		numsRec[nums[i]] = 1;
	}
}

void test() {

	vector<int> nums(7,0);
	vector<char> str(7,0);
	vector<int>	nums1;
	vector<int>	nums2;
	vector<int>	nums3;
	vector<int> numsRec(10, 0);
	for (int i = 0; i < 7; i++) {
		cin >> nums[i];
		cin >> str[i];
		if (str[i] == 'T') {
			nums1.push_back(nums[i]);
		}
		if (str[i] == 'W') {
			nums2.push_back(nums[i]);
		}
		if (str[i] == 'B') {
			nums3.push_back(nums[i]);
		}
	}

	//cout << nums1.size() << endl;
	//cout << nums2.size() << endl;
	//cout << nums3.size() << endl;

	count(nums1, numsRec);
	count(nums2, numsRec);
	count(nums3, numsRec);

	int sum = 0;
	for (int num : numsRec)
		sum += num;
	if (sum != 7)
	{
		cout << "NO" << endl;
		return;
	}
	cout << "YES" << endl;
}


int main() {

	int n;
	cin >> n;
	while (n--) {
		test();
	}

	system("pause");
	return 0;
}

发表于 2022-03-16 11:20:14 回复(0)
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main(){
    int n;
    cin >> n;
    
    while(n--){
        vector<int>T(10,0);
        int T_num =0,W_num=0,B_num = 0;
        vector<int>W(10,0);
        vector<int>B(10,0);
        int flag = 0;
        for(int i=0;i<7;i++){
            string str;
            cin >> str;
            int num = str[0]-'0';
            if(str[1]=='T'){
            	if(T[num]||B[num]||W[num]){
            		flag  = 1;
				}

                T[num]++;
                T_num++;
                
            }else if(str[1] == 'B'){
            	if(T[num]||B[num]||W[num]){
            		flag  = 1;
				}
                B[num]++;
                B_num++;
            }else{
            	if(T[num]||B[num]||W[num]){
            		flag  = 1;
				}

                W[num]++;
                W_num++;
            }
        }
//        cout << T_num << endl << B_num << endl<< W_num<< endl;
        if(T_num>3 || B_num>3 || W_num>3 || flag){
            	cout<<"NO"<<endl;
            	continue;
			}
		
		cout<<"YES"<<endl;
        
    }
return 0;
}

发表于 2022-03-01 13:54:20 回复(0)
#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
int main() {
	int n;
	cin >> n;
	while (n-- > 0) {
		vector<string>cards;
		while (1) {
			string card;
			cin >> card;
			cards.push_back(card);
			if (cin.get() == '\n') break;
		}
		sort(cards.begin(), cards.end());
		vector<int> record(9);
		unordered_map<char, int> M;
		unordered_map<char, char>preBTW;
		M['B'] = 3; M['T'] = 3; M['W'] = 3;
		preBTW['B'] = '*'; preBTW['T'] = '*'; preBTW['W'] = '*';
		int f = true;
		for (int i = 0; i < 7;i++) {
			string card = cards[i];
			if (record[card[0] - '0' - 1] || M[card[1]] <= 0 || (preBTW[card[1]] != '*' && (card[0] - preBTW[card[1]])%3!=0)) {
				std::cout << "NO" << endl;
				f = false;
				break;
			}
			preBTW[card[1]] = card[0];
			record[card[0] - '0' - 1]++;
			M[card[1]]--;
		}
		if(f) std::cout << "YES" << endl;
	}
}

发表于 2021-08-07 17:01:52 回复(0)

判断可以组成七星不靠的条件

  1. 所有的数字不能重复(共7个数字)
  2. 条,饼,万每种类型内的元素个数最多不得大于3个,最少不得小于1个
    因此按条饼万三类分别将元素分组以后。
    必有至少一组元素个数为3,元素个数为3的组内元素必满足147,258,369三种情况中的一种
    元素个数为2的组内元素必满足从147,258,369三种情况中抽去一个数字,因此这两个数之差绝对值要么为3要么为6。
    可以用set容器去重,将各个元素分类后排序后先判断是否满足第一条7个数字之间不重复,不满足则直接输出NO,接下来讨论每一类元素个数,只有同时满足三元素组,二元素组的条件下,才输出YES
    #include<iostream>
    #include<vector>
    #include<string>
    #include<algorithm>
    #include<set>
    using namespace std;
    void judge(int n){
     for(int i=0;i<n;++i){
         set<int> MW,MB,MT,MM;
         string temp;
         int size = 7;
         while(size--){
             cin>>temp;
             if(temp[1]=='W') MW.insert(temp[0]-'0');
             else if(temp[1]=='B') MB.insert(temp[0]-'0');
             else if(temp[1]=='T') MT.insert(temp[0]-'0');
         }
         vector<string> vec(3);
         for(auto m:MW) {vec[0]+=(m+'0');MM.insert(m);}
         for(auto m:MB) {vec[1]+=(m+'0');MM.insert(m);}
         for(auto m:MT) {vec[2]+=(m+'0');MM.insert(m);}
         //总共7个 没有重复。
         if(MM.size()!=7){
             cout<<"NO"<<endl;
             continue;
         }
         bool f2=true;
         for(auto v:vec){
             if(v.size()==3){
                 if(v[2]-v[1]!=3 || v[1]-v[0]!=3) f2 = false;
             }
             else if(v.size()==2){
                 if(v[1]-v[0] != 3 && v[1]-v[0] != 6) f2 = false;
             }
             else if(v.size()==1){}
             else f2 = false;
         }
         if(f2) cout<<"YES"<<endl;
         else cout<<"NO"<<endl;
     }
    }
    int  main(){
     int n;
     while(cin>>n){
         judge(n);
     }
    }
发表于 2021-08-06 17:45:46 回复(0)
其实就这要考虑下面情况即可
1.每个类型的总数不能超过3,不能小于1 
2.每个类型不能有重复元素
3.所有的类型之和不能有重复元素
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

bool nodep(string s,int size){
        if(size == 1){
            return true;
        }else if(size == 2){
           return s[0]==s[1]?false:true;
        }else{
            if(s[0]==s[1]||s[1]==s[2]||s[2]==s[0]){
                return false;
            }else{
                return true;
            }
            
        }
    }

bool nocmm(string B,string T,string W){
    string con = B+T+W;
    sort(con.begin(),con.end());
    int low = 0;
    int fast = 1;
    while(fast<con.size()){
        if(con[low]==con[fast]){
            return false;
        }
        low++;
        fast++;
    }
    return true;
    
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        string B = "";
        string T = "";
        string W = "";
        for(int k=0;k<7;k++){
            string temp="";
            cin>>temp;
            char nums = temp[0];
            if(temp.back()=='B'){
                B.push_back(nums);
            }else if(temp.back()=='T'){
                T.push_back(nums);
            }else{
                W.push_back(nums);
            }
        }
        //一组数据接收完成 数据接收正常
        int B_size = B.size();
        int T_size = T.size();
        int W_size = W.size();
        if(B_size>=4||B_size<=0||T_size>=4||T_size<=0||W_size>=4||W_size<=0){
            cout<<"NO"<<endl;
        }else{
            //不能有重复元素
            if(nodep(B,B_size)&&nodep(T,T_size)&&nodep(W,W_size)&&nocmm(B,T,W)){
                cout<<"YES"<<endl;
            }else{
                //cout<<"----";
                cout<<"NO"<<endl;
            }
             
        }
        
    }
    return 0;
}


发表于 2021-08-06 14:48:35 回复(0)
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;

int seven[3][3];
int check_Seven(vector<vector<int>>& num, int w, int b) {
	int pei[3] = { w,b,3 - w - b };
	int pipei = 0;
	for (int i = 0; i < num[0].size(); i++) {
		int k = 0;
		for (int j = k; j < 3; j++) {
			if (num[0][i] == seven[w][j]) {
				k++;
				pipei++;
				break;
			}
		}
	}
	if (pipei < 1)return -1;
	for (int i = 0; i <num[1].size(); i++) {
		int k = 0;
		for (int j = k; j < 3; j++) {
			if (num[1][i] == seven[b][j]) {
				k++;
				pipei++;
				break;
			}
		}
	}
	if (pipei < 4)return -1;
	for (int i = 0; i < num[2].size(); i++) {
		int k = 0;
		for (int j = k; j < 3; j++) {
			if (num[2][i] == seven[3 - w - b][j]) {
				k++;
				pipei++;
				break;
			}
		}
	}
	return 9 - pipei;
}
void sevenStar(string& str) {
	vector<vector<int>>num(3, vector<int>());
	for (int i = 0; i < str.size(); i += 3) {
		if (str[i + 1] == 'W')num[0].push_back(str[i] - '0');
		else if (str[i + 1] == 'B')num[1].push_back(str[i] - '0');
		else if (str[i + 1] == 'T')num[2].push_back(str[i] - '0');
	}
	for (int i = 0; i < 3; i++)sort(num[i].begin(), num[i].end());
	int check = num[0].size()*num[1].size()*num[2].size();
	if (check != 9 && check != 12)cout << "NO" << endl;
	else {
		int dec = 0;
		bool flag = false;
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				if (j == i)continue;
				dec=check_Seven(num, i, j);
				if (dec == 2)
				{
					flag = true;
					cout << "YES" << endl;
					break;
				}
				else dec = 0;
			}
			if (flag)break;
		}
		if (!flag)cout << "NO" << endl;
	}
}
int main() {
	int k = 1;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			seven[j][i] = k;
			k++;
		}
	}
	int group;
	cin >> group;
	cin.get();
	for (int i = 0; i < group; i++) {
		string str;
		getline(cin, str);
		sevenStar(str);
	}
	return 0;
}
因为数字过少,只有几种,直接暴力了
发表于 2021-08-06 13:55:13 回复(0)