考研408-数据结构(上机) --华中科技大学

1 篇文章 0 订阅

AcWing - 考研408-数据结构(上机)


3592. 矩阵转置 - AcWing题库

输入一个 𝑁×𝑁 的矩阵,将其转置后输出。

#include<bits/stdc++.h>

using ll=long long;
using ari=std::array<int,3>;
using PII=std::pair<int,int>;

#define fir first
#define sec second

const int N=100+10;
const int mod=1e9+7;
const double eps=1e-6;

int n;
int g[N][N];
void solve()
{
	std::cin>>n;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			std::cin>>g[i][j];
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=i;j++)
		{
			std::swap(g[i][j],g[j][i]);
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			std::cout<<g[i][j]<<" ";
		}
		std::cout<<'\n';
	}
}
signed main()
{
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	
	int t=1;
	//std::cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}

3593. 统计单词 - AcWing题库

编写一个程序,读入用户输入的,以 . 结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。 (凡是以一个或多个空格隔开的部分就为一个单词)

#include<bits/stdc++.h>

using ll=long long;
using ari=std::array<int,3>;
using PII=std::pair<int,int>;

#define fir first
#define sec second

const int N=100+10;
const int mod=1e9+7;
const double eps=1e-6;

std::string s;
void solve()
{
	while(std::cin>>s)
	{
		if(s[s.length()-1]!='.') std::cout<<s.length()<<" ";
		else {
			std::cout<<s.length()-1<<" ";
			break;
		}
	}
}
signed main()
{
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	
	int t=1;
	//std::cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}

 3594. IP地址 - AcWing题库

输入一个 ip 地址串,判断是否合法。

一个合法的 ip 地址串,其形式为 a.b.c.d,其中 a,b,c,d𝑎,𝑏,𝑐,𝑑 都是 0∼2550∼255 的整数。

模拟题但是wa了好几发。。。 

#include<bits/stdc++.h>

using ll=long long;
using ari=std::array<int,3>;
using PII=std::pair<int,int>;

#define fir first
#define sec second

const int N=100+10;
const int mod=1e9+7;
const double eps=1e-6;

std::string s;
void solve()
{
	while(std::cin>>s)
	{
		int flag=1;
		int num=0;
		for(int i=0;i<s.length();i++)
		{
			if(s[i]=='.')
			{
				if(num>=0&&num<=255) num=0;
				else {
					flag=0;
					std::cout<<"No!\n";
					break;
				}
			}else if(s[i]>='0'&&s[i]<='9'){
				num=num*10+s[i]-'0';
			}else{
				flag=0;
				std::cout<<"No!\n";
				break;
			}
		}
		if(flag) 
		{
			if(num<0||num>255) std::cout<<"No!\n";
			else std::cout<<"Yes!\n";
		}
	}
}
signed main()
{
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	
	int t=1;
	//std::cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}

3595. 二叉排序树 - AcWing题库

开两个map分别存对于节点i的左边是谁和右边是谁。

dfs的过程就是如果当前没有根节点直接返回-1并且更新根节点为当前节点。

每次新进来一个节点就从根节点开始搜,如果当前节点比根节点大就往右边搜索,否则往左边。

#include<bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using ari = std::array<int, 3>;
using PII = std::pair<int, int>;

const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const double eps = 1e-6;

std::map<int, int> l, r;
int rt = -1;

int dfs(int fa, int x) {
    if (fa == -1) {
        rt = x;
        return -1;
    }
    if (x > fa) {
        if (r.count(fa) == 0) {
            r[fa] = x;
            return fa;
        } else return dfs(r[fa], x);
    }
    if (x < fa) {
        if (l.count(fa) == 0) {
            l[fa] = x;
            return fa;
        } else return dfs(l[fa], x);
    }
}

void solve() {
    int n;
    std::cin >> n;

    int x;
    while (n--) {
        std::cin >> x;
        std::cout << dfs(rt, x) << '\n';
    }
}

signed main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);

    int t = 1;
    //std::cin>>t;
    while (t--) {
        solve();
    }

    return 0;
}

3596. a+b - AcWing题库

高精度加法没啥可说。

注意多测要清空数组即可。

#include<bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using ari = std::array<int, 3>;
using PII = std::pair<int, int>;

const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const double eps = 1e-6;

std::string A, B;
int a[N], b[N], c[N];

void solve() {
    while (std::cin >> A >> B) {
        std::reverse(A.begin(), A.end());
        std::reverse(B.begin(), B.end());

        memset(a, 0, sizeof a);
        memset(b, 0, sizeof b);
        int len = std::max(A.length(), B.length());
        for (int i = 0; i < A.length(); i++) {
            a[i] = A[i] - '0';
        }
        for (int i = 0; i < B.length(); i++) {
            b[i] = B[i] - '0';
        }
        int jw = 0;
        for (int i = 0; i < len; i++) {
            c[i] = a[i] + b[i] + jw;
            jw = c[i] / 10;
            c[i] %= 10;
        }
        if (jw) c[len++] = jw;
        for (int i = len - 1; i >= 0; i--) {
            std::cout << c[i];
        }
        std::cout << '\n';
    }
}

signed main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);

    int t = 1;
    //std::cin>>t;
    while (t--) {
        solve();
    }

    return 0;
}

