练习
作者:
Tie
,
2019-09-20 13:28:53
,
所有人可见
,
阅读 751
a small hedge fund
- 题目
Given weights of N lions, find their relativate ranks and the lions with the top three largest weights, which will be termed: “Alpha”, “Beta”,”Gamma”.
- Example:
Input: [90,85,80,75,70]
Output: [“Alpha” ,”Beta”, “Gamma”, “4”,”5”]
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
const int N = 100010;
int n;
int lions[N];
string index[N]; //i的index
int main()
{
vector<int> original;
cin >> n;
for (int i = 0; i < n; i ++ )
{
cin >> lions[i];
original.push_back(lions[i]);
}
sort(lions, lions + n);
reverse(lions, lions + n);
for (int i = 0; i < n; i ++ )
{
if (lions[0] == original[i]) index[i] = "Alpha";
if (lions[1] == original[i]) index[i] = "Beta";
if (lions[2] == original[i]) index[i] = "Gamma";
for (int j = 3; j < n; j ++ )
if (lions[j] == original[i]) index[i] = to_string(j + 1);
}
for (int i = 0; i < n; i ++ )
{
cout << index[i] << " ";
}
return 0;
}
- 题目
Given an array nums, write a function to move all 0’s to the end of it while maintainning the relative order of the non-zero elements
- Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
#include <iostream>
#include <vector>
using namespace std;
const int N = 100010;
int n;
int a[N];
int q[N];
int hh = 0, tt = n - hh - 1;
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> a[i];
for (int i = 0; i < n; i ++ )
{
if (a[i] == 0) q[tt -- ] = a[i];
if (a[i] != 0) q[hh ++ ] = a[i];
}
for (int i = 0; i < n; i ++ ) cout << q[i] << " ";
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
vector<char> g;
string line;
int main()
{
while(getline(cin, line))
{
for (int i = 0; i < line.size(); i ++ )
for (int j = 0; j < i + 1; j ++ )
g.push_back(line[j]);
}
for (int i = 0; i < g.size(); i ++ ) cout << g[i];
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int n;
int a[N];
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> a[i];
reverse(a + 1, a + n - 1);
for (int i = 0; i < n; i ++ ) cout << a[i] << " ";
return 0;
}