头像

天乔巴夏丶




离线:1天前


活动打卡代码 LeetCode 728. 自除数

class Solution {
    public List<Integer> selfDividingNumbers(int left, int right) {
        List<Integer> ans = new LinkedList<>();
        while(left <= right){
            int t = left;
            boolean flag = true;
            while(t > 0){
                int k =  t % 10;
                if(k == 0 || left % k != 0){
                    flag = false;
                    break;
                }else{
                    t /= 10;
                }
            }
            if(flag) ans.add(left);
            left ++;
        }
        return ans;
    }
}


活动打卡代码 AcWing 3257. 跳一跳

import java.util.*;

class Main{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int pre = -1;
        int ans = 0;
        int k = 2;
        while(true){
            int cur = sc.nextInt();
            if(cur == 0){
                break;
            }

            if(cur == 1) {
                ans ++;
                k = 2;   
            }
            else {
                if(pre == -1 || pre == 1) ans += 2;
                else ans = ans + k;
                k += 2;
            }
            pre = cur;
        }
        System.out.println(ans);

    }
}


活动打卡代码 AcWing 3227. 折点计数

import java.util.*;

class Main{
    static int n;

    static int[] a;

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        a = new int[n];
        for(int i = 0; i < n; i ++) a[i] = sc.nextInt();
        int ans = 0;
        for(int i = 1; i < n - 1;i ++){
            if(a[i] > a[i - 1] && a[i] > a[i + 1] || a[i] < a[i - 1] && a[i] < a[i + 1]) 
                ans ++;
        }
        System.out.println(ans);
    }
}


活动打卡代码 AcWing 441. 数字统计

import java.util.*;

class Main{
    static int L, R;

    public static void main(String[] args){
        int ans = 0;
        Scanner sc = new Scanner(System.in);
        L = sc.nextInt(); R = sc.nextInt();
        for(int i = L; i <= R; i ++){
            int t = i;
            while(t > 0){
                if(t % 10 == 2) ans ++;
                t /= 10;
            }
        }
        System.out.println(ans);
    }
}


活动打卡代码 AcWing 3232. 最大波动

import java.util.*;

class Main{
    static int n;

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        int ans = 0, pre = 0;
        for(int i = 1; i <= n; i ++){
            int cur = sc.nextInt();
            ans = Math.max(ans, Math.abs(pre - cur));
            pre = cur;
        }
        System.out.println(ans);
    }
}


活动打卡代码 AcWing 3203. 画图

#include <iostream>
using namespace std;

const int N = 110;

bool g[N][N];

int main()
{
    int n;
    cin >> n;
    while(n--)
    {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;

        for(int i = x1; i < x2; i++)
        {
            for(int j = y1; j < y2; j++)
            {
                g[i][j] = 1;
            }
        }
    }

    int res = 0;


    for(int i = 0; i < 110; i++)
    {
        for(int j = 0; j < 110; j++)
        {
            res += g[i][j];
        }
    }

    cout << res;
}



活动打卡代码 AcWing 3208. Z字形扫描

import java.util.*;

class Main{
    static int n;
    static int[][] nums;

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        nums = new int[n][n];
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < n; j ++){
                nums[i][j] = sc.nextInt();
            }
        }
        for(int k = 0, flag = 1; k < 2 * n - 1; k ++, flag ^= 1){
            if(flag == 1){
                for(int i = k; i >= 0; i --){
                    for(int j = 0; j <= k; j ++){
                        if(i + j == k && i < n && j < n) System.out.print(nums[i][j] + " ");
                    }
                }
            }else{
                for(int i = k; i >= 0; i --){
                    for(int j = 0; j <= k; j ++){
                        if(i + j == k && i < n && j < n) System.out.print(nums[j][i] + " ");
                    }
                }
            }
        }
    }
}



class Solution {
    public int countHomogenous(String s) {
        int mod = (int)(1e9 + 7);
        long ans = 0;
        for(int i = 0; i < s.length(); i ++){
            int j = i + 1;
            while(j < s.length() && s.charAt(j) == s.charAt(i)) j ++;
            long len = j - i;
            ans = (ans + len * (len + 1) / 2) % mod;
            i = j - 1;
        }
        return (int)(ans % mod);
    }
}


活动打卡代码 AcWing 1477. 拼写正确

import java.util.*;

class Main{
    static String n;
    static String[] strs = {"zero", "one","two","three","four","five","six","seven","eight","nine"};
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.next();
        int ans = 0;
        for(int i = n.length() - 1; i >= 0; i --){
            ans += (n.charAt(i) - '0');
        }
        String str = ans + "";
        StringBuilder res = new StringBuilder();
        for(int i = 0; i < str.length(); i ++){
            int idx = str.charAt(i) - '0';
            res.append(strs[idx] + " ");
        }
        System.out.println(res.toString());
    }
}



活动打卡代码 AcWing 425. 明明的随机数

import java.util.*;

class Main{

    static int N = 1010;

    static int[] cnt = new int[N];
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i = 0; i < n; i ++) {
            int a = sc.nextInt();
            if( ++cnt[a] == 2) cnt[a] = 1; 
        }
        int ans = 0;
        for(int i = 0; i < cnt.length; i ++){
            if(cnt[i] == 1) {
                ans ++;
            }
        }
        System.out.println(ans);
        for(int i = 0; i < cnt.length; i ++){
            if(cnt[i] == 1) {
                System.out.print(i + " ");
            }
        }
    }
}