PAT Basic Level 1069 微博转发抽奖 (20 分)

题目链接:

https://pintia.cn/problem-sets/994805260223102976/problems/994805265159798784

AC代码:

#include <bits/stdc++.h>
using namespace std;

vector<string> list_;
vector<string> lottery;

int main(){
    int M,N,S;
    scanf("%d%d%d",&M,&N,&S);
    getchar();
    int m=M;
    while(m--){
        string str;
        cin>>str;
        getchar();
        list_.push_back(str);
    }
    if(M<S){
        printf("Keep going...");
        return 0;
    }
    for(int i=S-1;i<M;i+=N){
        if(i==S-1){
            lottery.push_back(list_[i]);
        }
        else{
            while(find(lottery.begin(),lottery.end(),list_[i])!=lottery.end()){
            //找到了
                i++;
            }
            lottery.push_back(list_[i]);
        }
    }
    for(int i=0;i<lottery.size();i++){
        cout<<lottery[i]<<endl;
    }
}

二刷:(15分代码,测试点3过不了...)

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<string> zhong;
string peo[1010];

int main(){
    int m,n,s,i,j;
    cin>>m>>n>>s;
    getchar();

    for(i=1;i<=m;i++){
        cin>>peo[i];
        getchar();
    }
    if(s>m){
        printf("Keep going...");
    }
    else{
        zhong.push_back(peo[s]);
        i=s+n;
        while(i<=m){
            if(find(zhong.begin(),zhong.end(),peo[i])!=zhong.end()){
                i++;
            }
            zhong.push_back(peo[i]);
            i+=n;
        }
    }
    int len=zhong.size();
    for(j=0;j<len;j++){
        cout<<zhong[j]<<endl;
    }
    return 0;
}

二刷(用map):

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

map<string, int> mp;
string peo[1010];

int main() {
	int m, n, s, i, j;
	cin >> m >> n >> s;
	getchar();

	for (i = 1; i <= m; i++) {
		cin >> peo[i];
		getchar();
	}
	if (s > m) {
		printf("Keep going...");
	}
	else {
		mp[peo[s]] = 1;
		cout << peo[s] << endl;
		i = s + n;
		while (i <= m) {
			if (mp[peo[i]] == 0) {
				mp[peo[i]] = 1;
				cout << peo[i] << endl;
				i = i + n;
			}
			else {
				i++;
			}
		}
	}
	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值