1、IP转换成整数及整数转换成IP
Java实现:
package com.mian.demo; public class IpToInt { public static void main(String[] args) { String ip="192.168.12.90"; int ipInt=ipToInt(ip); System.out.println(ipInt); String ipStr=intToIp(ipInt); System.out.println(ipStr); } private static String intToIp(int ip){ StringBuilder sb=new StringBuilder(); for(int i=3;i>=0;--i){ int ipa=(ip>>(8*i))&(0xff); System.out.print(ipa+" "); sb.append(String.valueOf(ipa)+"."); } return new String(sb).substring(0,sb.length()-1); } private static int ipToInt(String ip){ int res=0; String[] arr=ip.split("\."); for(int i=0;i<arr.length;++i){ int ipa=Integer.parseInt(arr[i]); System.out.print(ipa+" "); res=(res<<8)|ipa;//用+也可以,但是位运算更快 } return res; } }
C++实现:
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 int ip_to_int(string ip) 7 { 8 size_t idx = 0; 9 string str = ""; 10 int ret = 0; 11 12 while (true) 13 { 14 idx = ip.find_first_of('.'); 15 str = ip.substr(0, idx); 16 int ipa = atoi(str.c_str()); 17 ret = (ret << 8) | ipa;//用+也可以,但是位运算更快 18 ip = ip.substr(idx + 1); 19 if (idx == string::npos) 20 break; 21 } 22 23 return ret; 24 } 25 26 string int_to_ip(int ip) 27 { 28 string str = ""; 29 for (int i = 3; i >= 0; --i) 30 { 31 int ipa = (ip >> (8 * i))&(0xff); 32 str.append(to_string(ipa) + "."); 33 } 34 35 return str.substr(0, str.size() - 1); 36 } 37 38 int main() 39 { 40 string ip = "192.168.12.90"; 41 int p = ip_to_int(ip); 42 cout << p << endl; 43 cout << int_to_ip(p) << endl; 44 45 return 0; 46 }
2、给一个字符串表示IP地址,检测是否合法
Java实现:
package com.mian.demo; import java.util.Scanner; public class IsValidIp { public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()) { String input = sc.next(); System.out.println(isValidIp(input)); } } private static boolean isValidIp(String ip){ int size=ip.length(); if(ip.isEmpty()||size<7||size>15){ return false; } String[] arr=ip.split("\."); if(arr.length!=4){ return false; } for(int i=0;i<arr.length;++i){ if(arr[i].length()>1&&arr[i].charAt(0)=='0'){ return false; } for(int j=0;j<arr[i].length();++j){ if(arr[i].charAt(j)<'0'||arr[i].charAt(j)>'9'){ return false; } } } for(int i=0;i<arr.length;++i){ int tmp=Integer.parseInt(arr[i]); if(i==0){ if(tmp<1||tmp>255){ return false; } }else{ if(tmp<0||tmp>255){ return false; } } } return true; } }
C++实现:
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 5 using namespace std; 6 7 bool isValidIp(string &ip) 8 { 9 int size = ip.size(); 10 if (ip.empty() || size < 7 || size>15) 11 return false; 12 vector<string> arr; 13 size_t idx = 0; 14 while (true) 15 { 16 idx = ip.find_first_of('.'); 17 arr.push_back(ip.substr(0, idx)); 18 ip = ip.substr(idx + 1); 19 if (idx == string::npos) 20 break; 21 } 22 if (arr.size() != 4) 23 return false; 24 for (int i = 0; i < arr.size(); ++i) 25 { 26 if (arr[i].size() > 1 && arr[i][0] == '0') 27 return false; 28 for (int j = 0; j < arr[i].size(); ++j) 29 if (arr[i][j]<'0' || arr[i][j]>'9') 30 return false; 31 } 32 for (int i = 0; i < arr.size(); ++i) 33 { 34 int tmp = atoi(arr[i].c_str()); 35 if (i == 0) 36 { 37 if (tmp < 1 || tmp>255) 38 return false; 39 } 40 else 41 { 42 if (tmp < 0 || tmp>255) 43 return false; 44 } 45 } 46 return true; 47 } 48 49 int main() 50 { 51 string str; 52 while (cin >> str) 53 cout << isValidIp(str) << endl; 54 55 return 0; 56 }
3、杨辉三角
Java实现:
package com.mian.demo; import java.util.ArrayList; import java.util.List; public class YangHuiSanJiao { public static void main(String[] args) { int[][] res=yangHuiSanJiao(10); for(int i=0;i<res.length;++i){ for(int k=0;k<res.length-i;++k){ System.out.print(" "); } for(int j=0;j<res[i].length;++j){ System.out.print(res[i][j]+" "); } System.out.println(); } } private static int[][] yangHuiSanJiao(int n){ int[][] res=new int[n][n]; for(int i=0;i<n;++i){ res[i]=new int[i+1]; } for(int i=0;i<n;++i){ res[i][0]=1; res[i][i]=1; for(int j=1;j<i;++j){ res[i][j]=res[i-1][j]+res[i-1][j-1]; } } return res; } }
C++实现:
1 #include<iostream> 2 #include<vector> 3 4 using namespace std; 5 6 vector<vector<int>> yanghui(int n) 7 { 8 vector<vector<int>> res(n, vector<int>()); 9 for (int i = 0; i < n; ++i) 10 { 11 res[i].resize(i + 1); 12 res[i][0] = 1; 13 res[i][i] = 1; 14 } 15 for (int i = 1; i < n; ++i) 16 for (int j = 1; j < i; ++j) 17 res[i][j] = res[i - 1][j - 1] + res[i - 1][j]; 18 19 return res; 20 } 21 22 int main() 23 { 24 int n; 25 while (cin >> n) 26 { 27 vector<vector<int>> res = yanghui(n); 28 for (int i = 0; i < n; ++i) 29 { 30 for (int k = 0; k < n - i; ++k) 31 cout << " "; 32 for (int j = 0; j < res[i].size(); ++j) 33 if (j == 0) 34 cout << res[i][j]; 35 else 36 cout << " " << res[i][j]; 37 cout << endl; 38 } 39 } 40 41 return 0; 42 }
4、定义一个函数求字符串长度,要求该函数体中不能声明任何一个变量(不使用额外变量,实现strlen函数)
1 #include <iostream> 2 3 using namespace std; 4 5 int getLen(char *str) 6 { 7 if (*str == '