PAT-BASIC1012——数字分类

我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC

原题链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805311146147840

题目描述:

知识点:数学

思路:先分类,再计算

本题有一个坑点:

如果被5除后余2的数字的个数是0,不能输出0,需要输出N。

时间复杂度和空间复杂度均为O(n),其中n为输入的数字个数。

C++代码:

#include<iostream>
#include<vector>

using namespace std;

int main() {
	int count;
	cin >> count;
	int temp;
	vector<int> nums;

	for (int i = 0; i < count; i++) {
		cin >> temp;
		nums.push_back(temp);
	}

	vector<int> a1;
	vector<int> a2;
	vector<int> a3;
	vector<int> a4;
	vector<int> a5;


	for (int i = 0; i < nums.size(); i++) {
		if (nums[i] % 2 == 0 && nums[i] % 5 == 0) {
			a1.push_back(nums[i]);
		}
		if (nums[i] % 5 == 1) {
			a2.push_back(nums[i]);
		}
		if (nums[i] % 5 == 2) {
			a3.push_back(nums[i]);
		}
		if (nums[i] % 5 == 3) {
			a4.push_back(nums[i]);
		}
		if (nums[i] % 5 == 4) {
			a5.push_back(nums[i]);
		}
	}

	if (a1.size() == 0) {
		cout << "N ";
	} else {
		int sum = 0;
		for (int i = 0; i < a1.size(); i++) {
			sum += a1[i];
		}
		cout << sum << " ";
	}

	if (a2.size() == 0) {
		cout << "N ";
	} else {
		int sum = 0;
		for (int i = 0; i < a2.size(); i++) {
			if (i % 2 == 0) {
				sum += a2[i];
			} else {
				sum -= a2[i];
			}
		}
		cout << sum << " ";
	}

	if (a3.size() == 0) {
		cout << "N ";
	} else {
		int sum = a3.size();
		cout << sum << " ";
	}

	if (a4.size() == 0) {
		cout << "N ";
	} else {
		int sum = 0;
		for (int i = 0; i < a4.size(); i++) {
			sum += a4[i];
		}
		double average = 1.0 * sum / a4.size();
		printf("%.1f ", average);
	}

	if (a5.size() == 0) {
		cout << "N";
	} else {
		int max = a5[0];
		for (int i = 1; i < a5.size(); i++) {
			if (a5[i] > max) {
				max = a5[i];
			}
		}
		cout << max;
	}
}

C++解题报告:

JAVA代码:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] strings = scanner.nextLine().split("\\s+");
        int[] nums = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            nums[i] = Integer.parseInt(strings[i]);
        }
        ArrayList<Integer> arrayList1 = conditionA1(nums);
        if(arrayList1.size() == 0){
            System.out.print("N ");
        }else{
            int sum = 0;
            for (int i = 0; i < arrayList1.size(); i++) {
                sum += arrayList1.get(i);
            }
            System.out.print(sum + " ");
        }
        ArrayList<Integer> arrayList2 = conditionA2(nums);
        if(arrayList2.size() == 0){
            System.out.print("N ");
        }else{
            int sum = 0;
            for (int i = 0; i < arrayList2.size(); i++) {
                if(i % 2 == 0){
                    sum += arrayList2.get(i);
                }else{
                    sum -= arrayList2.get(i);
                }
            }
            System.out.print(sum + " ");
        }
        ArrayList<Integer> arrayList3 = conditionA3(nums);
        if(arrayList3.size() == 0){
            System.out.print("N ");
        }else {
            System.out.print(arrayList3.size() + " ");
        }
        ArrayList<Integer> arrayList4 = conditionA4(nums);
        if(arrayList4.size() == 0){
            System.out.print("N ");
        }else{
            int sum = 0;
            for (int i = 0; i < arrayList4.size(); i++) {
                sum += arrayList4.get(i);
            }
            double average = 1.0 * sum / arrayList4.size();
            System.out.printf("%.1f ", average);
        }
        ArrayList<Integer> arrayList5 = conditionA5(nums);
        if(arrayList5.size() == 0){
            System.out.print("N");
        }else{
            int max = arrayList5.get(0);
            for (int i = 1; i < arrayList5.size(); i++) {
                if(arrayList5.get(i) > max){
                    max = arrayList5.get(i);
                }
            }
            System.out.print(max);
        }
    }

    private static ArrayList<Integer> conditionA1(int[] nums){
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 1; i < nums.length; i++) {
            if(nums[i] % 2 == 0 && nums[i] % 5 == 0){
                arrayList.add(nums[i]);
            }
        }
        return arrayList;
    }

    private static ArrayList<Integer> conditionA2(int[] nums){
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 1; i < nums.length; i++) {
            if(nums[i] % 5 == 1){
                arrayList.add(nums[i]);
            }
        }
        return arrayList;
    }

    private static ArrayList<Integer> conditionA3(int[] nums){
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 1; i < nums.length; i++) {
            if(nums[i] % 5 == 2){
                arrayList.add(nums[i]);
            }
        }
        return arrayList;
    }

    private static ArrayList<Integer> conditionA4(int[] nums){
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 1; i < nums.length; i++) {
            if(nums[i] % 5 == 3){
                arrayList.add(nums[i]);
            }
        }
        return arrayList;
    }

    private static ArrayList<Integer> conditionA5(int[] nums){
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 1; i < nums.length; i++) {
            if(nums[i] % 5 == 4){
                arrayList.add(nums[i]);
            }
        }
        return arrayList;
    }
}

JAVA解题报告:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值