这道题虽然我想到了和结构体,但没见过比较那么多的,一时间绷住了qwq(基础果然还要多练练)
- #include
- using namespace std;
- int n;
- struct stu{
- int cn, en, ma;
- int num;
- int sum;
- }st[310];
- bool cmp(stu a, stu b){
- if(a.sum > b.sum) return 1;
- else if(a.sum == b.sum && a.cn > b.cn) return 1;
- else if(a.sum == b.sum && a.cn == b.cn && a.num < b.num) return 1;
- else return 0;
- }
- int main(){
- cin >> n;
- for(int i = 1; i <= n; i ++ ){
- cin >> st[i].cn >> st[i].ma >> st[i].en;
- st[i].num = i;
- st[i].sum = st[i].cn + st[i].ma + st[i].en;
- }
- sort(st + 1, st + 1 + n, cmp);
- for(int i = 1; i <= 5; i ++ ){
- cout << st[i].num << " " << st[i].sum << endl;
- }
- return 0;
- }
- #include
- using namespace std;
- int n;
- string s[25];
- bool cmp(const string &a, const string &b){
- return a + b > b + a;
- }
- int main(){
- cin >> n;
- for(int i = 0; i < n; i ++ ) cin >> s[i];
- sort(s, s + n, cmp);
- for(int i = 0; i < n; i ++ ) cout << s[i];
- return 0;
- }
woc这道题掉坑里了,题目求的是第k小的数不是排好序后第k个数
按样例来分析,已知最小的数是第0小,如果求的是第1小的数, 【1 2 3 4 5】数列中第1小的数应该是2,第k个数应该是1, 所以在输入k后要自加1
- #include
- using namespace std;
- const int N = 5e6 + 10;
- int a[N];
- int n, k;
- int quick_select(int a[], int l, int r, int k){
- if(l >= r) return a[l];
- int i = l - 1, j = r + 1, x = a[l + r >> 1];
- while(i < j){
- do i ++ ; while(a[i] < x);
- do j -- ; while(a[j] > x);
- if(i < j) swap(a[i], a[j]);
- }
- if(k <= j - l + 1) quick_select(a, l, j, k);
- else quick_select(a, j + 1, r, k - (j - l + 1));
- }
- int main(){
- ios::sync_with_stdio(false);
- cin.tie(0), cout.tie(0);
- cin >> n >> k;
- k ++ ;
- for(int i = 0; i < n; i ++ ) cin >> a[i];
- cout << quick_select(a, 0, n - 1, k)<< endl;
- return 0;
- }
- #include
- using namespace std;
- const int N = 5010;
- int n, m;
- struct stu{
- int num, score;
- }st[N];
- bool cmp(stu a, stu b){
- if(a.score > b.score) return true;
- else if(a.score == b.score && a.num < b.num) return true;
- else return false;
- }
- int main(){
- cin >> n >> m;
- for(int i = 1; i <= n; i ++ ) cin >> st[i].num >> st[i].score;
- sort(st + 1, st + 1 + n, cmp);
- m = m * 1.5;
- int level = st[m].score;
- int cnt = m;
- for(int i = m + 1; i <= n && st[i].score == level; i ++ ){
- cnt ++ ;
- }
- cout << level << " " << cnt << endl;
- for(int i = 1; i <= cnt; i ++ ){
- cout << st[i].num << " " << st[i].score << endl;
- }
- return 0;
- }
这题a.sum.size() 的做法好骚啊 难绷
- #include
- using namespace std;
- int n;
- struct stu{
- int num;
- string sum;
- }s[25];
- bool cmp(stu a, stu b){
- if(a.sum.size() > b.sum.size()) return true;
- else if(a.sum.size() == b.sum.size() && a.sum > b.sum) return true;
- else return false;
- }
- int main(){
- cin >> n;
- for(int i = 1; i <= n; i ++ ){
- cin >> s[i].sum;
- s[i].num = i;
- }
- sort(s + 1, s + 1 + n, cmp);
- cout << s[1].num << endl;
- cout << s[1].sum << endl;
- return 0;
- }
get新知识点wwwwwww
- #include
- using namespace std;
- int n, ans;
- int main(){
- cin >> n;
- for(int i = 0; i < 3; i ++ ){
- int cnt, price;
- cin >> cnt >> price;
- int c = cnt, p = price;
- while(cnt < n){
- cnt <<= 1;
- price <<= 1;
- }
- while(cnt > n){
- cnt -= c;
- price -= p;
- }
- while(cnt < n){
- cnt += c;
- price += p;
- }
- if(price < ans || ans == 0) ans = price;
- }
- cout << ans << endl;
- return 0;
- }
这道题死在了stack加数据原来是push 不是push_back orz
stack的使用
empty() 堆栈为空则返回真
pop() 移除栈顶元素 (删除)
push() 在栈顶增加元素 (增加)
size() 返回栈中元素数目
top() 返回栈顶元素,不删除(获取)
- #include
- using namespace std;
- stack<int> a;
- int main(){
- int x;
- while(cin >> x){
- if(x == 0) break;
- a.push(x);
- }
- while(!a.empty()){
- cout << a.top() << " ";
- a.pop();
- }
- return 0;
- }
这道题用一开始用字符串有一个wa了,因为字符串和数组的 .size()的数值不一样,和strlen()长度弄混了就寄了
这张网图可以很清楚的表现出二者的区别,wwwww我的破基础orz
- #include
- using namespace std;
- string s;
- int sum;
- char c;
- int main(){
- getline(cin, s);
- for(int i = 0, j = 1; i < s.size() - 2; i ++ ){
- if(isdigit(s[i])){
- sum += (s[i] - '0') * j;
- j ++ ;
- }
- }
- sum %= 11;
- int t = s[12];
- s[12] = sum == 10 ? 'X' : sum + '0';
- if(t == s[12]){
- cout << "Right";
- }
- else{
- for(int i = 0; i < s.size(); i ++ ) cout << s[i];
- }
- return 0;
- }
- #include
- #define INF 0x3f3f3f3f
- using namespace std;
- char s[13];
- int sum;
- char c;
- int main(){
-
- for(int i = 0, j = 1; i < 13; i ++ ){
- cin >> s[i];
- if(isdigit(s[i]) && i != 12){
- sum += (s[i] - '0') * j;
- j ++ ;
- }
- }
- sum %= 11;
- int t = s[12];
- s[12] = sum == 10 ? 'X' : sum + '0';
- if(t == s[12]){
- cout << "Right";
- }
- else{
- for(int i = 0; i < 13; i ++ ) cout << s[i];
- }
- return 0;
- }
- #include
- #define INF 0x3f3f3f3f
- using namespace std;
- const int N = 1e3 + 10;
- int a[N][N]; //标记的数组
- int n, m, k, x;
- int main(){
- cin >> n >> m >> k;
- for(int i = 1; i <= n; i ++ ){
- for(int j = 1; j <= m; j ++ ){
- cin >> x;
- a[x][j] = 1;//输入第i个人在第x天打了第j次卡,并做标记
- }
- }
- for(int i = 1; i <= k; i ++ ){//一共k次输出
- for(int j = 1; j <= m; j ++ ){//从1~最大打卡天数进行枚举,看第i场有多少人打卡
- a[i][j] += a[i][j - 1];
- }
- cout << a[i][m] << " "; //输出部分和的总量,即第1~k天分别对应的模拟赛场次
- }
- return 0;
- }
一开始wa的亚UN隐式1两个整数相除有可能有小数点,需要用浮点型,所以干脆整型设为double(?应该是这个原因吧因为改了就ac了)
- #include
- #define INF 0x3f3f3f3f
- #define PI 3.14159
- using namespace std;
- typedef long long LL;
- double s, v;
- int main(){
- cin >> s >> v;
- int tm = ceil(s / v) + 10;
- int n = (8 + 24) * 60;
- n -= tm;
- if(n >= 24 * 60) n -= 24 * 60;
- int hh = n / 60;
- int mm = n % 60;
- printf("%02d:%02d", hh, mm);
- return 0;
- }
为什么打表放在主函数外会re啊?QVQ
- #include
- #define INF 0x3f3f3f3f
- using namespace std;
- typedef long long LL;
- string s;
- int cnt;
- int t[50] = {0};
-
- int main(){
- string key[12] = {
- "", "abc", "def",
- "ghi", "jkl", "mno",
- "pqrs", "tuv", "wxyz",
- "*", " ", "#"
- };
- for(int i = 0; i < 12; i ++ ){
- for(int j = 0; j < key[i].size(); j ++ ){
- t[key[i][j]] = j + 1;
- }
- }
- getline(cin, s);
- for(int i = 0; i < s.size(); i ++ ){
- cnt += t[s[i]];
- }
- cout << cnt << endl;
- return 0;
- }
难绷,这道题写快了没考虑到 s[i] == 'z' 的情况呜呜呜呜QAQ
- #include
- #define INF 0x3f3f3f3f
- using namespace std;
- typedef long long LL;
- string s;
- int main(){
- int n;
- cin >> n >> s;
- for(int i = 0; i < s.size(); i ++ ){
- for(int j = 1; j <= n; j ++ ){
- s[i] ++ ;
- if(s[i] > 'z') s[i] = 'a';
- }
-
- }
- cout << s << endl;
- return 0;
- }
一开始数组开太大了爆了呜呜呜
美美MLE
- #include
- #define INF 0x3f3f3f3f
- using namespace std;
- typedef long long LL;
- const int N = 1e5 + 10;
- int a[N], b[N], g[N], k[N];
- int ans;
- int main(){
- int n, x, y;
- cin >> n;
- for(int i = 1; i <= n; i ++ ) cin >> a[i] >> b[i] >> g[i] >> k[i];
- cin >> x >> y;
- for(int i = 1; i <= n; i ++ ){
- if(x >= a[i] && x <= a[i] + g[i] && y >= b[i] && y <= b[i] + k[i]){
- ans = i;
- }
- }
- cout << ans << endl;
- return 0;
- }
- #include
- using namespace std;
- const int N = 110;
- int a[N];
- int main(){
- int n;
- cin >> n;
- for(int i = 1; i <= n + 1; i ++ ) cin >> a[i];
- if(n == 0){
- cout << a[1];
- return 0;
- }
-
- if(abs(a[1]) != 1) cout << a[1] << "x^" << n;
- else{
- if(a[1] == 1) cout << "x^" << n;
- else if(a[1] == -1) cout << "-x^" << n;
- }
-
- int num = 2;
- for(int i = n - 1; i >= 1; i -- ){
- if(i != 1){
- if(a[num] > 0){
- if(a[num] == 1) cout << "+x^" << i;
- else cout << "+" << a[num] << "x^" << i;
- }
- else if(a[num] < 0){
- if(a[num] == -1) cout << "-x^" << i;
- else cout << a[num] << "x^" << i;
- }
- }
- else{
- if(a[num] > 0){
- if(a[num] == 1) cout << "+x";
- else cout << "+" << a[num] << "x";
- }
- else if(a[num] < 0){
- if(a[num] == -1) cout << "-x";
- else cout << a[num] << "x";
- }
- }
- num ++ ;
- }
- if(a[num] > 0) cout << "+" << a[num];
- else if(a[num] < 0) cout << a[num];
- return 0;
- }
- #include
- #define INF 0x3f3f3f3f
- using namespace std;
- typedef long long LL;
- const int N = 1e5 + 10;
- int f[N], a[N];
- int main(){
- int n;
- cin >> n;
- for(int i = 1; i <= n; i ++ ){
- cin >> a[i];
- f[1] = a[1];
- if(i != 1){
- if(a[i] <= a[i - 1]) f[i] = f[i - 1];
- else f[i] = f[i - 1] + a[i] - a[i - 1];
- }
- }
- cout << f[n] << endl;
- return 0;
- }