day23:发牌(易)、牛棚问题(难)、等差数列(难)

发牌

问题描述

编制一个模拟发牌的程序。有编号为1,2,3,4四个人,将一付去掉大小怪的扑克按照如下顺序排列梅花c0-c12,方块d0-d12,红桃h0--h12,黑桃s0-s12,然后按照1,2,3,4四个人的顺序发牌,问最后每个人手上的牌有哪些。

个人总结

  1. 推荐使用二维数组,我只使用了一维数组,那是因为我觉得将牌整齐排放,给定四个玩家0,1,2,3的初始下标,每4个一张牌刚好能排完

推荐用二维数组解:

cpp 复制代码
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    // 1. 初始化牌组:梅花c0-c12, 方块d0-d12, 红桃h0-h12, 黑桃s0-s12
    char suits[] = {'c', 'd', 'h', 's'}; // 四种花色
    char cards[52][2]; // 存储完整的52张牌
    
    // 构建完整的牌组
    int index = 0;
    for (int s = 0; s < 4; s++) { // 遍历四种花色
        for (int num = 0; num < 13; num++) { // 遍历每个花色的13个数字
            cards[index][0] = suits[s]; // 花色
            cards[index][1] = num;      // 数字
            index++;
        }
    }
    
    // 2. 发牌:创建4个玩家的牌组,每个玩家13张牌
    char players[4][13][2]; // players[0]对应玩家1,players[1]对应玩家2...
    int player_idx = 0;
    
    for (int i = 0; i < 52; i++) { // 遍历所有52张牌
        // 将当前牌分配给对应玩家
        players[player_idx][i/4][0] = cards[i][0];
        players[player_idx][i/4][1] = cards[i][1];
        
        // 切换到下一个玩家(1->2->3->4->1循环)
        player_idx = (player_idx + 1) % 4;
    }
    
    // 3. 处理输入输出
    int input_player;
    while (cin >> input_player) { // 读取多组输入
        int p = input_player - 1; // 转换为数组索引(1->0, 2->1...)
        
        // 输出该玩家的13张牌
        for (int i = 0; i < 13; i++) {
            if (i > 0) {
                cout << " "; // 牌之间加空格
            }
            cout << players[p][i][0] << " " << (int)players[p][i][1];
        }
        cout << endl; // 每组输出换行
    }
    
    return 0;
}

个人解法:

cpp 复制代码
#include <iostream>
#include <vector>
//#include <string>
//#include <algorithm>
#include <cmath>
//#include <iomanip>
//#include <set>
//#include <sstream>
//#include <bits/stdc++.h> 
using namespace std;


int main(){
	int n;
	while(cin >> n){
		int tag = n-1;
		char card;
		int num = 0;
		bool first = true;
		while(num < 4){
			if(tag / 13 > 0){
				num ++;
				if(num >  3){
					break;
				}
				tag = tag % 13;
			}
			switch(num){
				case 0:
					card = 'c';
					break;
				case 1:
					card = 'd';
					break;
				case 2:
					card = 'h';
					break;
				case 3:
					card = 's';
					break;
			}
			
			if(first){
				cout << card;
				first = false;
			}
			else{
  				cout << " " << card;
			}
			
			cout << " " << tag;
			tag += 4;
			
		}
		cout << endl;
	} 
	
	
	
	return 0;
}	

修理牛棚

问题描述

在一个暴风雨的夜晚,农民约翰的牛棚的屋顶、门被吹飞了。 好在许多牛正在度假,所以牛棚(牛棚的总数S:1<= S<=200)没有住满。 剩下的牛一个紧挨着另一个被排成一行安置在有屋顶的牛棚来过夜。 所以有些牛棚里有牛,有些没有。

所有的牛棚有相同的宽度,且宽度设为1。 因为有些门遗失,农民约翰需要架起新的木板作为门。 他的新木材供应者将会供应他任何他想要的长度,但是供应者只能提供有限数目的木板。 农民约翰想将他购买的木板总长度减到最少。

计算拦住所有有牛的牛棚所需木板的最小总长度。

输出所需木板的最小总长度作为的答案。

说明:拦住一个牛棚需要的木板长度为1,拦住相邻的三个牛棚则需要木板长度为3。

比如有牛的牛棚编号为:

3 5 8 10 11

并且只能使用两块木板,

则第一块木板从3到5,长度为3,

第二块木板从8到11,长度为4,

因此,需要木板的总长度为7。

个人总结

  1. 求所需木板最短长度,先求最大木板减去最小木板之间的距离,这是总长

  2. 再求每一对之间的间隙长度,将间隙长度由大到小排序

  3. 所需跳过的间隙,即间隙最大的部分,若间隙数小于木板数,就取间隙数;若间隙数大于木板数,即取木板数

