ecjtu-2020训练赛(2)

A:Second Order Statistics

链接:CF22A
题意:

给定一个数组,输出其中第二小的整数(相等的整数只计算一次)。

思路:

去重排序;

参考代码:

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

int main()
{
    
    
    int n;
    scanf("%d",&n);
    vector<int> all; // 这里采用vector存储
    for (int i = 1; i <= n; ++i)
    {
    
    
        int x; scanf("%d",&x);
        all.push_back(x);
    }
    // 去重排序下面两行代码就行
    sort(all.begin(), all.end());
    all.erase(unique(all.begin(), all.end()), all.end()); // unique函数是将all里面的相同的放到后面去,并返回第一个的位置
    if( all.size() == 1) puts("NO");                // 然后将后面的删除就能实现了去重排序
    else cout << all[1] << endl;
    return 0;
}

B:Die Roll

链接:CF9A

题意:

小Y,小W和小D进行扔骰子(六面)游戏,谁投出的点数最大算谁胜利,现在已知小Y和小W的得分,请你帮小D求出她获胜的概率
注意:
1.以"分子/分母"输出,特别的,若不可能获胜输出"0/1",100%获胜输出"1/1"
2.小Y和小W非常绅士,如果小D的得分和他们一样,他们也会算作小D获胜

思路:

求出Y,M点数最大值,然后逐步分析即可

int a, b, c;
    cin >>a >> b ;
    c=max(a,b);
    if(c == 4)cout<<"1/2";
    else if(c == 1) cout << "1/1";
    else if(c == 2) cout << "5/6";
    else if(c == 3)cout << "2/3";
    else if(c == 5)cout << "1/3";
    else if( c == 6)cout << "1/6";

C:Hexadecimal’s Numbers

链接:CF9C

题意:

输入n,输出1-n的自然数中各数位只包含0和1的数的个数。

思路:

可以发现,各数位是0,1的数从小到大:1,10,11,100,101, 110,111···
如果从1开始可以发现;1 -> 10 / 11,然后10 - > 100 / 101, 11 -> 110, 111·····

参考代码:

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

long long ans, n;

void solve(long long x)
{
    
    
    if(x > n) return ;
    ans ++;
    solve( x * 10);
    solve( x * 10 + 1);
}

int main()
{
    
    
    scanf("%lld",&n);
    solve(1);
    printf("%lld\n",ans);
    return 0;
}

D:Train and Peter

链接:CF8A

题意:

Peter在坐火车,前后看见了两个序列,给出一个总序列表示路径,判断火车是在前进还是后退

思路:

假设s是总序列,A,B分别为Peter前后看见的序列。如果在s序列中找到了A并且在A的后面又找到了B则可以判断是在前进,然后将s序列反转,同样判断是否是后退

string str1,str2;
char a;
str1.find(str2);  //在字符串str1中查找str2,返回str2中首个字符在str1中的地址
str1.find(str2,2);  //从str1的第2个字符开始查找str2
str.find(a);//在str1中查找字符a并返回第一个查找到的地址
str1.find(str2,23)  //在str1中的第二个字符开始查找前str2的前3个字符
//没有找到返回npos即使-1;

参考代码:

int main()
{
    
    
	bool f=0,b=0;
	string s, A, B;
	cin >> s >> A >> B;
	int len = s.size();
	if(s.find(A) < len && s.find(B, s.find(A) + A.size()) < len)
		f=1;
	reverse(s.begin(),s.end());
	if(s.find(A) < len && s.find(B, s.find(A) + A.size()) < len)
		b=1;
	if(f && b) cout<<"both";
	else if(f) cout<<"forward";
	else if(b) cout<<"backward";
	else cout<<"fantasy";
	return 0;
}

E:Running Student

链接:CF9B

题目:

小明上学,在二维坐标系中,给出公交站台的个数,小明的运动速度,公交车的移动速度,以及站台的坐标(xi,0),求在那个站台下车时间最短

思路:

枚举每个站台即可,因为上车站不能下车,所以需要从第二个站台开始枚举

参考代码:

ll n, vb, vs, x, y, a[100010], ans;//这里要用ll的原因是因为后面计算距离的时候可能会爆int
double min_1 = INF, min_2 = INF;

int main()
{
    
    
	cin >> n >> vb >> vs;
	for(int i = 1; i <= n; i++ ) cin >> a[i];
	cin >> x >> y;

	for(int i = 2; i <= n; i++){
    
    

		double k = (double) a[i] / vb;
		double v= (double) sqrt( (x - a[i] ) * ( x - a[i] ) + y * y ) / vs;
		if( k + v <= min_1 && v < min_2)
		{
    
    
			min_1 = k + v;
			min_2 = v;
			ans = i;
		}
	}

	cout<<ans;

	return 0;
}

F: Registration system

链接CF4C

题意:

给出一个用户名(字符串),判断是否存在,不存在输出YES,存在输出字符串+i,i从1开始逐渐递增;

思路:

利用map容器存储

参考代码:

map<string,int> person;
int main()
{
    
    
	int n;
	cin >> n;
	while(n -- )
    {
    
    
        string s;
        cin >> s;
        if( !person[s] )
        {
    
    
            puts("OK");
            person[s]++;
        }
        else
        {
    
    
            cout << s << person[s]++ << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ecjtu2020/article/details/114093169