CCF uninitialized variable 01

Question background
: An uninitialized variable may contain any value. Therefore, using an uninitialized variable directly, such as assigning it to another variable, does not comply with general programming logic. This situation occurs in the code, often because the initialization statement is omitted or the variable name is typed incorrectly. Checking the statements that use uninitialized variables in the code can easily identify some hidden bugs in the code.

Problem Description
Consider a simple piece of code containing assignment statements. This section of code uses at most variables, which are denoted as . The constants used by this section of code are denoted as .

Article () assignment statement is, satisfies,, indicating that the value of is assigned to the variable. The lvalue, which is called the assignment statement, must be a variable; the rvalue, which is called the rvalue, can be a constant or a variable.

For any assignment statement, if the rvalue is a variable, it should have been initialized before.
Specifically, if the variable appears as an lvalue in the previous assignment statement, it is satisfied (there is no need to consider whether the first assignment statement itself also has the problem of uninitialized rvalues), and we consider that the variable has been used in the first assignment statement. is initialized;
otherwise, we think that the statement has an uninitialized rvalue problem.

According to the above rules, try to count how many assignment statements have uninitialized rvalues ​​in the given code.

Input format
The first line of input contains two positive integers separated by spaces, which respectively represent the number of variables and the number of assignment statements.

Next, enter the line, where line () contains two positive integers separated by spaces, indicating the assignment statement.

Output format:
Output an integer, indicating the number of assignment statements with uninitialized rvalue problems.

Sample input

public class Main {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int n=0;
		int k=0;
		int count=0;
		int x,y;
		Scanner in=new Scanner(System.in);
		n=in.nextInt();
		k=in.nextInt();
		boolean a[]=new boolean[n+1]; 
		a[0]=true;
		for(int i=0;i<k;i++)
		{
    
    
			x=in.nextInt();
			y=in.nextInt();
			if(a[y]==false)
			{
    
    
				count++;
				a[x]=true;
			}
			else if(a[y]==true)
			{
    
    
				a[x]=a[y];
			}
		}
		System.out.println(count);
	}
	
}

Guess you like

Origin blog.csdn.net/X131644/article/details/125154486