Summary of the habit of doing questions

I have blood lessons for each of the following (this article is updated every year, one plus one, welcome to exchange)

  1. Write your ideas in the form of comments before doing the question
    • This should be viewed together with the second point. A question can be divided into input data, processing data, and output data no matter what.
    • That is to say, at least three lines of comments can be written, and then the program segment in the processing data can be taken out.
    • When typing the code at the end, you can type a blank line when completing each step above
    • This is the same as the high school math problem that needs to be reviewed first. I am also used to writing important conditions on paper with a pen (actually because there is no second screen), so you are equivalent to running the program in your mind, you can Enjoy silky smoothness
  2. block check
    • For example, in Example 1 below, we can see that we need to complete the bucket row, find the largest and smallest, and check the prime numbers. At a rough glance, there are three program segments. When completing each program segment, a printf statement should be added for verification. Of course, functions can also be used, but in any case, the program segment must be verified.
    • There are many benefits, the most obvious is that this is much better than when you type it all and report an error, and then watch a big pile of code and can't start, break down the code and check it one by one, which can minimize errors.
  3. Variables are separated from intermediate variables
    • This is a little habit of my own, that is, for example, writing two lines of int, the first line puts the actual data used in the title, and the second line puts intermediate variables, such as i, j and other code that is not practically useful when looping
    • I like this personal habit because I can see it separately and make it clearer, and I don't like giving meaningful names to intermediate variables, so I put them separately.
  4. Know your name
    • This first follows the principle of priority of question setting. What variable name appears on the title will be used.
    • After that, we call the intermediate variables that do not appear in the title, such as loops, flags, etc. The names are i, j, flag, etc.
  5. There is one set of intermediate variables, and the impact is resolutely avoided
    • Important things are said three times, there is one set, one set, and one set
    • That is to say, use i1, j1 to continue like this
    • Intermediary variables can be completely avoided, which is very common, so be careful
    • If you are not happy, you can check this when the program has an infinite loop, nine out of ten
  6. Use macro definitions
    • Use macro definitions when defining array size
    • This can help you when you don't see the question clearly at first and the value is too small
  7. In the face of complex input data, try more data
    • The main thing is to take into account the special circumstances

put a sample code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main (void)
{
	//Enter n, representing n groups of test data
	// Sort by bucket, put letters into bucket (0~25 is a~z)
	//find maxn, minn
	//Check if cha is a prime number
	// output
	int n, maxn, minn, cha;
	int i,j,i1,j1,flag;
	int tong[26];
	char danci [30];
	scanf ("%d",&n);
	for (i1=0;i1<n;i1++) {
		memset(tong,0,sizeof(tong));//Clear bucket data
		maxn = 0;
		minn=105;
		flag=0;
		/*for (j=0;j<26;j++) {
			printf ("%d ",tong[j]);
		}*/
		scanf ("%s",danci);
		getchar();
		for (j=0;j<strlen(danci);j++) {//sizeof counts the space, strlen counts the actual number of bytes
			tong[danci[j]-'a']++;
		}
		/*for (j=0;j<26;j++) {
			printf ("%d ",tong[j]);
		}*/
		for(i=0;i<26;i++) {
			if (tong[i]!=0) {//0 means that there is no need to consider this word
				if (maxn<tong[i]) {
					maxn=tong[i];
				}
				if (minn>tong[i]) {
					minn = tong [i];
				}
			}
		}
		cha = maxn-minn;
		//printf ("%d %d %d",cha,maxn,minn);
		if (cha==0||cha==1) {
			;
		} else {
			for (j1=2;j1<cha+1;j1++) {
				if (j1==cha) {
					flag=1;
					break;
				}
				if (cha%j1==0) {
					break;
				}
			}
		}
		if (flag==0) {
			printf ("No Answer\n0\n");
		} else {
			printf ("Lucky Word\n%d\n",cha);
		}
	}  
}

short summary

  • These seem very cumbersome and seem to reduce your efficiency, but in fact, it will improve your efficiency. If you can calm down and do each question, whether it is a difficult or simple question, you can do it step by step. Instead, it will speed up your speed. In short, everything is about improving efficiency and reducing errors.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324612721&siteId=291194637