Article Directory
A:Second Order Statistics
Link: CF22A
title meaning:
Given an array, output the second smallest integer (equal integers are calculated only once).
Ideas:
Reordering
Reference Code:
#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
Link: CF9A
Title:
Little Y, Little W and Little D play a dice (six-sided) game. Whoever throws the most points counts as the winner. Now that the scores of Little Y and Little W are known, please help Little D to find her probability of winning.
Note :
1. Output in "numerator/denominator", especially if it is impossible to win, output "0/1", 100% win output "1/1"
2. Little Y and Little W are very gentlemen, if the score of Little D and Like them, they will be counted as a small D winning
Ideas:
Find the maximum value of Y and M points, and then analyze it step by step
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
Link: CF9C
Title:
Input n, output 1-n natural numbers, each digit only contains the number of 0 and 1.
Ideas:
It can be found that each digit is 0, and the number of 1 is from small to large: 1,10,11,100,101,110,111···
If it can be found from 1; 1 -> 10 / 11, then 10 -> 100 / 101, 11 -> 110, 111·····
Reference Code:
#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
Link: CF8A
Title:
Peter was on a train, and he saw two sequences before and after. A total sequence was given to indicate the path and judge whether the train was moving forward or backward.
Ideas:
Suppose s is the total sequence, and A and B are the sequences seen before and after Peter. If A is found in the s sequence and B is found behind A, then it can be judged that it is advancing, and then the s sequence is reversed, and it is also judged whether it is backward
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,2,3) //在str1中的第二个字符开始查找前str2的前3个字符
//没有找到返回npos即使-1;
Reference Code:
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
Link: CF9B
topic:
Xiao Ming goes to school. In the two-dimensional coordinate system, give the number of bus stops, Xiao Ming's moving speed, the moving speed of the bus, and the coordinates of the platform (xi, 0), and find the shortest time to get off at that platform
Ideas:
Just enumerate each platform, because you can’t get off at the station, you need to start enumeration from the second platform
Reference Code:
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
Link CF4C
Title:
Given a user name (string), judge whether it exists, there is no output YES, there is an output string +i, i gradually increases from 1;
Ideas:
Use map container storage
Reference Code:
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;
}