School OJ bloody lessons

Immediately midterm exam in order not to die too miserable, decided to record it here, sand sculpture sand sculpture error of topics WA

Question one:

Determining whether a number is a prime number greater than five and not symmetrical.

#include <stdio.h>

int main(){
	int T,i,j,a;
	scanf("%d",&T);
	for(i=1;i<=T;i++){
		scanf("%d",&a);
		int flag=0;
		if(a==1){
			flag=1;
		}else{
			for(j=2;j<a;j++){
				if(a%j==0){
					flag=1;
				}	
			} 
		}  
		    if(flag==0){
		    	if(a<10&&a>1){
		    		printf("Yes\n");
				}else if(a<100&&a>=10&&(a%10==a/10)){
					printf("Yes\n");
				}else if(a<1000&&a>=100&&a/100==a%10){
					printf("Yes\n");
				}else if(a<10000&&a>=1000&&a/1000==a%10&&(a%1000)/100==(a%100)/10){
					printf("Yes\n");
				}else if(a<100000&&a>=10000&&a/10000==a%10&&(a%10000)/1000==(a%100)/10){
					printf("Yes\n");
				}else{
					printf("No\n");
				}
			}else{
				printf("No\n");
			}
		}
} 

error:

1. Variables

(Beginning with the array) later found there is no need to use too much trouble, the direct use of i represents the number of cycles, and then find a number of input variables in place just fine.

2. Variable Initialization

as a variable flag which he did not re-initialized cycle, if the first number is not a prime number, then back into the input number will be considered flag is equal to 1, will not enter the loop.

3. equal sign

Or just starting, ah, an equal sign and two equal sign, but not being given the wrong dev answer is wrong ah.

4. Output Format

You must be sure to remember the blank line ah.

Second problem: the strongest letters

The strongest character in the output string of letters.

#include<stdio.h>

int main(){
	char ch,max;
	scanf("%c",&ch);
	max=getchar();
	while((ch = getchar()) != '\n'){
		if(ch < max)
		max = ch;
	}
	printf("%c",max);
} 

error

1. did not know the getchar usage, it is inexcusable.

Question three: the number of troubled finished within a long time seeking N

End number is defined: and factor equal to the number itself (such as: 6 = 1 + 2 + 3)

Now toss for so long must be first on an error code more violent -
#include<stdio.h>

int main(){
	int N,i,j,k=0,sum,l;
	int a[100];
	scanf("%d",&N);
	while(N>=6)
	{
	    for(i=6;i<N;i++)
	    {
		    for(j=1;j<N;j++)
			{
			   if(i%j==0)
			   {
			   	a[k]=j;
			   	k++;
			   	sum=sum+j;
			   }	
			}
			if(sum==i)
			{
				printf("%d its factors are ",i);
			}
			for(l=0;l<k;l++)
			{
				printf("%d ",a[l]);
			 } 
	    }
			
	}
}

First of all, we have to product a product that fool (not me!) Want to do -
the What? N> = 6
so classmates, N <6 were you eaten yet?
O turned out that he felt would not have to meet the conditions of a number of so skip when N <6
but --while embedded in two for loops? ? ?
What is this doing? ? ?
Oh, you want to exclude smaller than 6, if it is not used on the line yet? Shaa wanted to do with while little brother. . .
Well, this time we can get rid of while (because you did not find this stuff plus eggs will increase with time complexity).
Obviously, the first for loop is used to take a 1 to all of the number N, the second for loop is used to determine factors.

for(i=1;i<N;i++)
	    {
		    for(j=1;j<N;j++)
		    //来来,康康这个循环,它里面判断循环结束的条件竟然是——j<N
		    //这会出现什么问题呢?
			{
			   if(i%j==0)
			   {
			   	a[k]=j;
			   	k++;
			   	sum=sum+j;
			   }	
			}

Obviously, the above loop condition called death conditions throughout the program, which will bring what kind of impact?
Determining if the following conditions are sum == i, if by this procedure, sum further comprises that the number itself,
SUM! = I, so the procedure is wrong.
Most Finally, the subject of the request output after a set number of line breaks, this did not reflect in the program.

summary of the issue

1. loop nest should pay attention to the role of each cycle, the cycle does not make sense not to do. (Analyzing and distinguish cycle ah ah)
2. The cycle determines the conditions, many times may not necessarily be the most primitive data to the end of the cycle.
3. Output line feed.

The following is the proper code

#include<stdio.h> 
#include<string.h>

int main(){
	int n,i,j,k,l,sum;
	int a[1000];
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		sum=0;
		k=0;
		for(j=1;j<i;j++){
			if(i%j==0){
				sum=sum+j;
				a[k]=j;
				k++;
			}
		}
		if(sum==i){
			printf("%d its factors are ",i);
			for(l=0;l<k;l++){
				printf("%d ",a[l]);
			}
			printf("\n");
		}
	} 
}
1. array initialization
2. seek time factor, the j <i wrong written j <n

Error induction before midterm exam

Look at the subject in claim 1. When the output format, after the last data if there is space. And input and output data format corresponding to the type. scanf address is taken.
2. collision detection, reverse consider the issue.
3. sensitive.
4.
Released three original articles · won praise 0 · Views 156

Guess you like

Origin blog.csdn.net/LucyHolmes/article/details/102938679
Recommended