First, preface
Here is the iOS job in the problem of the algorithm, I hope to help you. Not regularly updated. If you want to run code debugging online, you can copy the code here. and then Debug. The following are common algorithm topics.
Second, the text
1, on the factorial of N.
Idea: This is achieved by recursion
#include <stdio.h>intGETNJ (intN) {if(n==1|| n==0) { return 1; } returnN*GETNJ (n1);}intMain () {printf ("%d", Getnj (Ten)); return 0;}
The results of the operation are as follows:
3628800
2, determine whether a string is an IP.
Idea: First divide the string into two arrays (a numeric array, a character array), and then determine whether the content of the array satisfies the IP condition, and whether the character array is ".". This mainly uses the SSCANF function. The code is as follows:
#include <stdio.h>intCheckip (Const Char*p) {intn[4]; Charc[4]; if(SSCANF (P,"%d%c%d%c%d%c%d%c", &n[0],&c[0],&n[1],&c[1],&n[2],&c[2],&n[3],&c[3])==7) { inti; for(i=0;i<3; i++) { if(c[i]!='.') { return 0; } } for(i=0;i<4; i++) { if(n[i]>255|| n[i]<0) { return 0; } } return 1; }Else { return 0; }}intMain () {Const Char*x[] = { "132.168.1.1", "10.0.0.1.", "127.256.0.1", "Iudfdsfdasfdaf", "172.16,2.1" }; Const Char*m[] = { "not a legitimate IP address", "It's a legitimate IP address." }; intI=0; while(x[i]!=0) {printf ("%s%s\n", X[i],m[checkip (X[i])); I++; } return 0; return 0;}
The results of the operation are as follows:
132.168. 1.1 It's a legitimate IP address. 10.0. 0.1 . Not a valid IP address 127.256. 0.1 not a valid IP address IUDFDSFDASFDAF is not a valid IP address 172.16,2.1 is not a legitimate IP address
Common algorithmic topics in iOS interviews