3597. 特殊排序 - AcWing题库

#include<bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using ari = std::array<int, 3>;
using PII = std::pair<int, int>;

const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const double eps = 1e-6;

int a[N];

void solve() {
    int n;
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        std::cin >> a[i];
    }
    std::sort(a + 1, a + 1 + n);
    
    std::cout << a[n] << '\n';
    
    if (a[1] == a[n]) {
        std::cout << -1 << '\n';
        return;
    }
    for (int i = 1; i <= n - 1; i++) {
        std::cout << a[i] << " ";
    }
}

signed main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);

    int t = 1;
    //std::cin>>t;
    while (t--) {
        solve();
    }

    return 0;
}

3598. 二叉树遍历 - AcWing题库

给定中序+先/后序,输出后/先序

这类题目复习一下即可。

P-[NOIP2001]求先序排列_2021秋季算法入门班第二章习题:递归、分治

#include<bits/stdc++.h>
std::string fir,mid,ans;
void solve(int l1,int r1,int l2,int r2){
	if(l1>r1) return ;
	char rt=fir[l1];
	
	int m;
	for(int i=l2;i<=r2;i++){
		if(mid[i]==rt){
			m=i;
			break;
		}
	} 
	solve(l1+1,m-l2+l1,l2,m-1);
	solve(m+1-r2+r1,r1,m+1,r2);
	std::cout<<rt;
}
int main() {
    while(std::cin>>fir>>mid){
    	int len=fir.length();
    	solve(0,len-1,0,len-1);
    	std::cout<<'\n';
	} 
    return 0;
}

3599. 奇偶校验 - AcWing题库

模拟即可。唯一需要注意的是ASCII不能直接-'0'转化,要用int转

#include<bits/stdc++.h>

void deal(int x) {
    std::string s;
//	0 1 10 11 100
//  4%2=0,4
    int c = 0;
    while (x) {
        if (x % 2 == 1) c++;
        s += '0' + x % 2;
        x /= 2;
    }
    std::reverse(s.begin(), s.end());
    int t = 7 - (int) s.length();
    while (t--) {
        s = '0' + s;
    }
    if (c % 2) s = '0' + s;
    else s = '1' + s;
    std::cout << s << '\n';
}

int main() {
    std::string s;
    std::cin >> s;

    for (auto i: s) {
        deal((int) i);
    }
    return 0;
}

3600. 最大的两个数 - AcWing题库

#include<bits/stdc++.h>

const int N = 2e3 + 10;
int g[N][N], a[N][2];

int main() {
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= 5; j++) {
            std::cin >> g[i][j];
        }
    }
    for (int j = 1; j <= 5; j++) {
        int mmax = -1e9, max = -1e9;
        int tag1 = 1, tag2 = 1;

        for (int i = 1; i <= 4; i++) {
            if (g[i][j] <= mmax) {
                if (g[i][j] > max) {
                    max = g[i][j];
                    tag2 = i;
                }
            } else {
                if (mmax > max) {
                    max = mmax;
                    tag2 = tag1;
                }
                mmax = g[i][j];
                tag1 = i;
            }
        }
        if (tag1 < tag2) a[j][0] = max, a[j][1] = mmax;
        else a[j][0] = mmax, a[j][1] = max;
    }
    for (int j = 1; j >= 0; j--) {
            for (int i = 1; i <= 5; i++) {
                std::cout << a[i][j] << " ";
            }
        std::cout << '\n';
    }
    return 0;
}

3601. 成绩排序 - AcWing题库

#include<bits/stdc++.h>

const int N = 2e5 + 10;
struct stu {
    std::string name;
    int age, score;
} a[N];

bool cmp(stu a, stu b) {
    if (a.score != b.score) return a.score < b.score;
    else if (a.name != b.name) return a.name < b.name;
    else return a.age < b.age;
}

int main() {
    int n;
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        std::cin >> a[i].name >> a[i].age >> a[i].score;
    }
    std::sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= n; i++) {
        std::cout << a[i].name << " " << a[i].age << " " << a[i].score << '\n';
    }
    return 0;
}

3602. 守形数 - AcWing题库

#include<bits/stdc++.h>

const int N = 2e5 + 10;

int main() {
    int n;
    while (std::cin >> n) {
        int p = n * n;
        std::string s = std::to_string(p);
        std::string q = std::to_string(n);

        int cl = s.length(), dl = q.length();
        int l = 0, r = cl - dl;
        while (s[r] == q[l] && l != dl) {
            l++, r++;
        }
        if (l != dl) {
            std::cout << "No!\n";
        } else std::cout << "Yes!\n";
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值