1.从一个字符串中读出所有的整数(转为int)与实数(转为float)2.将整数转化为字符串3.字符串的冒泡排序(从小到大)4.查找介于1~32767之间所有满足下列条件的整数5.编写函数void change(char *a, char *b, char *c)6.对N行N列二维数组的每一行排序,偶数行(包括0)由小到大,奇数行由大到小7.找出1~10000之间的Troitsky数8.合并两个字符集合,每个字符串在新集合中仅出现一次,函数返回新集合中字符串9.找出7个默森尼数10.对一个二维数组按行最小值进行排序
1.从一个字符串中读出所有的整数(转为int)与实数(转为float)#include2.将整数转化为字符串#include int is_num(char c); int is_dot(char c); void count_num(char *str); int main() { char str[] = "Sakura.12.Uni.34.Pilot.56.468.Schneider.0.3"; char *p = str; count_num(p); return 0; } int is_num(char c) { return (c >= 48 && c <= 57) ? 1 : 0; } int is_dot(char c) { return (c == 46) ? 1 : 0; } void count_num(char *str) { double x = 0; int tag_dot = 0;//identify whether '.' has occurred(0) or the offset to this '.'(>=1) int tag_num = 0;//identify whether the current position is a number while (*str != 0) { if (is_num(*str) == 1) { tag_num = 1; if (tag_dot == 0) { x = 10 * x + (*str - 48);//x = 10*x + i } else { x = x + pow(0.1, tag_dot) * (*str - 48);//x = x + 0.1^(offset)*i tag_dot ++; } } else if (is_dot(*str) == 1 && tag_num == 1 && is_num(*(str+1))) { tag_dot = 1;//type is double } else { if (tag_num == 1) { if (tag_dot == 0) printf("%d ", (int)x); else printf("%lf ", x); x = 0; tag_dot = 0; tag_num = 0; } } str++; } if (*str == 0 && is_num(*(str-1))) { if (tag_dot == 0) printf("%d ", (int)x); else printf("%lf ", x); } }
#include3.字符串的冒泡排序(从小到大)#include #define MAX_SIZE 100 void type_trans (int num, char *str); int main() { char *str = (char*)malloc(MAX_SIZE * sizeof(char));//use this string to save result type_trans(12345, str); puts(str); return 0; } void type_trans (int num, char *str) { int digit = 1, tmp = 0; while(num / digit >= 10) { digit *= 10; } while (digit != 0) { *str = num / digit + 48; num = num % digit; digit /= 10; str ++; } *str = ''; }
#include4.查找介于1~32767之间所有满足下列条件的整数int str_len(char *src) { int len; while (*src != '') { len++; src++; } return len; } void bubble_sort(char *str) { int len = str_len(str), tmp, flag; for (int i = 0; i < len-1; i++) { flag = 0; for (int j = 0; j < len-i-1; j++ ) { if (str[j] > str[j+1]) { tmp = str[j]; str[j] = str[j+1]; str[j+1] = tmp; flag = 1; } } if (flag == 0) break; } } int main() { char str[] = "2462435"; bubble_sort(str); puts(str); return 0; }
(1)该数的十进制表示中有且仅有两个相同的数字位;
(2)该数是素数。
#include5.编写函数void change(char *a, char *b, char *c)int same_digit(int x); int is_prime(int x); int main() { int n1 = 1, n2 = 32767; int count = 0; for (int i = n1; i <= n2; ++i) { if (same_digit(i) && is_prime(i)) { printf("%d meets this two requirements.n", i); count++; } } printf("all %d numbers.", count); return 0; } int same_digit(int x) { int digit = 1;//represent the number of digits of x int count[10] = {0};//count the number of times of each number in 0~9 while (x / digit >= 10) digit *= 10; for (int i = 0; digit != 0; ++i) { count[x/digit] ++; x = x % digit; digit /= 10; } for (int i = 0; i < 10; ++i) { if (count[i] == 2) return 1; } return 0; } int is_prime(int x) { if (x == 1)//1 isn't a prime number return 0; for (int i = 2; i < x; ++i) { if (x % i == 0) return 0; } return 1; }
函数功能是:首先把b指向的字符串逆向存放,然后将a指向的字符串和b指向的字符串按排列顺序交叉合并到c指向的数组中,两个字符串中过长的剩余字符接在c指向数组的尾部。
例如,当a指向的字符串为"ABCDEFG",b指向的字符串为"123456789"时,c指向的数组中字符串应为"A9B8C7D6E5F4G321"。
#include6.对N行N列二维数组的每一行排序,偶数行(包括0)由小到大,奇数行由大到小#include #define MAX_SIZE 100 void change(char *a, char *b, char *c); int main() { char a[MAX_SIZE] = "ABCDEFG"; char b[MAX_SIZE] = "123456789"; char c[MAX_SIZE]; change(a, b, c); puts(c); return 0; } void change(char *a, char *b, char *c) { char *invert_b = (char*)malloc(MAX_SIZE * sizeof(char)); char *head_invert_b = invert_b;//the head of invert_b char *head_b = b;//the head of b int i = 0; while (*(b+1) != 0) b++; for (i = 0; b >= head_b; i++) *(invert_b+i) = *b--; *(invert_b+i) = 0; i = 0; while (*invert_b != 0 && *a != 0) { *(c + i++) = *a++; *(c + i++) = *invert_b++; } *(c+i) = 0; while (*invert_b != 0) *(c + i++) = *invert_b++; while (*a != 0) *(c + i++) = *a++; *(c+i) = 0; free(head_invert_b); }
#include7.找出1~10000之间的Troitsky数#include #include #define N 8 void arr_out(int arr[N][N]);//(*arr)[N] is also acceptable void arr_creat(int arr[N][N]); void sort_s_head(int *num);//sort one line with small number at head void sort_b_head(int *num);//sort one line with big number at head int main() { int arr[N][N]; arr_creat(arr); arr_out(arr); for (int col = 0; col < N; col++) (col % 2 == 0) ? sort_s_head(arr[col]) : sort_b_head(arr[col]); arr_out(arr); return 0; } void arr_out(int arr[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("arr[%d][%d]:%02d ", i, j, arr[i][j]); } printf("n"); } printf("n"); } void arr_creat(int arr[N][N]) { srand((unsigned)time(NULL)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { arr[i][j] = rand() % 100; } } } void sort_s_head(int *num) { int min = 0, tmp = 0; for (int i = 0; i < N-1; i++) { min = i; for (int j = i+1; j < N; j++) { if (num[j] < num[min]) min = j; } if (min != i) { tmp = num[i]; num[i] = num[min]; num[min] = tmp; } } } void sort_b_head(int *num) { int tmp = 0; for (int i = 0; i < N-1; i++) { for (int j = 0; j < N-1-i; j++) { if (num[j] < num[j+1]) { tmp = num[j+1]; num[j+1] = num[j]; num[j] = tmp; } } } }
Troitsky数:该数首位数字移动到末位数字之后得到的数是原数的整数倍。
例如:将142857的首位数字1移动到末位之后得到的数是428571,而428571等于3倍142857,因此142857是Troitsky数。
#include8.合并两个字符集合,每个字符串在新集合中仅出现一次,函数返回新集合中字符串#define MAX_SIZE 10000 int CountTroitsky(long a[]); int IsTroitsky(long x); int main() { long arr[MAX_SIZE] = {0}; int i = 0; printf("All %d Troitsky numbers:n", CountTroitsky(arr)); while (arr[i] != 0) printf("%ld is a Troitsky number,n", (int)arr[i++]); return 0; } int CountTroitsky(long a[]) { int count = 0; for (long i = 10; i <= 10000; i++) { if (IsTroitsky(i) == 1) { a[count] = i; count++; } } return count; } int IsTroitsky(long x) { int digit = 1;//represent the number of digits of x while (x / digit >= 10) digit *= 10; return (((x % digit) * 10 + (x / digit)) % x == 0) ? 1 : 0; }
例如,
集合A:{"while", "for", "switch", "if", "continue"},
集合B:{"for", "case", "do", "else", "char", "switch"},
运行结果:{"while", "for", "switch", "if", "continue", "case", "do", "else", "char"}。
#include9.找出7个默森尼数#define MAX_SIZE 100 void arr_out(char *src[MAX_SIZE]); void arr_merge(char *tar[MAX_SIZE], char *src[MAX_SIZE]); int str_cmp(char *str1, char *str2); int main() { char *tar[MAX_SIZE] = {"while", "for", "switch", "if", "continue"}; char *src[MAX_SIZE] = {"for", "case", "do", "else", "char", "switch"}; arr_out(tar); arr_out(src); arr_merge(tar, src); arr_out(tar); return 0; } void arr_out(char *src[MAX_SIZE]) { for (int i = 0; src[i] != NULL; i++) { printf("""); for (int j = 0; src[i][j] != 0; j++) { printf("%c", src[i][j]); } printf("", "); } printf("bb n"); } void arr_merge(char *tar[MAX_SIZE], char *src[MAX_SIZE]) { char **tar_tail = tar;//tail arr of tar int flag = 1;//'1' means insert allowed while (*tar_tail != NULL) tar_tail++; for (int i = 0; src[i] != NULL; i++) { flag = 1; for (int j = 0; tar[j] != NULL; j++) { if (str_cmp(src[i], tar[j]) == 1) flag = 0;//there is a repetition, cannot insert } if (flag == 1) { *tar_tail = src[i]; tar_tail++; } } } int str_cmp(char *str1, char *str2) { while (*str1 != 0 || *str2 != 0) { if (*str1++ != *str2++) return 0; } return 1; }
莫森尼数:对于 M p = 2 p − 1 M_p=2^p-1 Mp=2p−1,当 p p p是素数并且 M p M_p Mp也是素数时,称 M p M_p Mp为默森尼数。
#include10.对一个二维数组按行最小值进行排序int is_prime(int x); int pow_two(int x);//2^x int main() { int count = 0; for (int i = 1; i < 1000; i++) { if (is_prime(i) && is_prime(pow_two(i)-1)) { printf("%d is a Monison number.n", pow_two(i)-1); count++; } if (count == 7) break; } return 0; } int is_prime(int x) { if (x == 1) return 0; for (int i = 2; i < x; i++) { if (x % i == 0) return 0; } return 1; } int pow_two(int x) { int ret = 1; while (x-- != 0) ret *= 2; return ret; }
排序规则:整个数组中值最小的元素所在行排在数组第一行,除第一行元素以外的最小元素所在行排在第2行,以此类推。
#include#include #include #define ROW_LENGTH 3 #define COL_LENGTH 3 void arr_out(int arr[ROW_LENGTH][COL_LENGTH]); void arr_creat(int arr[ROW_LENGTH][COL_LENGTH]); void arr_swap(int arr[ROW_LENGTH][COL_LENGTH], int min_row, int k); int main() { int arr[ROW_LENGTH][COL_LENGTH]; int min_row, min_num; arr_creat(arr); arr_out(arr); for (int k = 0; k < COL_LENGTH; k++) { min_num = arr[k][0]; min_row = k; //find the minimum value for (int i = k; i < COL_LENGTH; i++) { for (int j = 0; j < ROW_LENGTH; j++) { if (arr[i][j] < min_num) { min_row = i; min_num = arr[i][j]; } } } //swap row k with the row with the minimum value arr_swap(arr, min_row, k); } arr_out(arr); return 0; } void arr_out(int arr[ROW_LENGTH][COL_LENGTH]) { for (int i = 0; i < ROW_LENGTH; i++) { for (int j = 0; j < COL_LENGTH; j++) { printf("arr[%d][%d]:%d ", i, j, arr[i][j]); } printf("n"); } printf("n"); } void arr_creat(int arr[ROW_LENGTH][COL_LENGTH]) { srand((unsigned)time(NULL)); for (int i = 0; i < ROW_LENGTH; i++) { for (int j = 0; j < COL_LENGTH; j++) { arr[i][j] = rand() % 100; } } } void arr_swap(int arr[ROW_LENGTH][COL_LENGTH], int min_row, int k) { int temp; for (int i = 0; i < ROW_LENGTH; i++) { temp = arr[min_row][i]; arr[min_row][i] = arr[k][i]; arr[k][i] = temp; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)