Master list:
- for while do..while 循环
- Counter and accumulator
- The method of debugging intermediate results output
- Timing functions with efficiency test program
- Redirection methods used to read and write files
- Reading and writing files with fopen
- Construction of the local operating environment with conditions pragma
- -Wall compiler option with more warnings
A, for circulation
Program. 1: for loop output
#include<stdio.h> int main(){ int n; scanf("%d",&n); for(int i = 1;i <= n;i++) printf("%d\n",i); return 0; }
Details: In the loop variable i is defined, and therefore not visible in vitro cycle i
# Suggestions proposed to shorten the range of the variables defined ------- e.g., initialization portion in a for loop, the loop variable is defined
Procedure 2: 4 perfect square outputs of all forms of aabb
#include <stdio.h> #include <math.h> // AABB 4 digits, so the range a --1-9, range b --0-9 int main () { for ( int a = . 1 ; a <= . 9 ; a ++ ) { for ( int B = 0 ; B <= . 9 ; B ++ ) { // start determination, aabb is not the number of square int n-= a * 1100 is + B * . 11 ; // with a memory variable m sqrt (n) after the rounded integer, then it is determined whether m is equal to m * n- // Floor (x) function returns the largest integer not exceeding x int m = Floor (sqrt (n) + 0.5); if(m*m == n) printf("%d\n",n); } }
return 0; }
Results: 7744
Floor (sqrt (n-) + 0.5 ) represents rounding
# Floating point computation errors may exist, it is assumed after a lot of calculation, due to the influence of the error, becomes an integer of 1 0.99999999, floor of the result will be 0 instead of 1, in order to reduce the influence of the error is generally rounded to , i.e., floor (x + 0.5)
# Floating point error may exist, performing floating-point comparison, floating point error should be taken into
Another approach is to enumerate the square root of x, so as to avoid square root operations
#include<stdio.h> int main(){ for(int x = 1; ; x++){ int n = x *x; if (n < 1000) continue; if (n > 9999) break; int hi = n/100; int lo = n%100; if( hi/10 == hi%10 && lo/10 == lo%10){ printf("%d\n",n); } } return 0; }
Two, while do ... while loop
Program 3: 3N + 1 problem
The following procedures Bug
#include<stdio.h> int main(){ int n,count = 0; scanf("%d",&n); while(n >1){ if(n%2 ==1) n = n *3 +1; else n /= 2; count++; } printf("%d",count); return 0; }
When a large number of inputs, such as 987654321, the final output is a
A "" outputs an intermediate result "error method
#include<stdio.h> int main(){ int n,count = 0; scanf("%d",&n); while(n >1){ if(n%2 ==1){ n = n *3 +1; printf(" %d\n",n); }else{ n /= 2; } count++; } printf("%d",count); return 0; }
Found ----> multiplication overflow
# C99 does not specify the exact size of type int, but in the popular contest platform, int is 32 ---- -2147483648 to 2147483647
And the upper bound of n in question present upper limit of 10 ^ 9 is slightly smaller than the int, very easy to overflow
Therefore, problems can be solved using the long long - are: -2 ^ 63 to 2 ^ 63 - 1% lld input
Follow discussed in detail!
#include<stdio.h> int main(){ int n2,count = 0; scanf("%d",&n2); long long n = n2; while(n >1){ if(n%2 ==1) n = n *3 +1; else n /= 2; count++; } printf("%d",count); return 0; }
Procedure 4: approximated
#include<stdio.h> int main(){ double sum = 0; for(int i = 0;;i++){ double term = 1.0 / (i*2 + 1); if(i%2 ==0) sum += term; else sum -= term; if(term < 1e-6) break; } printf("%.6f\n",sum); return 0; }
#include<stdio.h> int main(){ double sum = 0; int i = 0; double term = 0; do{ term = 1.0/(i*2+1); if(i%2 == 0) sum += term; else sum -= term; i++; }while(term > 1e-6); printf("%.6f",sum); return 0; }
Third, the cost of recycling
Program 5: factorial sum
#include <stdio.h> int main () { int n-, S = 0 ; Scanf ( " % D " , & n-); for ( int I = . 1 ; I <= n-; I ++ ) { int factorial = . 1 ; // at the beginning of the loop defined variables, each time execution of the loop are declared and initialized patronize for ( int J = . 1 ; J <= I; J ++ ) factorial * = J; S + = factorial; } the printf ( " % D \ n- " , S%1,000,000 ); // for as long as the end 6, it is necessary for the modulo 10 ^ 6 output return 0 ; }
Obviously, very easy to overflow
When n = 10 ^ 6, the overflow will be more, but very slow
NOTE: To calculate contains only integer addition, subtraction and multiplication expression is divided by a positive integer n, n may take the remainder after each step in the calculation, the same results
Next, the program into the "modulo every step" form, and then add a timer to see Speed:
#include<stdio.h> #include<time.h> int main(){ const int MOD = 1000000; int n,s = 0; scanf("%d",&n); for(int i = 1;i <= n;i++){ int factorial = 1; for(int j = 1;j <=i;j++){ factorial = (factorial * j %MOD); } s = (s + factorial) %MOD; } printf("%d\n",s); printf("Time used = %.2f\n",(double) clock() /CLOCKS_PER_SEC); return 0; }
Timing function Clock () ---- so far run time of the function returns the program, call this function before the end of the program, you can get the entire running time of the program, after then divided by a constant CLOCKS_PER_SEC, the resulting value in seconds for the s unit
Keyboard input time is also taken into account, in order to avoid data input time affect the test results, you can use a technique called "pipe" tips:
Perform echo 20 in windows command line | abc, the system will automatically input 20, abc is the name of the program
Fourth, the input to the algorithm in the race, the output of the frame
Program 6: statistics
Some input integer, min max obtained and their average (three decimal places). Enter the number to ensure that these are no more than 1,000 integer
[There are] Bug
#include<stdio.h> int main(){ int x,n =0,min,max,s = 0; while(scanf("%d",&x) == 1){ s += x; if(x <min) min = x; if(x >max) max = x; n++; } printf("%d %d %.3f",min,max,(double) s / n); return 0; }
# Scanf () returns the number of input variables of success
have a test
5,002,320 come from? ---- variable value no previous assignment is uncertain !
The solution is to use before initial value
A better way is to use the file ---- input data stored in the file, the output data is also saved in the file. As long as the previously entered data stored in the file, there is no need to re-enter the
The easiest way to use the file is to use the input and output redirection
Only need to add the entrance to the main function:
freopen("input.txt","r",stdin) freopen("output.txt","w",stdout)
At such a statement scanf () reads the write output.txt from input.txt, printf ()
Algorithm contest, players should strictly abide by the provisions of the file name of the game, especially [path can not add the path, even if it is a relative path]
Methods: using file redirection when the unit test, but once the game is automatically submitted to "" delete "" Redirect statement
#define the LOCAL #include <stdio.h> #define INF 1000000000 int main () { #ifdef the LOCAL The freopen ( " data.in. " , " R & lt " , stdin); The freopen ( " data.out " , " W " , stdout ); #endif // the LOCAL int X, = n- 0 , min = INF, max = -INF, S = 0 ; // Causes of INF is: given a virtual infinity of the while (Scanf ( " % D " , X &) == . 1 ) { s += x; if(x <min) min = x; if(x >max) max= x; /* printf("x = %d,min = %d,max = %d\n",x,min,max); */ n++; } printf("%d %d %.3f\n",min,max,(double) s/ n); return 0; }
If the game requires a file input and output, but the way prohibit the use of redirection:
#include<stdio.h> #define INF 1000000000 int main(){ FILE *fin,*fout; fin = fopen("data.in","r"); fout = fopen("data.out","wb"); int x,n = 0,min = INF,max = -INF,s = 0; while(fscanf(fin,"%d",&x)== 1){ s += x; if(x < min) min = x; if(x >max) max = x; n++; } fprintf(fout,"%d %d %.3f\n",min,max,(double) s / n); fclose(fin); fclose(fout); return 0; }
用fopen("con","r")的方法打开标准输入输出不是可移植的,在Linux下是无效的!!!!
程序7: 多组数据问题
#include<stdio.h> #define INF 1000000000 int main(){ int x,n =0,min = INF,max = -INF,s = 0,kase = 0; while(scanf("%d",&n) == 1&& n){ int s= 0; for(int i = 0;i <n;i++){ scanf("%d",&x); s += x; if(x < min) min = x; if(x >max) max = x; } if(kase) printf("\n"); printf("Case %d: %d %d %.3f\n",++kase,min,max,(double) s/n); } return 0; }
要点分析: