C语言编程题
1,最大公约数
//最大公约数int a=4, b=12,temp;while(a % b){temp = a%b;b = a;a = temp;}printf("%d\n" , b);
思路:
- 求两数之间的最小值min
- for循环min一次递减
- if(两数是否都可以余i)
2,最小公倍数
//最小公倍数int c = 4, d = 12,min,max; min = c < b ? c : d; max = c > b ? c : d; for (int i = 1; i <= min; i++) { if ((max * i) % min == 0) { printf("%d\n", max * i); break; } }
3,求n的所有素数因子
/*求n的所有素数因子(12的因子有1,2,3,6,12,但素数因子只有 2,3),*/void prime(int k) {int i, j;for (i = 2; i <= k/2; i++) {if (k % i == 0)//求所有因子{for (j = 2; j < i; j++) { //判断该因子是否为素数if (i % j == 0) break;}if (j == i){printf("%5d", i);}}}}
4,将整数字符串转成数值
#include int chnum(char* p) { int num = 0, k, len, j;len = strlen(p);for (;len!=0;p++){k = *p - '0';//将第一个字符转换成数字j = len-1;while (j > 0) {//判断有几位数 k*(int)pow(10,j)k = k * 10;j--;}num = num + k;}return num;}
5,判断子串substr在母串str中出现的次数
int count(char* str,char *substr) {int i, j, k, num = 0;for (i = 0; str[i] != '\0'; i++) for (j=i,k=0;substr[k]==str[j];k++,j++)if (substr[k+1] == '\0') { num++; break; }return num;
1,先遍历母串str
2,每次循环子串下标 初始值为0,并且j=i;
3,如果子串下一位的字符为‘\0’,num++; break;跳出循环
6,删除在a数组中全部删除k这个数字
void deleteNum(int a[],int n, int k) {int i, j,x=0; //数组中有n个kfor (i = 0; i < n; i++) // 遍历a{if (k==a[i]) //在a数组中查找k{x++; //k的个数for (j = i; j < n-x; j++)//从后面往前移动 最后一位为N-n{a[j] = a[j + 1];}i--; //删除连续的k值}a[n-x] = 0;}}
第六章
1,100以内的素数及素数的个数
int main() {/*1,100以内的素数 */int i, j, flag,num=0;for ( i = 3; i < 100; i++){flag = 1; //*******************for (j = 2; j <= sqrt(i); j++){if (i % j == 0) {flag = 0;break;}}if (flag != 0){printf("%5d", i);//输出100以内的素数num++;}}printf("\n100以内的素数个数有%d\n", num);
2,用选择法进行排序(升序)
int a[10], i, j, n, temp;for (i = 0; i < 10; i++)a[i] = 10 - i;//scanf("%d", &a[i]);输入数据for (i = 0; i < 10; i++)// 0N{n = i;for (j =i; j a[j]) n = j;}if (n!=i){temp = a[i];a[i] = a[n];a[n] = temp;}}//打印数组for (i = 0; i < 10; i++){printf("%5d", a[i]);if ((i+1)%5==0)printf("\n");}
3,求二维数组a[3][3]的对角线的和
int a[3][3], i, j, s = 0,n=1;for (i = 0; i < 3; i++) //给数组输入值{for (j = 0; j < 3; j++){scanf("%d", &a[i][j]);}}for (i = 0; i < 3; i++){for (j = 0; j < 3; j++){if (i == j) //i==js += a[i][j];if (i+j==2) //j+i==N-1{s += a[i][j];}}}printf("sum=%d", s);
1, i==j
- j +i==N-1
4,在已排好序的数组中,插入一个数k,并且数组还是有序的
int a[N],i,j,k=6;for (i = 0; i < N-2; i++)//输入数据到a[3]{a[i] = 10-i;}//*****************************if (a[1] > a[0])//判断数组 a是递增还是递减for (i = 0; a[i] < k; i++);//找出插入的下标elsefor (i = 0; a[i] > k; i++);//**************************************for (j = N-2; j >= i; j--)//将插入下标后的全部后移{a[j + 1] = a[j];}a[j+1] = k; //************for (i = 0; i < N; i++) //输出a[3]{printf("%3d", a[i]);if ((i+1)%5==0){printf("\n");}}
5,将数组倒序
int a[N],i,high=N-1,low=0,temp;for (i = 0; i < N; i++){a[i] = i + 1; //scanf("%d",&a[i]);}while (low < high) { //倒序排列方法一temp = a[low];a[low] = a[high];a[high] = temp;low++;high--;}for (i = 0; i < N; i++){printf("%3d", a[i]);}printf("\n");for (i = 0; i < N/2; i++) {//倒序排列方法二temp = a[i];a[i] = a[N-1-i];a[N - 1 - i] = temp;}for ( i = 0; i < N; i++){printf("%3d", a[i]);}
6,杨辉三角
int a[10][10], i, j;for (i = 0; i < 10; i++)//0 N{for (j = 0; j <= i; j++)//0j<=i{if (i == j || j == 0) a[i][j] = 1;else a[i][j] = a[i-1][j-1]+ a[i-1][j];}}for (i = 0; i < 10; i++)//打印二维数组{for (j = 0; j <=i; j++)//***** j <=i;***********printf("%5d", a[i][j]);printf("\n");}
7,求一个二维数组的鞍点(该行最大,该列最小 的地址(row,col))
int a[3][3], i, j,k,max,min, col=0,row=0,flag=0;for ( i = 0; i < 3; i++) //给二维数组a输入值{for (j = 0; j < 3; j++){a[i][j] = i + j;}}a[0][2] = 1;for (i = 0; i < 3; i++){//找出行中的最大值max及他的列数colmax = a[i][0];for (j = 0; j < 3; j++){if (a[i][j] > max){max = a[i][j];col = j;}}//找出列中的最小值min及他的行数rowfor (k = 0; k < 3; k++){min = a[i][col];if (min>a[k][col]){min = a[k][col];row = k;}}if (i == row) {printf("a[3][3]的鞍点有:a[%2d][%2d]", i, k);flag=1;}}if (flag==0){printf("该二维数组无鞍点!");}
8,折半查找法在有序数组a中查找是否有输入的n有就输出他是第几个数,否则输出查无此数
int a[N], n,i,mid,high,low;n = 0;for (i = 0; i < N; i++)a[i] = i;//scanf("%d", &n);high = N-1;low = 0;while (low<=high) {mid = (high+low) / 2;if (n > a[mid]) low = mid + 1;else if (n < a[mid]) high = mid - 1;else break;}if (low>high)printf("查无此数!");else printf("%5d",mid+1);
思路:
看到折半应想到 high=N-1,low=0;while(high>=low){ mid=(high+low)/2; if…}
N=strlen(str);
9,有三行每行80个字符,统计每行字符中的空格,数字,大小写字母及其他字符的个数
char a[3][80];int i,j,digit,big,small,space,el;for (i = 0; i < 3; i++){gets_s(a[i]);}for (i = 0; i < 3; i++){j = 0,digit = 0, big = 0, small = 0, space = 0, el = 0;while (a[i][j]!='\0'){if (a[i][j] <= 'z' && a[i][j] >= 'a')small++;else if (a[i][j] <= 'Z' && a[i][j] >= 'A')big++;else if (a[i][j] <= '9' && a[i][j] >= '0')digit++;else if (a[i][j] == ' ')space++;elseel++;j++;}printf("第%d行有big=%-5d,small=%-5d,space=%-5d,digit=%-5d,el=%-5d\n",i+1, big, small, space, digit,el);}
思路:
可以直接双 for循环 边输入 边判断(for(i<3){ for(j<80) {if…} }
10,打印5×9的 平行四边形
int i, j,k;for ( i = 0; i < 5; i++){for (j = 0; j < i; j++)printf(" ");for (k = 0; k < 5; k++)printf("*");printf("\n");}
11,仿写一个strcat函数将b追加到a中
char a[20], b[20], * p1, * p2;p1 = a,p2=b;gets_s(a);gets_s(b);while (*p1 != '\0')p1++;//== for (; *p1 != '\0'; p1++);while (*p2 != '\0') {*p1 = *p2;p1++;p2++;}*p1 = '\0';p1 = a;puts(a);
12,比较数组a,b的大小,返回相差的ascii值
char a[N]="abde", b[N]="abd", * p1, * p2;p1 = a, p2 = b;while (*p1==*p2){if (*p1=='\0'){printf("0");break;}p1++;p2++;}if(*p1 != *p2)printf("%d",*p1-*p2);
函数形式
intmystrcmp(char* p1, char* p2) {while (*p1==*p2){if (*p1=='\0')//那么得出p1,拍{return 0;}p1++;p2++;}return *p1-*p2;}
思路:先用while循环进行比较 while(*p1==*p2)
13,仿写一个strcpy函数
void mystrcpy(char* p1, char* p2) {while (*p2!='\0'){*p1 = *p2;p1++;p2++;}*p1 = '\0';}
14, 破译密码 (a->z,A->Z )
void change(char* p) {while (*p!='\0'){if (*p>='a'&&*p<='z') //小写{*p = 27 - (*p - 96) + 96;}else if (*p >= 'A' && *p <= 'Z') {//大写*p = 27 - (*p - 64) + 64;}p++;}}
思路:假设a=1;z=26; 那么 a+反编译=27