The Twelfth Blue Bridge Cup Simulation Tournament (Phase 3): Question 1

The Twelfth Blue Bridge Cup Simulation Tournament (Phase 3): Question 1

Problem Description:

From 1 to 2020, how many numbers are relatively prime to 2020, that is, how many numbers have the greatest common divisor of 2020 as 1.

Answer submission:

This is a fill-in-the-blank question, you only need to calculate the result and submit it. The result of this question is an integer. When submitting the answer, only fill in this integer. If you fill in the extra content, you will not be able to score.

Code:

method one:

public class Main {
    
    
	public static void main(String[] args) {
    
    
	int count=2020;//统计与2020互质的数
		int count2 = 0;//统计与2020互质的数
		int count1[] = new int[11];
		int index = -1;
		//找出2020的全部因数,除了1
		for(int i=2;i<=2020;i++) {
    
    
			if(2020%i==0) {
    
    
				index++;
				count1[index] = i;
				System.out.println(i);
			}
		}
		for(int i=1;i<=2020;i++) {
    
    
			for(int j=0;j<count1.length;j++) {
    
    
				//如果一个数可以整除2020的因数,则他必然与2020不互质
				//1——2020共2020个数不满足count--
				if(i%count1[j]==0) {
    
    
					count--;
					break;
				}
			}
		}
		System.out.println(count);
	}
}

Method Two:

public class Main {
    
    
	public static void main(String[] args) {
    
    
		int count=2020;//统计与2020互质的数
		int count2 = 0;//统计与2020互质的数
		int count1[] = new int[11];
		int index = -1;
		//找出2020的全部因数,除了1
		for(int i=2;i<=2020;i++) {
    
    
			if(2020%i==0) {
    
    
				index++;
				count1[index] = i;
				System.out.println(i);
			}
		}
		for(int i=1;i<=2020;i++) {
    
    
			if(check(i,count1)) {
    
    
				count2++;
			}
		}
		System.out.println(count2);
	}
	public static boolean check(int i, int[] num) {
    
    
		for(int j=0;j<num.length;j++) {
    
    
			//一个数i能整除2020的任意一个因数,返回false
			if(i%num[j]==0) {
    
    
				return false;
			}
		}
		//i不能整除2020的全部因数,返回true
		return true;
	}
}

Nishiuchi ~~

This is the end of today's study. Due to my limited ability and knowledge, if there is something wrong with the writing, please criticize and correct me. If you don’t understand anything, please leave me a message. If you want to continue to learn and improve, please pay attention to me and make a little progress every day. This is the beginning of the lead. Come on. If you think this article is helpful to you, welcome to forward, comment, and like! ! !

Guess you like

Origin blog.csdn.net/qq_45004455/article/details/115036023