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

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

题目描述:

PAT-BASIC1087——有多少不同的值

知识点:标记数组

思路:开一个大小为15000的数组来记录那些数字出现过

时间复杂度是O(N)。空间复杂度是O(15000)。

C++代码:

#include<iostream>

using namespace std;

int main(){
	int N;
	scanf("%d", &N);
	
	int flags[15000];
	for(int i = 0; i < 15000; i++){
		flags[i] = 0;
	}
	
	int count = 0;
	int result;
	for(int i = 1; i <= N; i++){
		result = i / 2 + i / 3 + i / 5;
		if(flags[result] == 0){
			count++;
			flags[result]++;
		}
	}
	
	printf("%d", count);
	
	return 0;
}

C++解题报告:

PAT-BASIC1087——有多少不同的值

 

相关文章: