├── 汽水瓶.cpp ├── 取近似值.cpp ├── 等差数列.cpp ├── 图片整理.cpp ├── iNOC产品部-杨辉三角.cpp ├── 统计每个月兔子的总数.cpp ├── 取近似值.py ├── 求int型数据在内存中存储时1的个数.cpp ├── 求小球落地5次后所经历的路程和第5次反弹的高度.cpp ├── 查找输入整数二进制中1的个数.cpp ├── 字符串合并处理.cpp ├── 质数因子.cpp ├── 字符个数统计.cpp ├── 蛇形矩阵.cpp ├── 字符串匹配.cpp ├── 尼科彻斯定理.cpp ├── 求最大连续bit数.cpp ├── 统计大写字母个数.cpp ├── 计算字符个数.cpp ├── 字符串反转.cpp ├── 放苹果.cpp ├── 数字颠倒.cpp ├── 删除字符串中出现次数最少的字符.cpp ├── iNOC产品部--完全数计算.cpp ├── 挑7.cpp ├── 提取不重复的整数.py ├── 字符逆序.cpp ├── 求解立方根.cpp ├── 输入n个整数,输出其中最小的k个 .cpp ├── 字符串排序.cpp ├── 找出字符串中第一个只出现一次的字符.cpp ├── 自守数.cpp ├── 输入一行字符,分别统计个数.cpp ├── 矩阵乘法A.cpp ├── 记负均正.cpp ├── 名字的漂亮度.cpp ├── 计算日期到天数转换.cpp ├── 合并表记录.cpp ├── 成绩排序.cpp ├── 字符串最后一个单词的长度.cpp ├── 记票统计.cpp ├── DNA序列.cpp ├── 在字符串中找出连续最长数字串.py ├── 求最小公倍数.cpp ├── 字符统计.cpp ├── 在字符串中找出连续最长的数字串.cpp ├── 简单密码破解.cpp ├── 素数伴侣.cpp ├── 字符串运用-密码截取.cpp ├── 合唱队.cpp ├── 进制转换.cpp ├── 公共字串计算.cpp ├── 计算字符串的相似度.cpp ├── 单词倒排.cpp ├── 句子逆序.cpp ├── 合法IP.cpp ├── 输出单向链表中倒数第k个.cpp ├── 参数解析.cpp ├── 字符串加密.cpp ├── 超长正整数相加.c ├── 寻找等差数列.cpp ├── 字符串加解密.cpp ├── 整型数组合并.cpp ├── 判断两个IP是否属于同一个子网.cpp ├── 表示数字.cpp ├── 从单向链表中删除指定值.cpp ├── 密码强度等级.cpp ├── 表达式求值.cpp └── 学英语.cpp /汽水瓶.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void main() 4 | { 5 | int n; 6 | while (cin >> n, n) 7 | { 8 | cout << n / 2 << endl; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /取近似值.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | double a; 6 | cin >> a; 7 | if (a >= 0) 8 | cout << (int)(a + 0.5); 9 | else 10 | cout << (int)(a - 0.5); 11 | } 12 | -------------------------------------------------------------------------------- /等差数列.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solution(int num) 5 | { 6 | if (num < 0) return NULL; 7 | return ((3 * num + 1)* num)/2; 8 | } 9 | 10 | void main() 11 | { 12 | int num; 13 | cin >> num; 14 | cout << Solution(num) << endl; 15 | } 16 | -------------------------------------------------------------------------------- /图片整理.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | char str[1024]; 7 | cin.getline(str, 1024); 8 | sort(str, str + strlen(str)); 9 | for (int i = 0; str[i] != '\0'; i++) 10 | cout << str[i]; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /iNOC产品部-杨辉三角.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void main() 4 | { 5 | int n; 6 | cin >> n; 7 | if (n % 2 == 1) cout << 2 << endl; 8 | else if (n % 2 == 0 && (n / 2 % 2 + 3 <= n + 1)) 9 | cout << n / 2 % 2 + 3 << endl; 10 | else 11 | cout << -1 << endl; 12 | } 13 | -------------------------------------------------------------------------------- /统计每个月兔子的总数.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solution(int n) 5 | { 6 | if (n <= 2) return 1; 7 | else 8 | return Solution(n - 1) + Solution(n - 2); 9 | } 10 | 11 | void main() 12 | { 13 | int n; 14 | cin >> n; 15 | cout << Solution(n) << endl; 16 | } 17 | -------------------------------------------------------------------------------- /取近似值.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fun(self, F): 3 | result = 0; flag = 0 4 | if F < 0: result = F - 0.5 5 | else: result = F + 0.5 6 | return int(result) 7 | 8 | if __name__ == "__main__": 9 | F = input("input string:") 10 | result = Solution().fun(F) 11 | print result 12 | -------------------------------------------------------------------------------- /求int型数据在内存中存储时1的个数.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solution(int num) 5 | { 6 | int count = 0; 7 | while (num) 8 | { 9 | ++count; 10 | num = num & (num - 1); 11 | } 12 | return count; 13 | } 14 | 15 | void main() 16 | { 17 | int num; 18 | cin >> num; 19 | cout << Solution(num); 20 | } 21 | -------------------------------------------------------------------------------- /求小球落地5次后所经历的路程和第5次反弹的高度.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void main() 5 | { 6 | double heigh, s = 0.0; 7 | cin >> heigh; 8 | s = heigh; 9 | for (int i = 1; i < 5; i++) 10 | { 11 | heigh = 0.5 * heigh; 12 | s += heigh * 2; 13 | } 14 | cout << s << endl; 15 | cout << heigh * 0.5 << endl; 16 | } 17 | -------------------------------------------------------------------------------- /查找输入整数二进制中1的个数.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solution(int num) 5 | { 6 | int count = 0; 7 | while (num) 8 | { 9 | num = num & (num - 1); 10 | count++; 11 | } 12 | return count; 13 | } 14 | 15 | void main() 16 | { 17 | int num; 18 | cin >> num; 19 | cout << Solution(num) << endl; 20 | } 21 | -------------------------------------------------------------------------------- /字符串合并处理.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | void Solution(char * X, char * Y) 6 | { 7 | 8 | } 9 | 10 | void main() 11 | { 12 | char str1[100]; char str2[100]; 13 | cin >> str1; 14 | cin >> str2; 15 | 16 | Solution(str1, str2); 17 | 18 | /* 后缀数组方法 */ 19 | 20 | //LCS_suffix(X, strlen(X), Y, strlen(Y)); 21 | } 22 | -------------------------------------------------------------------------------- /质数因子.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void getResult(long int input) 5 | { 6 | for (int i = 2; i <= input; i++) 7 | { 8 | while ((input%i == 0) && input != 0) 9 | { 10 | cout << " "; 11 | input = input / i; 12 | } 13 | } 14 | 15 | } 16 | 17 | int main() 18 | { 19 | int a; 20 | cin >> a; 21 | getResult(a); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /字符个数统计.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solution(char *str) 5 | { 6 | if (str == NULL) return 0; 7 | 8 | int count = 0; char *p = str; 9 | while (*p != '\0') 10 | { 11 | ++count; 12 | ++p; 13 | } 14 | return count; 15 | } 16 | 17 | void main() 18 | { 19 | char str[1000]; 20 | cin.getline(str, 1000); 21 | cout << Solution(str); 22 | } 23 | -------------------------------------------------------------------------------- /蛇形矩阵.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int y, x, start = 1; 6 | int n; 7 | cin >> n; 8 | for (y = 0; y < n; y++) 9 | { 10 | start += y; 11 | cout << start << " "; 12 | int end = start; 13 | for (x = y + 2; x <= n; x++) 14 | { 15 | end += x; 16 | cout << end << " "; 17 | 18 | } 19 | cout << endl; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /字符串匹配.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | char a[100], b[50]; 6 | gets(b); 7 | gets(a); 8 | int flag = 0; 9 | for (int i = 0; b[i] != '\0'; i++) 10 | for (int j = 0; a[j] != '\0'; j++) 11 | if (b[i] == a[j]) 12 | { 13 | flag++; 14 | break; 15 | } 16 | if (flag == strlen(b)) 17 | cout << "true"; 18 | else 19 | cout << "false"; 20 | } 21 | -------------------------------------------------------------------------------- /尼科彻斯定理.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void Solution(int n) 5 | { 6 | int x; 7 | x = n * n - n + 1; 8 | 9 | for (int i = 0; i < n - 1; i++) 10 | cout << x + 2 * i << "+"; 11 | cout << x + 2 * (n-1) << endl; 12 | } 13 | 14 | int main() 15 | { 16 | int num; 17 | cin >> num; 18 | if (num <= 100) 19 | Solution(num); 20 | else 21 | return -1; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /求最大连续bit数.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void main() 4 | { 5 | int num, count = 0, result = 0; 6 | cin >> num; 7 | char Array[32]; 8 | _itoa_s(num, Array, 2); 9 | for (int i = 0; i < strlen(Array); i++) 10 | { 11 | if (Array[i] == '1') 12 | count++; 13 | else 14 | count = 0; 15 | if (count > result) 16 | result = count; 17 | } 18 | cout << result << endl; 19 | } 20 | -------------------------------------------------------------------------------- /统计大写字母个数.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solution(char *str) 5 | { 6 | if (str == NULL) return NULL; 7 | 8 | int count = 0; 9 | while (*str != '\0') 10 | { 11 | if (*str >= 'A' && *str <= 'Z') ++count; 12 | ++str; 13 | } 14 | return count; 15 | } 16 | 17 | void main() 18 | { 19 | char string[100]; 20 | cin.getline(string, 100); 21 | cout << Solution(string) << endl; 22 | } 23 | -------------------------------------------------------------------------------- /计算字符个数.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solution(char *str, char ch) 5 | { 6 | if (str == NULL || ch == NULL) return NULL; 7 | 8 | int count = 0; 9 | while (*str != '\0') 10 | { 11 | if (*str == ch) ++count; 12 | ++str; 13 | } 14 | return count; 15 | } 16 | 17 | void main() 18 | { 19 | char str[1000]; char ch; 20 | cin.getline(str, 1000); 21 | cin >> ch; 22 | cout << Solution(str, ch); 23 | } 24 | -------------------------------------------------------------------------------- /字符串反转.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void Solution(char *str) 5 | { 6 | if (str == NULL) return; 7 | 8 | char *p1 = str; char *p2 = str; 9 | while (*p2 != '\0') ++p2; 10 | --p2; 11 | while (p1 < p2) 12 | { 13 | char temp = *p1; 14 | *p1 = *p2; 15 | *p2 = temp; 16 | ++p1; --p2; 17 | } 18 | } 19 | 20 | void main() 21 | { 22 | char str[100]; 23 | cin.getline(str, 100); 24 | Solution(str); 25 | cout << str; 26 | } 27 | -------------------------------------------------------------------------------- /放苹果.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solution(int m, int n) 5 | { 6 | if (m == 0 || n == 1) return 1; 7 | else if (m < n) return Solution(m, m); 8 | else 9 | return Solution(m, n - 1) + Solution(m - n, n); 10 | } 11 | 12 | void main() 13 | { 14 | int m, n; 15 | cin >> m; 16 | cin >> n; 17 | if ((m >= 0 && m <= 10) && (n >= 1 && n <= 10) && (n <= m)) 18 | cout << Solution(m, n) << endl; 19 | else 20 | return; 21 | } 22 | -------------------------------------------------------------------------------- /数字颠倒.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void Solution(int num) 5 | { 6 | if (num < 0) return; 7 | 8 | char* str = (char*)malloc(1); 9 | if (num == 0) 10 | { 11 | _itoa_s(num, str, 10, 10); 12 | printf("%s", str); 13 | } 14 | while (num) 15 | { 16 | _itoa_s(num%10, str, 10, 10); 17 | printf("%s", str); 18 | num = num / 10; 19 | } 20 | 21 | } 22 | 23 | void main() 24 | { 25 | int num; 26 | cin >> num; 27 | Solution(num); 28 | } 29 | -------------------------------------------------------------------------------- /删除字符串中出现次数最少的字符.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void main() 6 | { 7 | string str; 8 | cin >> str; 9 | int count[26] = {0}; 10 | 11 | for (int i = 0; i < str.size(); i++) 12 | count[str[i] - 97] += 1; 13 | for (int i = 0; i < str.size(); i++) 14 | { 15 | for (int j = 0; j < 26; j++) 16 | { 17 | if ((count[str[i] - 97] > count[j]) && count[j] != 0) 18 | { 19 | cout << str[i]; 20 | break; 21 | } 22 | } 23 | } 24 | cout << endl; 25 | } 26 | -------------------------------------------------------------------------------- /iNOC产品部--完全数计算.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solution(int n) 5 | { 6 | int count = 0; 7 | for (int i = 1; i < n; i++) 8 | { 9 | int sum = 0; 10 | for (int j = 1; j < i; j++) 11 | { 12 | if (i%j == 0) sum += j; 13 | } 14 | if (sum == i) 15 | count++; 16 | } 17 | return count; 18 | } 19 | 20 | int main() 21 | { 22 | int num; 23 | cin >> num; 24 | if (num > 0) 25 | { 26 | cout << Solution(num) << endl; 27 | return 0; 28 | } 29 | else 30 | return -1; 31 | } 32 | -------------------------------------------------------------------------------- /挑7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int Solution(int N) 6 | { 7 | int num = 0; 8 | for (int i = 1; i <= N; i++) 9 | if (i % 7 == 0) 10 | num++; 11 | else 12 | { 13 | char str[10]; 14 | _itoa_s(i, str, 10, 10); 15 | for (int j = 0; j> N; 29 | cout << Solution(N) << endl; 30 | } 31 | -------------------------------------------------------------------------------- /提取不重复的整数.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fun(self, List): 3 | Dict = {}; res = '' 4 | for item in List: 5 | if item not in Dict: Dict.setdefault(item,1) 6 | else: Dict[item] += 1 7 | for i in range(len(List) - 1, -1, -1): 8 | if Dict[List[i]] > 0: 9 | res += List[i] 10 | Dict[List[i]] = 0 11 | return int(res) 12 | 13 | if __name__ == "__main__": 14 | List = str(input()) 15 | result = Solution().fun(List) 16 | print result 17 | -------------------------------------------------------------------------------- /字符逆序.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | char* Reverse(char *s) 6 | { 7 | if (s == NULL) return NULL; 8 | 9 | char *pBegin = s; 10 | char *pEnd = s; 11 | while (*pEnd != '\0') 12 | pEnd++; 13 | pEnd--; 14 | 15 | while (pBegin < pEnd) 16 | { 17 | char temp = *pBegin; 18 | *pBegin = *pEnd; 19 | *pEnd = temp; 20 | 21 | pBegin++; 22 | pEnd--; 23 | } 24 | return s; 25 | } 26 | 27 | int main() 28 | { 29 | char str[500]; 30 | cin.getline(str, 500); 31 | cout << Reverse(str) << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /求解立方根.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define E 0.0001 4 | using namespace std; 5 | 6 | double getCubeRoot(double num) 7 | { 8 | double x0 = num; 9 | double result; 10 | while (1) 11 | { 12 | result = x0 - (x0*x0*x0 - num) / (3 * x0*x0); 13 | if (result*result*result - num-E) 14 | return result; 15 | else 16 | x0 = result; 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | int number; 23 | cin >> number; 24 | cout << fixed << showpoint << setprecision(1) << getCubeRoot(number) << endl; 25 | } 26 | -------------------------------------------------------------------------------- /输入n个整数,输出其中最小的k个 .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool comp1(const int &a, const int&b) 7 | { 8 | return a > b; 9 | } 10 | 11 | void GetLeastNumbers(const vector& num, int k) 12 | { 13 | int temp[k]; 14 | int count = num.size(); 15 | for (int i = 0; i < count; i++) 16 | { 17 | 18 | } 19 | } 20 | 21 | 22 | void main() 23 | { 24 | int N, k; 25 | cin >> N; 26 | cin >> k; 27 | vector array; 28 | for (int i = 0; i> m; 32 | array.push_back(m); 33 | } 34 | Solution(array, k); 35 | } 36 | -------------------------------------------------------------------------------- /字符串排序.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | string str; 9 | getline(cin, str); 10 | 11 | int Len = str.size(); 12 | for (int i = 1; i < Len; i++) 13 | { 14 | for (int j = Len - 1; j >= i; j--) 15 | { 16 | int k = j - 1; 17 | if (isalpha(str[j])) 18 | { 19 | while (!isalpha(str[k])) --k; 20 | if (tolower(str[k]) > tolower(str[j])) 21 | { 22 | int temp = str[j]; 23 | str[j] = str[k]; 24 | str[k] = temp; 25 | } 26 | } 27 | else 28 | continue; 29 | } 30 | } 31 | 32 | cout << str << endl; 33 | } 34 | -------------------------------------------------------------------------------- /找出字符串中第一个只出现一次的字符.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //有Bug啊! 5 | char Solution(char *str) 6 | { 7 | if (str == NULL) return NULL; 8 | const int tablesize = 256; 9 | unsigned int hashTable[tablesize]; 10 | for (int i = 0; i < tablesize; i++) 11 | hashTable[i] = 0; 12 | 13 | char* p = str; 14 | while (*(p) != '\0') 15 | hashTable[*(p++)] ++; 16 | 17 | p = str; 18 | while (*p != '\0') 19 | { 20 | if (hashTable[*p] == 1) 21 | return *p; 22 | p++; 23 | } 24 | return NULL; 25 | } 26 | 27 | void main() 28 | { 29 | char str[200]; 30 | cin.getline(str, 200); 31 | cout << Solution(str) << endl; 32 | } 33 | -------------------------------------------------------------------------------- /自守数.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int CalcAutomorphicNumbers(int n) 4 | { 5 | 6 | int count = 0; 7 | for (int i = 0; i < n; i++) 8 | { 9 | int sum = i * i; 10 | if (!(i / 10)) 11 | { 12 | if (sum % 10 == i) count++; 13 | } 14 | else if (!(i / 100)) 15 | { 16 | if (sum % 100 == i) count++; 17 | } 18 | else if (!(i / 1000)) 19 | { 20 | if (sum % 1000 == i) count++; 21 | } 22 | else if (!(i / 10000)) 23 | { 24 | if (sum % 10000 == i) count++; 25 | } 26 | } 27 | 28 | return count; 29 | } 30 | 31 | int main() 32 | { 33 | int num; 34 | cin >> num; 35 | cout << CalcAutomorphicNumbers(num) << endl;; 36 | } 37 | -------------------------------------------------------------------------------- /输入一行字符,分别统计个数.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void main() 5 | { 6 | char str[1000]; 7 | int countEnglishChar = 0, countBlank = 0, countNumber = 0, countOther = 0; 8 | cin.getline(str, 1000); 9 | 10 | for (int i = 0; i < strlen(str); i++) 11 | { 12 | if (str[i] >= 'a' &&str[i] <= 'z' || str[i] >= 'A' &&str[i] <= 'Z') 13 | countEnglishChar++; 14 | else if (str[i] >= '0' &&str[i] <= '9') 15 | countNumber++; 16 | else if (str[i] == ' ') 17 | countBlank++; 18 | else 19 | countOther++; 20 | } 21 | 22 | cout << countEnglishChar << endl; 23 | cout << countBlank << endl; 24 | cout << countNumber << endl; 25 | cout << countOther << endl; 26 | } 27 | -------------------------------------------------------------------------------- /矩阵乘法A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a[100][100], b[100][100], c[100][100] = { 0 }; 7 | int x, y, z; 8 | cin >> x; 9 | cin >> y; 10 | cin >> z; 11 | for (int i = 0; i> a[i][j]; 14 | for (int i = 0; i> b[i][j]; 17 | 18 | for (int i = 0; i < x; i++) 19 | for (int j = 0; j < z; j++) 20 | for (int k = 0; k < y; k++) 21 | c[i][j] = a[i][k] * b[k][j]; 22 | 23 | for (int i = 0; i < x; i++) 24 | { 25 | for (int j = 0; j < z - 1; j++) 26 | cout << c[i][j] << " "; 27 | cout << c[i][z - 1] << endl; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /记负均正.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int a[100]; 7 | int n; 8 | int countfu = 0; 9 | int count = 0; 10 | int sum = 0; 11 | double average = 0; 12 | cin >> n; 13 | for (int i = 0; i> m; 17 | a[i] = m; 18 | } 19 | for (int i = 0; i=0) 24 | { 25 | sum += a[i]; 26 | count++; 27 | } 28 | } 29 | average = double(sum) / count; 30 | cout << countfu; 31 | cout << ' '; 32 | if (sum % (count) == 0) 33 | cout << sum / (count) << endl; 34 | else 35 | cout << fixed << setprecision(1) << average << endl; 36 | } 37 | -------------------------------------------------------------------------------- /名字的漂亮度.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int Beauty(char a[]) //计算一个字符串的最大漂亮度 6 | { 7 | int b[26] = { 0 }; //存储每个字符的个数 8 | int sum = 0; 9 | for (int i = 0; a[i] != '\0'; i++) 10 | a[i] = tolower(a[i]); //全部先转换成小写 11 | for (int i = 0; a[i] != '\0'; i++) 12 | b[a[i] - 97]++; 13 | sort(b, b + 26); 14 | for (int i = 25; i >= 0; i--) 15 | sum += (i + 1)*b[i]; 16 | return sum; 17 | } 18 | 19 | int main() 20 | { 21 | int M; 22 | char array[100][100]; 23 | cin >> M; 24 | getchar(); //清除回车 25 | for (int i = 0; i 2 | using namespace std; 3 | 4 | bool loopyear(int year) //判断是否是闰年 5 | { 6 | if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) 7 | return 1; 8 | else 9 | return 0; 10 | } 11 | 12 | int getOutDay(int year, int month, int day) 13 | { 14 | int sum = 0; 15 | int a[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; 16 | if (loopyear(year) && month>2) //如果是闰年且月份大于2 17 | sum = a[month - 1] + day + 1; 18 | else 19 | sum = a[month - 1] + day; 20 | return sum; 21 | } 22 | int main() 23 | { 24 | int year, month, day; 25 | cin >> year; 26 | cin >> month; 27 | cin >> day; 28 | cout << getOutDay(year, month, day) << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /合并表记录.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | struct Record 5 | { 6 | int Index; 7 | int Value; 8 | }; 9 | bool cmp(const struct Record a, const struct Record b) 10 | { 11 | return a.Index> N; 19 | for (int i = 0; i < N; i++) 20 | cin >> array[i].Index >> array[i].Value; 21 | sort(array, array + N, cmp); 22 | 23 | for (int i = 0; i < N; i++) 24 | if (array[i].Index == array[i + 1].Index) 25 | array[i + 1].Value = array[i].Value + array[i + 1].Value; 26 | else 27 | { 28 | cout << array[i].Index << endl; 29 | cout << array[i].Value << endl; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /成绩排序.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct student 6 | { 7 | char name[20]; 8 | int score; 9 | }; 10 | 11 | bool compare0(student A, student B) 12 | { 13 | return A.score > B.score; 14 | } 15 | bool compare1(student A, student B) 16 | { 17 | return A.score < B.score; 18 | } 19 | 20 | void main() 21 | { 22 | int N, flag; 23 | cin >> N; 24 | student array[100]; 25 | cin >> flag; 26 | for (int i = 0; i> array[i].name >> array[i].score; 28 | 29 | if (flag == 0) 30 | sort(array, array + N, compare0); 31 | if (flag == 1) 32 | sort(array, array + N, compare1); 33 | for (int i = 0; i 5 | using namespace std; 6 | 7 | int Solution(char *str) 8 | { 9 | if (str == NULL) return NULL; 10 | 11 | char *p1 = str; 12 | char *p2 = str; 13 | int spaceNum = 0; 14 | int lenLastWord = 0; 15 | 16 | while (*p1 == ' ') ++p1; 17 | while (*p1 != '\0') 18 | { 19 | if (*p1 == ' ' && *(++p1) != ' ') ++spaceNum; 20 | ++p1; 21 | } 22 | 23 | while (*p2 == ' ') ++p2; 24 | while (*p2 != '\0') 25 | { 26 | if (*p2 == ' ' && *(++p2) != ' ') --spaceNum; 27 | if (spaceNum == 0) ++lenLastWord; 28 | ++p2; 29 | } 30 | 31 | return lenLastWord; 32 | } 33 | 34 | void main() 35 | { 36 | char string[1000]; 37 | cin.getline(string, 1000); 38 | cout << Solution(string) << endl; 39 | } 40 | -------------------------------------------------------------------------------- /记票统计.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | struct vote 5 | { 6 | string name; 7 | int count; 8 | }; 9 | int main() 10 | { 11 | int numhx, numvote, Invalid = 0; 12 | struct vote vt[100]; 13 | string s[100]; 14 | cin >> numhx; 15 | for (int i = 0; i < numhx; i++) 16 | { 17 | cin >> vt[i].name; 18 | vt[i].count = 0; 19 | } 20 | cin >> numvote; 21 | for (int i = 0; i < numvote; i++) 22 | cin >> s[i]; 23 | 24 | for (int i = 0; i < numvote; i++) 25 | for (int j = 0; j < numhx; j++) 26 | if (vt[i].name == s[j]) 27 | vt[i].count++; 28 | 29 | for (int i = 0; i < numhx; i++) 30 | { 31 | cout << vt[i].name << " : " << vt[i].count << endl; 32 | Invalid += vt[i].count; 33 | } 34 | cout << "Invalid" << " : " << numvote - Invalid << endl; 35 | } 36 | -------------------------------------------------------------------------------- /DNA序列.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void Solution(char *str, int num) 6 | { 7 | float MaxRatio = 0.0; 8 | char *p1 = str; char *p2 = str; char *result = NULL; 9 | while (*p1 != '\0') 10 | { 11 | p2 = p1; 12 | float ratio = 0.0; int GCNum = 0; 13 | while (p2 - p1 < num) 14 | { 15 | if (*p2 == 'G' || *p2 == 'C') GCNum++; 16 | ++p2; 17 | } 18 | 19 | ratio = GCNum / float(num); 20 | if (MaxRatio < ratio) 21 | { 22 | result = p1; 23 | MaxRatio = ratio; 24 | } 25 | ++p1; 26 | } 27 | 28 | for (int i = 0; i < num; i++) 29 | printf("%c", result[i]); 30 | system("pause"); 31 | } 32 | 33 | void main() 34 | { 35 | char str[100]; int num; 36 | scanf_s("%s", &str, 100); 37 | scanf_s("%d", &num, 10); 38 | Solution(str, num); 39 | 40 | } 41 | -------------------------------------------------------------------------------- /在字符串中找出连续最长数字串.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fun(self, List): 3 | temp_res = ''; res = '' 4 | temp_num = 0; num = 0 5 | shuzi = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] 6 | for i in range(1, len(List)): 7 | if List[i] in shuzi: 8 | temp_res += List[i] 9 | if List[i - 1]: temp_num += 1 10 | else: 11 | if temp_num > num: 12 | res = temp_res 13 | num = temp_num 14 | temp_res = ''; temp_num = 0 15 | if temp_num > num: 16 | res = temp_res 17 | num = temp_num 18 | print res,',',num 19 | return 20 | 21 | if __name__ == "__main__": 22 | List = str(raw_input("input string:")) 23 | result = Solution().fun(List) 24 | -------------------------------------------------------------------------------- /求最小公倍数.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //最大公约数 5 | int GCD(int num1, int num2) 6 | { 7 | int temp; 8 | if (num1 > num2) 9 | { 10 | temp = num1; 11 | num1 = num2; 12 | num2 = temp; 13 | } 14 | if (num2%num1 == 0) 15 | return num1; 16 | else 17 | return GCD(num2%num1, num1); 18 | } 19 | 20 | 21 | //多个数的公倍数 22 | int LCM1(int Array[]) 23 | { 24 | int x, y, num = Array[0], gcd; 25 | for (int i = 0; i < sizeof(Array); i++) 26 | { 27 | x = num; y = Array[i+1]; 28 | gcd = GCD(x, y); 29 | num = (x * y) / gcd; 30 | } 31 | return num; 32 | } 33 | 34 | 35 | 36 | //最小公倍数 37 | int LCM(int num1, int num2) 38 | { 39 | int gcd = GCD(num1, num2); 40 | return (num1 * num2) / gcd; 41 | } 42 | void main() 43 | { 44 | int num1, num2; 45 | cin >> num1; 46 | cin >> num2; 47 | cout << LCM(num1, num2) << endl; 48 | } 49 | -------------------------------------------------------------------------------- /字符统计.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct Count 6 | { 7 | int c; 8 | int count; 9 | }; 10 | bool cmp(const Count &a, const Count &b) 11 | { 12 | return a.count>b.count; 13 | } 14 | 15 | void Solution(char* str) 16 | { 17 | struct Count a[256]; 18 | for (int i = 0; i<256; i++) 19 | { 20 | a[i].c = i; 21 | a[i].count = 0; 22 | } 23 | 24 | for (int i = 0; str[i] != '\0'; i++) 25 | { 26 | if ((str[i] >= 'a'&&str[i] <= 'z') || (str[i] >= 'A'&&str[i] <= 'Z') || (str[i] >= '0'&&str[i] <= '9') || str[i] == ' ') 27 | { 28 | char tmp = str[i]; 29 | a[tmp].count++; 30 | } 31 | } 32 | sort(a, a + 256, cmp); 33 | for (int i = 0; i<256; i++) 34 | { 35 | if (a[i].count != 0) 36 | cout << char(a[i].c); 37 | } 38 | } 39 | 40 | void main() 41 | { 42 | char str[100]; 43 | cin.getline(str, 100); 44 | Solution(str); 45 | } 46 | -------------------------------------------------------------------------------- /在字符串中找出连续最长的数字串.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int Continuemax(char a[], char*&s) 4 | { 5 | int max = 0,maxTemp = 0, index, length = strlen(a); 6 | for (int i = 0; i < length; i++) 7 | { 8 | int flag = i; 9 | while (a[flag] >= '0' && a[flag] <= '9' && a[flag]) 10 | { 11 | flag++; 12 | maxTemp++; 13 | } 14 | if (maxTemp > max) 15 | { 16 | max = maxTemp; 17 | index = i; 18 | } 19 | maxTemp = 0; 20 | } 21 | s = new char[max + 1]; 22 | for (int i = 0; i < max; i++) 23 | s[i] = a[i + index]; 24 | s[max] = '\0'; 25 | return max; 26 | } 27 | 28 | int main() 29 | { 30 | char array[100]; 31 | char *s; 32 | cin >> array; 33 | int length = Continuemax(array, s); 34 | if (length == 0) 35 | cout << '0' << endl; 36 | else 37 | { 38 | for (int i = 0; i 2 | using namespace std; 3 | 4 | void main() 5 | { 6 | char str[100]; 7 | cin.getline(str, 100); 8 | for (int i = 0; i < strlen(str); i++) 9 | { 10 | if (str[i] >= 'A' && str[i] < 'Z') 11 | cout << char(str[i] + 33); 12 | else if (str[i] == 'Z') 13 | cout << 'a'; 14 | else if (str[i] >= 'a' && str[i] <= 'c') 15 | cout << '2'; 16 | else if (str[i] >= 'd' && str[i] <= 'f') 17 | cout << '3'; 18 | else if (str[i] >= 'g' && str[i] <= 'i') 19 | cout << '4'; 20 | else if (str[i] >= 'j' && str[i] <= 'l') 21 | cout << '5'; 22 | else if (str[i] >= 'm' && str[i] <= '0') 23 | cout << '6'; 24 | else if (str[i] >= 'p' && str[i] <= 's') 25 | cout << '7'; 26 | else if (str[i] >= 't' && str[i] <= 'v') 27 | cout << '8'; 28 | else if (str[i] >= 'w' && str[i] <= 'z') 29 | cout << '9'; 30 | else 31 | cout << str[i]; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /素数伴侣.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | //#include 4 | using namespace std; 5 | 6 | bool Isprime(int num) 7 | { 8 | if (num == 1) return false; 9 | else if (num == 2) return true; 10 | for (int i = 2; i < num; i++) 11 | { 12 | if (num % i == 0) return false; 13 | } 14 | return true; 15 | } 16 | 17 | bool Primer_patener(int num1, int num2) 18 | { 19 | if (Isprime(num1 + num2)) 20 | return true; 21 | else 22 | return false; 23 | } 24 | 25 | void main() 26 | { 27 | int N, num[100], max = 0; 28 | cin >> N; 29 | if (N % 2 != 0 || N > 100) 30 | return; 31 | for (int i = 0; i < N; i++) 32 | cin >> num[i]; 33 | 34 | sort(num, num + N); 35 | 36 | while (next_permutation(num, num + N)) //穷举每一种组合 37 | { 38 | int k = 0; 39 | for (int j = 0; j 2 | using namespace std; 3 | 4 | int Solution(char str1[], char str2[]) 5 | { 6 | int len1 = strlen(str1); 7 | int len2 = strlen(str2); 8 | if (len1 <= 0 || len2 <= 0) return 0; 9 | 10 | int start1, start2; 11 | int max = 0; int count = 0; 12 | 13 | for (int i = 0; i < len1; i++) 14 | { 15 | for (int j = 0; j < len2; j++) 16 | { 17 | start1 = i; start2 = j; 18 | while (str1[start1] == str2[start2] && start1 < len1 && start2 < len2) 19 | { 20 | start1++; start2++; count++; 21 | } 22 | if (count > max) max = count; 23 | count = 0; 24 | } 25 | } 26 | return max; 27 | } 28 | 29 | void main() 30 | { 31 | char str1[100]; 32 | scanf_s("%s", &str1, 100); 33 | int Len = strlen(str1); 34 | char str2[100]; int i = 0, j = Len - 1; 35 | while (str1[i] != '\0') 36 | str2[j--] = str1[i++]; 37 | str2[Len] = '\0'; 38 | cout << Solution(str1, str2) << endl; 39 | } 40 | -------------------------------------------------------------------------------- /合唱队.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | int n, stature[100], result = 0; 8 | cin >> n; 9 | for (int i = 0; i < n; i++) 10 | cin >> stature[i]; 11 | stature[n] = '\0'; 12 | 13 | int *listrise = new int[n]; //上升子序列 14 | 15 | for (int i = 0; i < n; i++) 16 | { 17 | listrise[i] = 1; 18 | for (int j = 0; j < i; j++) 19 | if ((stature[i] > stature[j]) && (listrise[j] + 1 > listrise[i])) 20 | listrise[i] = listrise[j] + 1; 21 | } 22 | 23 | int *listdown = new int[n]; 24 | for (int i = n - 1; i >= 0; i--) 25 | { 26 | listdown[i] = 1; 27 | for (int j = n - 1; j > i; j--) 28 | if ((stature[i] > stature[j]) && (listdown[j] + 1> listdown[i])) 29 | listdown[i] = listdown[j] + 1; 30 | } 31 | 32 | for (int i = 0; i < n; i++) 33 | if (listrise[i] + listdown[i] - 1> result) 34 | result = listrise[i] + listdown[i] - 1; 35 | cout << n - result << endl; 36 | } 37 | -------------------------------------------------------------------------------- /进制转换.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | char* Solution(char *str) 5 | { 6 | if (str == NULL || *str != '0' || *(++str) != 'x') 7 | return NULL; 8 | if (strlen(str) < 3) 9 | return NULL; 10 | int num = 0; int temp = 0; 11 | ++str; 12 | while (*str != '\0') 13 | { 14 | if (!((0 < (*str - '0') && (*str - '0') < 10) || (0 < (*str - 'a') && (*str - 'a') < 5) || (0 < (*str - 'A') && (*str - 'A') < 5))) 15 | break; 16 | if (0 < (*str - '0') && (*str - '0') < 10) 17 | temp = (*str - '0'); 18 | if (0 < (*str - 'a') && (*str - 'a') < 5) 19 | temp = (*str - 'a') + 10; 20 | if (0 < (*str - 'A') && (*str - 'A') < 5) 21 | temp = (*str - 'A') + 10; 22 | num = num * 16 + temp; 23 | ++str; 24 | } 25 | char* str0 = (char*)malloc(33); 26 | _itoa_s(num, str0, 10, 10); 27 | return str0; 28 | } 29 | 30 | void main() 31 | { 32 | char str[1000]; 33 | cin >> str; 34 | cout << Solution(str) << endl; 35 | } 36 | -------------------------------------------------------------------------------- /公共字串计算.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solution(char str1[], char str2[]) 5 | { 6 | int len1 = strlen(str1); 7 | int len2 = strlen(str2); 8 | if (len1 <= 0 || len2 <= 0) return 0; 9 | 10 | int start1, start2; 11 | int max = 0; int count = 0; 12 | 13 | for (int i = 0; i < len1; i++) 14 | for (int j = 0; j < len2; j++) 15 | { 16 | start1 = i; start2 = j; 17 | while (str1[start1] == str2[start2] && start1 < len1 && start2 < len2) 18 | { 19 | start1++; start2++; count++; 20 | } 21 | if (count > max) max = count; 22 | count = 0; 23 | } 24 | return max; 25 | } 26 | 27 | void main() 28 | { 29 | char str1[100], str2[100]; 30 | scanf_s("%s", &str1, 100); 31 | scanf_s("%s", &str2, 100); 32 | for (int i = 0; i < strlen(str1); i++) 33 | str1[i] = tolower(str1[i]); 34 | for (int i = 0; i < strlen(str2); i++) 35 | str2[i] = tolower(str2[i]); 36 | cout << Solution(str1, str2) << endl; 37 | } 38 | -------------------------------------------------------------------------------- /计算字符串的相似度.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int Min(int x, int y) 5 | { 6 | return (x>y) ? y : x; 7 | } 8 | 9 | int StringDistance(string A, int pa, int lenA, string B, int pb, int lenB) 10 | { 11 | if (pa >= lenA) // A串为空时 12 | { 13 | if (pb >= lenB) 14 | return 0; 15 | else 16 | return lenB - pb; 17 | } 18 | if (pb >= lenB) // B串为空时 19 | { 20 | if (pa >= lenA) 21 | return 0; 22 | else 23 | return lenA - pa; 24 | } 25 | 26 | if (A[pa] == B[pb]) 27 | return StringDistance(A, pa + 1, lenA, B, pb + 1, lenB); 28 | else 29 | { 30 | int t1 = StringDistance(A, pa + 1, lenA, B, pb, lenB); 31 | int t2 = StringDistance(A, pa + 1, lenA, B, pb + 1, lenB); 32 | int t3 = StringDistance(A, pa, lenA, B, pb + 1, lenB); 33 | return Min(Min(t1, t2), t3) + 1; 34 | } 35 | } 36 | 37 | int main() 38 | { 39 | string a, b; 40 | cin >> a >> b; 41 | int distance = StringDistance(a, 0, a.size(), b, 0, b.size()); 42 | cout << "1/" << distance + 1 << endl; 43 | } 44 | -------------------------------------------------------------------------------- /单词倒排.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void Reverse(char* pBegin, char* pEnd) 5 | { 6 | if (pBegin == NULL || pEnd == NULL) 7 | return; 8 | 9 | while (pBegin < pEnd) 10 | { 11 | char temp = *pBegin; 12 | *pBegin = *pEnd; 13 | *pEnd = temp; 14 | 15 | pBegin++; 16 | pEnd--; 17 | } 18 | } 19 | 20 | char* ReverseSentence(char* str) 21 | { 22 | if (str == NULL) return NULL; 23 | 24 | char* pBegin = str; 25 | char *pEnd = str; 26 | while (*pEnd != '\0') pEnd++; 27 | pEnd--; 28 | 29 | //翻转整个句子 30 | Reverse(pBegin, pEnd); 31 | 32 | //翻转句子中每个单词 33 | pBegin = pEnd = str; 34 | while (*pBegin != '\0') 35 | { 36 | if (*pBegin == ' ') 37 | { 38 | pBegin++; 39 | pEnd++; 40 | } 41 | else if (*pEnd == ' ' || *pEnd == '\0') 42 | { 43 | Reverse(pBegin, --pEnd); 44 | pBegin = ++pEnd; 45 | } 46 | else 47 | { 48 | pEnd++; 49 | } 50 | } 51 | return str; 52 | } 53 | 54 | 55 | void main() 56 | { 57 | char str[1000]; 58 | cin.getline(str, 1000); 59 | cout << ReverseSentence(str) << endl; 60 | } 61 | -------------------------------------------------------------------------------- /句子逆序.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void Reverse(char* pBegin, char* pEnd) 5 | { 6 | if (pBegin == NULL || pEnd == NULL) 7 | return; 8 | 9 | while (pBegin < pEnd) 10 | { 11 | char temp = *pBegin; 12 | *pBegin = *pEnd; 13 | *pEnd = temp; 14 | 15 | pBegin++; 16 | pEnd--; 17 | } 18 | } 19 | 20 | char* ReverseSentence(char* str) 21 | { 22 | if (str == NULL) return NULL; 23 | 24 | char* pBegin = str; 25 | char *pEnd = str; 26 | while (*pEnd != '\0') pEnd ++; 27 | pEnd--; 28 | 29 | //翻转整个句子 30 | Reverse(pBegin, pEnd); 31 | 32 | //翻转句子中每个单词 33 | pBegin = pEnd = str; 34 | while (*pBegin != '\0') 35 | { 36 | if (*pBegin == ' ') 37 | { 38 | pBegin ++; 39 | pEnd ++; 40 | } 41 | else if (*pEnd == ' ' || *pEnd == '\0') 42 | { 43 | Reverse(pBegin, --pEnd); 44 | pBegin = ++pEnd; 45 | } 46 | else 47 | { 48 | pEnd ++; 49 | } 50 | } 51 | return str; 52 | } 53 | 54 | 55 | void main() 56 | { 57 | char str[1000]; 58 | cin.getline(str, 1000); 59 | cout << ReverseSentence(str) << endl; 60 | } 61 | -------------------------------------------------------------------------------- /合法IP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | void Solution(char* str) 6 | { 7 | if (str == NULL) return; 8 | int count = 0, i = 0; 9 | char *p = str; 10 | for (int i = 0; i < strlen(str); i++) 11 | if (str[i] == '.' && str[i + 1] != '.') 12 | count++; 13 | 14 | char *p2 = str; 15 | if (count == 3) 16 | { 17 | int k = 0, temp = 0, flag = 0; 18 | for (int j = 0; j < 4; j++) 19 | { 20 | while (*p2 != '.' && *p2 != '\0') 21 | { 22 | if (*p2 >= '0' && *p2 <= '9') 23 | { 24 | temp = temp * 10 + *p2 - '0'; 25 | } 26 | //else 27 | //{ 28 | // cout << "NO1" << endl; 29 | // break; 30 | //} 31 | ++p2; 32 | } 33 | if (temp < 0 || temp > 255) 34 | { 35 | cout << "NO" << endl; 36 | break; 37 | } 38 | else 39 | flag++; 40 | 41 | temp = 0; ++p2; 42 | } 43 | if (flag == 4) 44 | cout << "YES" << endl; 45 | } 46 | else 47 | cout << "NO" << endl; 48 | 49 | 50 | } 51 | 52 | void main() 53 | { 54 | char str[500]; 55 | cin >> str; 56 | Solution(str); 57 | } 58 | -------------------------------------------------------------------------------- /输出单向链表中倒数第k个.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef int ElemType; 4 | struct ListNode 5 | { 6 | ListNode *next; 7 | ElemType data; 8 | }; 9 | 10 | int FindKthToTail(ListNode* pListHead, unsigned int k) 11 | { 12 | ListNode *pAhead = pListHead; 13 | ListNode *pBehind = NULL; 14 | 15 | for (unsigned int i = 0; i < k; i++) 16 | { 17 | pAhead = pAhead->next; 18 | } 19 | 20 | pBehind = pListHead; 21 | 22 | while (pAhead->next != NULL) 23 | { 24 | pAhead = pAhead->next; 25 | pBehind = pBehind->next; 26 | } 27 | return pBehind->data; 28 | } 29 | 30 | void main() 31 | { 32 | ListNode *pListNode, *PListNode, *qListNode; 33 | int num, count = 0, k; 34 | pListNode = PListNode = new ListNode; 35 | cin >> num; 36 | for (int i = 0; i> qListNode->data; 40 | pListNode->next = qListNode; 41 | pListNode = qListNode; 42 | } 43 | pListNode->next = NULL; 44 | cin >> k; //输入要输出的位置 45 | ListNode *head = PListNode->next; 46 | 47 | cout << FindKthToTail(head, k) << endl; 48 | } 49 | -------------------------------------------------------------------------------- /参数解析.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void Solution(string str) 6 | { 7 | int count = 1, flag = 0; 8 | char para[100][100]; 9 | for (int i = 1; i < str.size(); i++) 10 | { 11 | if (str[i] == ' ' && str[i - 1] != ' ' && flag == 0) 12 | count++; 13 | if (str[i] == '"') flag = 1; 14 | if (str[i] == ' ' && str[i - 1] == '"' && flag == 1) 15 | count++; 16 | } 17 | int i = 0, j = 0, k = 0; 18 | int P = count; 19 | while (P--) 20 | { 21 | while (str[i] != '"' && str[i] != ' ') 22 | { 23 | para[j][k++] = str[i]; 24 | i++; 25 | } 26 | para[j][k] = '\0'; 27 | 28 | if (str[i-1] == '"' && str[i-2] == ' ') 29 | while (str[i] != '"') 30 | para[j][k++] = str[i]; 31 | para[j][k] = '\0'; 32 | i++; j++; 33 | } 34 | 35 | cout << count << endl; 36 | for (int i = 0; i < count; i++) 37 | { 38 | int j = 0; 39 | while (para[i][j] != '\0') 40 | cout << para[i][j] ; 41 | cout << endl; 42 | } 43 | } 44 | 45 | void main() 46 | { 47 | string str; 48 | getline(cin, str); 49 | Solution(str); 50 | } 51 | -------------------------------------------------------------------------------- /字符串加密.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void Solution(char* key, char* data, char* encrypt) 5 | { 6 | int keych[26] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, keychT[26] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 }; 7 | int keyLen = strlen(key); 8 | int offsetKey = key[0] - '^' > 0 ? 32 : 0; 9 | for (int i = 0; i < keyLen; i++) 10 | { 11 | int keyValue = key[i] - 'A' - offsetKey; 12 | if (keych[i] == 0) 13 | { 14 | keychT[keyValue] = -1; 15 | keych[i] = keyValue; 16 | } 17 | } 18 | int j = 0; 19 | for (int i = keyLen; i < 26; i++) 20 | { 21 | while (keychT[j] == -1) j++; 22 | keych[i] = keychT[j]; 23 | j++; 24 | } 25 | 26 | int offsetData = data[0] - '^' > 0 ? 32 : 0; 27 | int index = 0; 28 | for (index; index < strlen(data); index++) 29 | { 30 | encrypt[index] = char(keych[data[index] - 'A' - offsetData] + 'A' + offsetData); 31 | } 32 | encrypt[index] = '\0'; 33 | } 34 | 35 | 36 | void main() 37 | { 38 | char key[100], data[100], encrypt[100]; 39 | cin >> key; 40 | cin >> data; 41 | Solution(key, data, encrypt); 42 | cout << encrypt << endl; 43 | 44 | } 45 | -------------------------------------------------------------------------------- /超长正整数相加.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define BIGINTRADIX 10000 5 | #define RADIX 4 6 | #define MAX_LEN (100/RADIX + 1)*BIGINTRADIX 7 | 8 | char m[MAX_LEN * RADIX], n[MAX_LEN * RADIX]; 9 | int a[MAX_LEN] = { 0 }, b[MAX_LEN] = { 0 }, c[MAX_LEN + 1] = { 0 }; 10 | 11 | void Solution(char *s1, char *s2) 12 | { 13 | int i, j, k, temp, low; 14 | int len1 = strlen(s1), len2 = strlen(s2); 15 | 16 | j = 0; 17 | for (i = len1; i > 0; i -= RADIX) 18 | { 19 | temp = 0; 20 | low = i - RADIX > 0 ? i - RADIX : 0; 21 | for (k = low; k < i; k++) temp = temp * 10 + s1[k] - '0'; 22 | a[j++] = temp; 23 | } 24 | j = 0; 25 | for (i = len2; i > 0; i -= RADIX) 26 | { 27 | temp = 0; 28 | low = i - RADIX > 0 ? i - RADIX : 0; 29 | for (k = low; k < i; k++) temp = temp * 10 + s2[k] - '0'; 30 | b[j++] = temp; 31 | } 32 | 33 | for (int i = 0; i < MAX_LEN + 1; i++) c[i] = 0; 34 | for (i = 0; i < MAX_LEN; i++) 35 | { 36 | c[i] += a[i] + b[i]; 37 | if (c[i]>=BIGINTRADIX) 38 | { 39 | c[i] = c[i] % BIGINTRADIX; 40 | c[i+1] = c[i + 1] + 1; 41 | } 42 | } 43 | 44 | int flag = 0; 45 | for (i = MAX_LEN - 1; i >= 0; --i) 46 | { 47 | if (flag) 48 | { 49 | printf("%04d", c[i]); 50 | } 51 | else if (c[i] > 0) 52 | { 53 | flag = 1; 54 | printf("%d", c[i]); 55 | } 56 | } 57 | } 58 | 59 | int main() 60 | { 61 | scanf_s("%s", m, 100); 62 | scanf_s("%s", n, 100); 63 | Solution(m, n); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /寻找等差数列.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | bool Isprime(int n) 7 | { 8 | int flag = 1; 9 | if (n<2) 10 | return false; 11 | if (n == 2) 12 | return true; 13 | for (int i = 2; i <= sqrt((double)n); i++) 14 | if (n%i == 0) 15 | { 16 | flag = 0; 17 | break; 18 | } 19 | if (flag) 20 | return true; 21 | else 22 | return false; 23 | } 24 | 25 | bool find(int m, int a[], int n) 26 | { 27 | int flag = 0; 28 | for (int i = 0; i d; 41 | cin >> min >> max; 42 | for (int i = min; i <= max; i++) 43 | if (Isprime(i)) 44 | a[k++] = i; //所有的素数存起来 45 | for (int i = 0; i::iterator it = unique(d.begin(), d.end()); 50 | d.erase(it, d.end()); //所有的不重复的公差存起来 51 | 52 | for (int i = 0; ilen || (count == len &&d[j]>dk)) 63 | { 64 | len = count; 65 | end = a[i]; 66 | dk = d[j]; 67 | 68 | } 69 | count = 1; 70 | } 71 | 72 | for (int i = 0; i 2 | using namespace std; 3 | void Encrypt(char aucPassword[], char aucResult[]) 4 | { 5 | int len = strlen(aucPassword); 6 | for (int i = 0; i= 'A'&&aucPassword[i]<'Z')//大写字母时 9 | aucResult[i] = (aucPassword[i] + 33); 10 | else if (aucPassword[i] >= 'a'&&aucPassword[i]<'z')//小写字母时 11 | aucResult[i] = (aucPassword[i] - 31); 12 | else if (aucPassword[i] == 'Z') //特殊情况 13 | aucResult[i] = 'a'; 14 | else if (aucPassword[i] == 'z') 15 | aucResult[i] = 'A'; 16 | else if (aucPassword[i] >= '0'&&aucPassword[i]<'9') 17 | aucResult[i] = aucPassword[i] + 1; 18 | else if (aucPassword[i] == '9') 19 | aucResult[i] = '0'; 20 | } 21 | aucResult[len] = '\0'; 22 | } 23 | 24 | 25 | void unEncrypt(char aucPassword[], char aucResult[]) 26 | { 27 | 28 | int len = strlen(aucPassword); 29 | for (int i = 0; i'A'&&aucPassword[i] <= 'Z')//大写字母时 32 | aucResult[i] = (aucPassword[i] + 31); 33 | else if (aucPassword[i]>'a'&&aucPassword[i] <= 'z')//小写字母时 34 | aucResult[i] = (aucPassword[i] - 33); 35 | else if (aucPassword[i] == 'A') //特殊情况 36 | aucResult[i] = 'z'; 37 | else if (aucPassword[i] == 'a') 38 | aucResult[i] = 'Z'; 39 | else if (aucPassword[i]>'0'&&aucPassword[i] <= '9') 40 | aucResult[i] = aucPassword[i] - 1; 41 | else if (aucPassword[i] == '0') 42 | aucResult[i] = '9'; 43 | } 44 | aucResult[len] = '\0'; 45 | } 46 | 47 | int main() 48 | { 49 | char source1[100], result1[100], source2[100], result2[100]; 50 | cin >> source1; 51 | cin >> source2; 52 | Encrypt(source1, result1); 53 | unEncrypt(source2, result2); 54 | cout << result1 << endl; 55 | cout << result2 << endl; 56 | } 57 | -------------------------------------------------------------------------------- /整型数组合并.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool compare(const int &a, const int &b) 6 | { 7 | return a b[q]) 52 | { 53 | c[k] = b[q]; 54 | k++; q++; 55 | } 56 | } 57 | if (p < lenA) 58 | { 59 | for (int i = p; i < lenA; i++) 60 | { 61 | c[k] = a[i]; 62 | k++; 63 | } 64 | } 65 | if (q < lenB) 66 | { 67 | for (int i = q; i < lenB; i++) 68 | { 69 | c[k] = b[i]; 70 | k++; 71 | } 72 | } 73 | 74 | for (int i = 0; i> m; //第一个数组的个数 83 | for (int i = 0; i < m; i++) 84 | { 85 | int number; 86 | cin >> number; 87 | a[i] = number; 88 | } 89 | cin >> n; //第二个数组的个数 90 | for (int i = 0; i < n; i++) 91 | { 92 | int number; 93 | cin >> number; 94 | b[i] = number; 95 | } 96 | 97 | Solution(a, m, b, n, c); 98 | } 99 | -------------------------------------------------------------------------------- /判断两个IP是否属于同一个子网.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void CharToInt(char* str, int* Int) 5 | { 6 | int i = 0, k = 0, temp = 0; 7 | while (str[i] != '\0' && i < strlen(str)) 8 | { 9 | while (str[i] != '.' && str[i] != '\0') 10 | { 11 | temp = temp * 10 + str[i] - '0'; 12 | i++; 13 | } 14 | Int[k++] = temp; 15 | temp = 0; i++; 16 | } 17 | } 18 | 19 | 20 | bool IpLegal(char* str) 21 | { 22 | if (str == NULL) return false; 23 | int count = 0, i = 0; 24 | char *p = str; 25 | for (int i = 0; i < strlen(str); i++) 26 | if (str[i] == '.' && str[i + 1] != '.') 27 | count++; 28 | 29 | char *p2 = str; 30 | if (count == 3) 31 | { 32 | int k = 0, temp = 0, flag = 0; 33 | for (int j = 0; j < 4; j++) 34 | { 35 | while (*p2 != '.' && *p2 != '\0') 36 | { 37 | if (*p2 >= '0' && *p2 <= '9') 38 | { 39 | temp = temp * 10 + *p2 - '0'; 40 | } 41 | //else 42 | //{ 43 | // cout << "NO1" << endl; 44 | // break; 45 | //} 46 | ++p2; 47 | } 48 | if (temp < 0 || temp > 255) 49 | { 50 | return false; 51 | break; 52 | } 53 | else 54 | flag++; 55 | 56 | temp = 0; ++p2; 57 | } 58 | if (flag == 4) 59 | return true; 60 | } 61 | else 62 | return false; 63 | 64 | 65 | } 66 | 67 | char Solution(char* mask, char* ip1, char* ip2) 68 | { 69 | if (!IpLegal(mask) || !IpLegal(ip1) || !IpLegal(ip2)) 70 | return '1'; 71 | 72 | int maskArray[4], ip1Array[4], ip2Array[4]; 73 | CharToInt(mask, maskArray); 74 | CharToInt(ip1, ip1Array); 75 | CharToInt(ip2, ip2Array); 76 | 77 | int temp1[4], temp2[4]; 78 | for (int i = 0; i < 4; i++) 79 | { 80 | temp1[i] = maskArray[i] & ip1Array[i]; 81 | temp2[i] = maskArray[i] & ip2Array[i]; 82 | if (temp1[i] != temp2[i]) 83 | return '2'; 84 | } 85 | return '0'; 86 | } 87 | void main() 88 | { 89 | char Mask[20], ip1[20], ip2[20]; 90 | cin >> Mask; 91 | cin >> ip1; 92 | cin >> ip2; 93 | cout << Solution(Mask, ip1, ip2) << endl; 94 | } 95 | -------------------------------------------------------------------------------- /表示数字.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | string Solution1(string str) 6 | { 7 | int length = str.size(); 8 | for (int i = 0; i arraySize) return NULL; 51 | 52 | int indexOriginal = originalLength; 53 | int indexNew = newLength; 54 | int flag = 0; 55 | while (indexOriginal >= 0 && indexOriginal < indexNew ) 56 | { 57 | if (('0' <= str[indexOriginal] && str[indexOriginal] <= '9') && flag == 0) 58 | { 59 | str[indexNew--] = '*'; 60 | str[indexNew--] = str[indexOriginal--]; 61 | flag = 1; 62 | continue; 63 | } 64 | if (!('0' <= str[indexOriginal] && str[indexOriginal] <= '9') && flag == 1) 65 | { 66 | str[indexNew--] = '*'; 67 | str[indexNew--] = str[indexOriginal--]; 68 | flag = 0; 69 | continue; 70 | } 71 | 72 | str[indexNew--] = str[indexOriginal--]; 73 | } 74 | return str; 75 | } 76 | 77 | 78 | int main() 79 | { 80 | //方案1 用string 81 | string str1; 82 | //方案2 字符串 83 | //char str2[500]; 84 | 85 | cin >> str1; 86 | //cin >> str2; 87 | cout << Solution1(str1) << endl; 88 | //cout << Solution2(str2, 500) << endl; 89 | }; 90 | 91 | -------------------------------------------------------------------------------- /从单向链表中删除指定值.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef struct node 4 | { 5 | int number; 6 | struct node *next; 7 | }Node; 8 | void ListNew(Node *head, int mynumber) 9 | { 10 | Node *nextnode = (Node *)malloc(sizeof(Node)); 11 | nextnode->next = NULL;//考虑容错 12 | nextnode->number = mynumber; 13 | if (head->next != NULL) 14 | { 15 | nextnode->next = head->next; 16 | head->next = nextnode; 17 | } 18 | else//头节点的后继指针NULL,直接添加即可 19 | { 20 | head->next = nextnode; 21 | } 22 | } 23 | Node *Delete(Node *head, int key) 24 | { 25 | Node *node1 = head; 26 | Node *node2 = NULL; 27 | if (head == NULL) 28 | { 29 | return NULL; 30 | } 31 | else 32 | { 33 | if (node1->number == key) 34 | { 35 | head = head->next; 36 | free(node1); 37 | return head; 38 | } 39 | else 40 | { 41 | while (node1 != NULL) 42 | { 43 | node2 = node1; 44 | node2 = node2->next; 45 | if (node2->number == key) 46 | { 47 | node1->next = node2->next; 48 | free(node2); 49 | break; 50 | } 51 | node1 = node1->next; 52 | } 53 | return head; 54 | } 55 | } 56 | } 57 | int main() 58 | { 59 | Node *head = (Node*)malloc(sizeof(Node)); 60 | Node *p, *q, *q1; 61 | int key; 62 | p = (Node*)malloc(sizeof(Node)); 63 | q1 = q = head; 64 | int nodenum; 65 | cin >> nodenum;//待输入的节点数量 66 | getchar(); 67 | cin >> head->number;//头节点的值 68 | head->next = NULL; 69 | int h, t; 70 | for (int i = 1; i> t >> h; 73 | //先进行查找,输入格式中的头节点的位置 74 | while (q != NULL) 75 | { 76 | if (q->number != h) 77 | { 78 | q = q->next; 79 | } 80 | else if (q->number == h)//直接在首位 81 | { 82 | ListNew(q, t); 83 | q = head; 84 | break; 85 | } 86 | }//还需要增加容错 87 | } 88 | 89 | cin >> key; 90 | p = Delete(q, key); 91 | /*cout<<"删除一个"<number << " "; 95 | p = p->next; 96 | } 97 | //cout< 2 | #include 3 | using namespace std; 4 | 5 | void Solution(string Password) 6 | { 7 | int countEnglihCharH = 0, countEnglihCharL = 0, countNumber = 0, countOtherChar = 0; 8 | int len = Password.size(); 9 | 10 | for (int i = 0; i < len; i++) 11 | { 12 | if (Password[i] >= 'A' && Password[i] <= 'Z') 13 | countEnglihCharH++; 14 | else if (Password[i] >= 'a' && Password[i] <= 'z') 15 | countEnglihCharL++; 16 | else if (Password[i] >= '0' && Password[i] <= '9') 17 | countNumber++; 18 | else 19 | countOtherChar++; 20 | } 21 | // 22 | int sum = 0; 23 | if (len <= 4) sum += 5; 24 | else if (len >= 5 && len <= 7) 25 | sum += 10; 26 | else if (len >= 8) 27 | sum += 25; 28 | // 29 | if (countEnglihCharH == 0 && countEnglihCharL == 0) 30 | sum += 0; 31 | else if ((countEnglihCharH > 0 && countEnglihCharL == 0) || (countEnglihCharH == 0 && countEnglihCharL > 0)) 32 | sum += 10; 33 | else if (countEnglihCharH > 0 && countEnglihCharL > 0) 34 | sum += 20; 35 | // 36 | if (countNumber == 0) sum += 0; 37 | else if (countNumber == 1) 38 | sum += 10; 39 | else if (countNumber >= 2) 40 | sum += 20; 41 | // 42 | if (countOtherChar == 0) sum += 0; 43 | else if (countOtherChar == 1) 44 | sum += 10; 45 | else if (countOtherChar >= 2) 46 | sum += 25; 47 | // 48 | int countEnglihChar = countEnglihCharH + countEnglihCharL; 49 | if (countEnglihChar > 0 && countNumber > 0) 50 | sum += 2; 51 | else if (countEnglihChar > 0 && countNumber > 0 && countOtherChar > 0) 52 | sum += 3; 53 | else if (countEnglihCharH > 0 && countEnglihCharL > 0 && countNumber > 0 && countOtherChar > 0) 54 | sum += 5; 55 | 56 | if (sum >= 90) 57 | cout << "VERY_SECURE" << endl; 58 | else if (sum >= 80) 59 | cout << "SECURE" << endl; 60 | else if (sum >= 70) 61 | cout << "VERY_STRONG" << endl; 62 | else if (sum >= 60) 63 | cout << "STRONG" << endl; 64 | else if (sum >= 50) 65 | cout << "AVERAGE" << endl; 66 | else if (sum >= 25) 67 | cout << "WEAK" << endl; 68 | else if (sum >= 0) 69 | cout << "VERY_WEAK" << endl; 70 | 71 | } 72 | 73 | int main() 74 | { 75 | string str; 76 | getline(cin, str); 77 | Solution(str); 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /表达式求值.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "stdlib.h" 5 | using namespace std; 6 | int pri(char m) 7 | { 8 | switch (m) 9 | { 10 | case '+': 11 | case '-': 12 | return 1; 13 | case '*': 14 | case '/': 15 | return 2; 16 | case '(': 17 | case ')': 18 | return 0; 19 | case '#': 20 | return -1; 21 | default: 22 | break; 23 | } 24 | } 25 | int getResult(string s) 26 | { 27 | stackvalue; 28 | stackfuhao; 29 | s = s + "#$"; 30 | if (s[0] == '-') 31 | s = "0" + s; 32 | int i = 0; 33 | string c = ""; 34 | while (s[i] != '$') 35 | { 36 | if (s[i] <= '9'&&s[i] >= '0') 37 | { 38 | c = c + s[i]; 39 | i++; 40 | while (s[i] <= '9'&&s[i] >= '0') 41 | { 42 | c = c + s[i]; 43 | i++; 44 | } 45 | i--; 46 | int m = atoi(c.c_str()); 47 | value.push(m); 48 | c = ""; 49 | } 50 | 51 | else if (fuhao.size() == 0 || s[i] == '(' || pri(s[i]) > pri(fuhao.top())) 52 | fuhao.push(s[i]); 53 | else if (s[i] == ')') 54 | { 55 | for (; fuhao.top() != '('; fuhao.pop()) 56 | { 57 | char d = fuhao.top(); 58 | int a = value.top(); 59 | value.pop(); 60 | int b = value.top(); 61 | value.pop(); 62 | switch (d) 63 | { 64 | case '+': 65 | { 66 | a = a + b; 67 | break; 68 | } 69 | case '-': 70 | { 71 | a = b - a; 72 | break; 73 | } 74 | case '*': 75 | { 76 | a = a * b; 77 | break; 78 | } 79 | case '/': 80 | { 81 | a = b / a; 82 | break; 83 | } 84 | default: 85 | break; 86 | } 87 | value.push(a); 88 | } 89 | fuhao.pop(); 90 | } 91 | else 92 | { 93 | for (; fuhao.size() != 0 && pri(s[i]) <= pri(fuhao.top()); fuhao.pop()) 94 | { 95 | char d = fuhao.top(); 96 | int a = value.top(); 97 | value.pop(); 98 | int b = value.top(); 99 | value.pop(); 100 | switch (d) 101 | { 102 | case '+': 103 | { 104 | a = a + b; 105 | break; 106 | } 107 | case '-': 108 | { 109 | a = b - a; 110 | break; 111 | } 112 | case '*': 113 | { 114 | a = a*b; 115 | break; 116 | } 117 | case '/': 118 | { 119 | a = b / a; 120 | break; 121 | } 122 | } 123 | value.push(a); 124 | } 125 | fuhao.push(s[i]); 126 | } 127 | i++; 128 | } 129 | return value.top(); 130 | } 131 | 132 | bool calculating(string str) 133 | { 134 | 135 | cout << getResult(str) << endl; 136 | return true; 137 | } 138 | 139 | int main(void) 140 | { 141 | string a; 142 | cin >> a; 143 | if (calculating(a)) 144 | { 145 | cout << "true" << endl; 146 | } 147 | return 0; 148 | } 149 | -------------------------------------------------------------------------------- /学英语.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | string NUmberToEnglishString(long int number) 5 | { 6 | string s; 7 | if (number<0) 8 | { 9 | s = "error"; 10 | return s; 11 | } 12 | if (number<20) 13 | { 14 | switch (number) 15 | { 16 | case 0: 17 | s = "zero"; 18 | return s; 19 | case 1: 20 | s = "one"; 21 | return s; 22 | case 2: 23 | s = "two"; 24 | return s; 25 | case 3: 26 | s = "three"; 27 | return s; 28 | case 4: 29 | s = "four"; 30 | return s; 31 | case 5: 32 | s = "five"; 33 | return s; 34 | case 6: 35 | s = "six"; 36 | return s; 37 | case 7: 38 | s = "seven"; 39 | return s; 40 | case 8: 41 | s = "eight"; 42 | return s; 43 | case 9: 44 | s = "nine"; 45 | return s; 46 | case 10: 47 | s = "ten"; 48 | return s; 49 | case 11: 50 | s = "eleven"; 51 | return s; 52 | case 12: 53 | s = "twelve"; 54 | return s; 55 | case 13: 56 | s = "thirteen"; 57 | return s; 58 | case 14: 59 | s = "fourteen"; 60 | return s; 61 | case 15: 62 | s = "fifteen"; 63 | return s; 64 | case 16: 65 | s = "sixteen"; 66 | return s; 67 | case 17: 68 | s = "seventeen"; 69 | return s; 70 | case 18: 71 | s = "eighteen"; 72 | return s; 73 | case 19: 74 | s = "nineteen"; 75 | return s; 76 | default: 77 | s = "error"; 78 | return s; 79 | } 80 | } 81 | if (number<100) //21-99 82 | { 83 | if (number % 10 == 0) //20,30,40,...90的输出 84 | { 85 | switch (number) 86 | { 87 | case 20: 88 | s = "twenty"; 89 | return s; 90 | case 30: 91 | s = "thirty"; 92 | return s; 93 | case 40: 94 | s = "forty"; 95 | return s; 96 | case 50: 97 | s = "fifty"; 98 | return s; 99 | case 60: 100 | s = "sixty"; 101 | return s; 102 | case 70: 103 | s = "seventy"; 104 | return s; 105 | case 80: 106 | s = "eighty"; 107 | return s; 108 | case 90: 109 | s = "ninety"; 110 | return s; 111 | default: 112 | s = "error"; 113 | return s; 114 | } 115 | } 116 | else 117 | { 118 | s = NUmberToEnglishString(number / 10 * 10) + ' ' + NUmberToEnglishString(number % 10); 119 | return s; 120 | } 121 | 122 | } 123 | if (number<1000) //100-999 124 | { 125 | if (number % 100 == 0) 126 | { 127 | s = NUmberToEnglishString(number / 100) + " hundred"; 128 | return s; 129 | } 130 | else 131 | { 132 | s = NUmberToEnglishString(number / 100) + " hundred and " + NUmberToEnglishString(number % 100); 133 | return s; 134 | } 135 | } 136 | if (number<1000000) //1000-999999 百万以下 137 | { 138 | if (number % 1000 == 0) 139 | { 140 | s = NUmberToEnglishString(number / 1000) + " thousand"; 141 | return s; 142 | } 143 | else 144 | { 145 | s = NUmberToEnglishString(number / 1000) + " thousand " + NUmberToEnglishString(number % 1000); 146 | return s; 147 | } 148 | } 149 | if (number<1000000000) //十亿以下 150 | { 151 | if (number % 1000000 == 0) 152 | { 153 | s = NUmberToEnglishString(number / 1000000) + " million"; 154 | return s; 155 | } 156 | else 157 | { 158 | s = NUmberToEnglishString(number / 1000000) + " million " + NUmberToEnglishString(number % 1000000); 159 | return s; 160 | } 161 | } 162 | if (number<9999999999) //十亿到99亿 163 | { 164 | if (number % 1000000000 == 0) 165 | { 166 | s = NUmberToEnglishString(number / 1000000000) + " billion"; 167 | return s; 168 | } 169 | else 170 | { 171 | s = NUmberToEnglishString(number / 1000000000) + " billion " + NUmberToEnglishString(number % 1000000000); 172 | return s; 173 | } 174 | } 175 | if (number>9999999999) 176 | { 177 | s = "error"; 178 | return s; 179 | } 180 | 181 | } 182 | int main() 183 | { 184 | long int a; 185 | cin >> a; 186 | cout << NUmberToEnglishString(a) << endl; 187 | 188 | } 189 | --------------------------------------------------------------------------------