cpp 复制代码
#include <iostream>
#include <vector>
//#include <string>
//#include <cmath>
#include <algorithm>
//#include <sstream>
using namespace std;
int main() {
    int M, C;
    cin >> M >> C;
    
    vector<int> stalls(C);  
    
    for (int i = 0; i < C; ++i) {
        cin >> stalls[i];
    }
    
    
    sort(stalls.begin(), stalls.end());
    
    vector<int> gaps;
    for (int i = 1; i < C; ++i) {
        int gap = stalls[i] - stalls[i-1] - 1;  
        gaps.push_back(gap);
    }
    
    
    sort(gaps.rbegin(), gaps.rend());
    
    
    int skip_gaps = 0;
    int skip_count = min(M-1, (int)gaps.size());  
    for (int i = 0; i < skip_count; ++i) {
        skip_gaps += gaps[i];
    }
    
    int total_length = (stalls.back() - stalls[0] + 1) - skip_gaps;
    
    cout << total_length << endl;
    
    return 0;
}

等差数列

问题描述

一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...) 在这个问题中a是一个非负的整数,b是正整数。

写一个程序来找出在双平方数集合S中长度为n的等差数列。双平方数集合是所有能表示成p2+q2的数的集合。

个人总结

  1. 本题需要解决三个主要问题:判断一个数是否可以表示为 p² + q²(0 ≤ p,q ≤ M),在双平方数集合中寻找长度为N的等差数列,排序输出:按特定规则排序输出结果。主要是暴力解法
cpp 复制代码
#include <iostream>
#include <cstring>
using namespace std;

int N, M;
int set[125000];
int sup;
int ans;

void initSet() {
    int pos;
    for (int i = 0; i <= M; ++i) {
        for (int j = i; j <= M; ++j) {
            pos = i * i + j * j;
            set[pos] = 1;
        }
    }
    sup = M * M + M * M;
}

bool isOk(int n) {
    return set[n] == 1;
}

void findOut() {
    ans = 0;
    int a, b, i;
    for (b = 1; b * (N - 1) <= sup; ++b) {
        for (a = 0; a + b * (N - 1) <= sup; ++a) {
            for (i = 0; i < N; ++i) {
                if (!isOk(a + b * i)) break;
            }
            if (i == N) {
                cout << a << " " << b << endl;
                ans++;
            }
        }
    }
}

int main() {
    memset(set, 0, sizeof(set));
    cin >> N >> M;
    initSet();
    findOut();
    if (ans == 0) cout << "NONE" << endl;
    return 0;
}

计算机英语翻译

第一段

  1. 电子计算机

在第二次世界大战期间,一个科学家和数学家团队,工作在Blethley公园,在伦敦的北部,发明了其中一台世界上首个全电子数据计算机:Colossus。在1943年12月,Colossus,内置了1500个vacuum管子,是可操作的。它被艾伦图灵领导的团队使用,在巨大的胜利愿望下去粉碎德国在Enigam编码的广播信息技术

incorporated

vacuum

Enigam

第二段

早在1939年,美国的John Atanasoff和Clifford Berry两人在洛娃州立大学独立的发明了电子机器的原型机。这个原型机和之后的研究被低调的完成了,之后便被电子数字integrator和计算机协会在1945年扩大研究。ENIAC创造了一项专利,这项专利在后面的几十年推翻,在1973年,当这台机器被公布,合作原则第一次被应用与Atanasoff-Berry计算机

integrator

integrator

overturned

incorporated

单词打卡

相关推荐
寻寻觅觅☆6 小时前
东华OJ-基础题-106-大整数相加(C++)
开发语言·c++·算法
fpcc6 小时前
并行编程实战——CUDA编程的Parallel Task类型
c++·cuda
偷吃的耗子7 小时前
【CNN算法理解】:三、AlexNet 训练模块(附代码)
深度学习·算法·cnn
l1t7 小时前
在wsl的python 3.14.3容器中使用databend包
开发语言·数据库·python·databend
赶路人儿7 小时前
Jsoniter(java版本)使用介绍
java·开发语言
化学在逃硬闯CS7 小时前
Leetcode1382. 将二叉搜索树变平衡
数据结构·算法
ceclar1238 小时前
C++使用format
开发语言·c++·算法
码说AI8 小时前
python快速绘制走势图对比曲线
开发语言·python
Gofarlic_OMS8 小时前
科学计算领域MATLAB许可证管理工具对比推荐
运维·开发语言·算法·matlab·自动化
lanhuazui108 小时前
C++ 中什么时候用::(作用域解析运算符)
c++