头像

Java不是瓜哇




离线:16小时前


最近来访(1)
用户头像
C++大蒟蒻

活动打卡代码 AcWing 815. 打印字符串

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

void print(char str[]) {
    for(int i = 0; str[i]; i++) cout << str[i];
}

int main()
{
    char str[101];
    cin.getline(str, 101);

    print(str);
}


活动打卡代码 AcWing 804. n 的阶乘

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int fact(int n) {
    if(n <= 1) return 1;

    return n * fact(n -1);
}

int main()
{
    int n;
    cin >> n;

    cout << fact(n) << endl;
    return 0;
}





#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    string s;
    getline(cin, s);
    int count = 0;

    for(auto c : s) {
        if(c <= '9' && c >= '0') count++;
    }

    cout << count << endl;
}


活动打卡代码 AcWing 760. 字符串长度

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    string s;
    int len = 0;

    getline(cin, s);

    for(int i = 0; s[i]; i++) len++;

    cout << len << endl;
}



Java题解(哈希表)

填入对应大小分组,填满就加入到答案列表里面

class Solution {
    public List<List<Integer>> groupThePeople(int[] groupSizes) {
        Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        for (int i = 0; i < groupSizes.length; i++) {
            if(!map.containsKey(groupSizes[i])) {
                map.put(groupSizes[i],new ArrayList<Integer>());
            }
            map.get(groupSizes[i]).add(i);
            if(map.get(groupSizes[i]).size() == groupSizes[i]) {
                list.add(map.get(groupSizes[i]));
                map.remove(groupSizes[i]);
            }
        }
        return list;
    }
}



#include <iostream>
#include <cmath>


using namespace std;

bool is_prime(int n) {
    if(n < 2) return 0;
    for (int i = 2; i <= sqrt(n); i++) {
        if(n % i == 0 ) 
           return 0;
    }
     return 1;
}

int main()
{
    int n;

    while(cin >> n) {
        bool flag = 0;
        for(int i = 1; i < n; i = (i / 10 + 1) * 10 + 1) {
            if(is_prime(i)) {
                cout << i << " ";
                flag = 1;
            }
        }
        if(!flag) cout << -1 ;
        cout << "\n";
    }
    return 0;
}




#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int n;
    while (cin >> n) {
        bool flag = false;
        for (int k = 2; k <= sqrt(n); k ++ ) {
            if(n % (k * k) == 0) {
                flag = true;
                cout << "Yes" << endl;
                break;
            }
        }
        if(!flag) cout << "No" << endl;
    }
}




题目链接 https://www.acwing.com/activity/content/problem/content/7343/
请问这代码怎么改才不超时,求大佬解答
今天周赛这题 4504. 字符串消除

错误的代码:


import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int count = 0;
        StringBuffer str = new StringBuffer(s);
        for (int i = 0; i < str.length() - 1; i++) {
            if(str.charAt(i) == str.charAt(i + 1)) {
               str.delete(i, i + 2);
               count++;
               i = -1;
            }
        }
        if(count % 2 == 1){
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

Time Limit Exceeded



活动打卡代码 AcWing 4504. 字符串消除

Java题解(栈模拟)


import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int count = 0;
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if(stack.empty() || stack.peek() != s.charAt(i)) {
                stack.push(s.charAt(i));
            } else {
                stack.pop();
                count++;
            }
        }
        if(count % 2 == 1){
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}



Java题解(写的自己都看不懂)

import java.util.Scanner;
public class Main {
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int tmp = 1; tmp <= t; tmp++) {
            int count = 0;
            int n = sc.nextInt();
            int m = sc.nextInt();
            int[] arr = new int[200000];
            for (int i = 1; i <= n; i++) {
                arr[i] = sc.nextInt();
            }
            for (int i = n; i >= 0; i--) {
                if(arr[i] == 1) {
                    int j = i-1;
                    while(j >= 0 && arr[j] - 1 == arr[j + 1]) j--;
                    if(i - j >= m) {
                        count++;
                    }
                    i = j + 1;
                }
            }
            System.out.println("Case #" + tmp  + ": " + count);
        }
    }
}