Common algorithm questions in iOS interviews and common iOS interview Algorithms

Source: Internet
Author: User

Common algorithm questions in iOS interviews and common iOS interview Algorithms

I. Preface

Here is an algorithm question you have encountered in your iOS job search. I hope it will help you. Occasionally updated. If you want to run code debugging online, you can copy the code here. Then perform debugging. The following are common algorithm questions.

Ii. Text

1. The factorial of n.

Idea: Recursive Implementation

#include <stdio.h>int getNJ(int n) {    if (n==1 || n==0) {        return 1;    }    return n*getNJ(n-1);}int main() {    printf("%d",getNJ(10));    return 0;}

The running result is as follows:

3628800

 

2. Determine whether a string is an ip address.

Train of Thought: divide the string into two arrays (one digit array and one character array), and then determine whether the content of the number array meets the ip address conditions and whether the content of the character array is ".". The sscanf function is used here. The Code is as follows:

# Include <stdio. h> int checkIP (const char * p) {int n [4]; char c [4]; if (sscanf (p, "% d % c", & n [0], & c [0], & n [1], & c [1], & n [2], & c [2], & n [3], & c [3]) = 7) {int I; 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 ;}} int main () {const char * x [] = {"132.161.1", "10.0.0.1. "," 127.256.0.1 "," iudfdsfdasfdaf "," 172.16, 2.1 "}; const char * m [] = {" not a valid IP Address "," a valid IP Address "}; int I = 0; while (x [I]! = 0) {printf ("% s \ n", x [I], m [checkIP (x [I]); I ++;} return 0; return 0 ;}

The running result is as follows:

132.161.1 is a legal IP address 10.0.0.1. The IP address 127.256.0.1 is not a legal IP address. iudfdsfdasfdaf is not a legal IP address 172.16, and 2.1 is not a legal IP Address

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.