收藏 分销(赏)

2021年C语言习题题库作答完整.docx

上传人:二*** 文档编号:4509664 上传时间:2024-09-26 格式:DOCX 页数:64 大小:27.04KB 下载积分:5 金币
下载 相关 举报
2021年C语言习题题库作答完整.docx_第1页
第1页 / 共64页
本文档共64页,全文阅读请下载到手机保存,查看更方便
资源描述
程序填空 共2题 第1题 /*------------------------------------------------------- 【程序填空】 --------------------------------------------------------- 功能:编程求某年第n天日期。用数组表达月天数。 -------------------------------------------------------*/ #include <stdio.h> #include <stdlib.h> main() { int y,m,f,n; int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; printf("y,n="); scanf("%d,%d",&y,&n); /***********SPACE***********/ f=y%4==0&&y%100!=0【||】y%400==0; /***********SPACE***********/ a[1]【+=】f; if(n<1||n>365+f) { printf("error!\n");exit(0); } /***********SPACE***********/ for(m=1;m【>】a[m-1];n-=a[m-1],m++); printf("y=%d,m=%d,d=%d\n",y,m,n); } 第2题 /*------------------------------------------------------- 【程序填空】 --------------------------------------------------------- 题目:下列程序从键盘输入所需数据,求出z值并输出,规定输出成果保存2位小数。 -------------------------------------------------------*/ #include <stdio.h> /***********SPACE***********/ 【#include<math.h>】 main() { int x; double y,z; /***********SPACE***********/ scanf("【%d%lf】",&x,&y); z=2*x*sqrt(y); /***********SPACE***********/ printf("z=【%lf】",z); } 程序改错 共1题 第1题 /*------------------------------------------------------ 【程序改错】 -------------------------------------------------------- 功能:将s所指字符串中字母转换为按字母序列后续字母(但 Z转换为A,z转换为a),其他字符不变。 ------------------------------------------------------*/ #include <stdio.h> #include <ctype.h> #include <conio.h> void fun (char *s) //函数定义 { /***********FOUND***********/ while(*s!=’0’) //字符串结尾标志为‘0’ { if(*s>='A' && *s <= 'Z' || *s >= 'a' && *s<='z') { if(*s=='Z') *s='A'; else if(*s=='z') *s='a'; else /***********FOUND***********/ *s += 1; //s为字符指针,而*s为指针所指字符 } /***********FOUND***********/ s++ } } main() { char s[80]; printf("\n Enter a string with length < 80. :\n\n "); gets(s); printf("\n The string :\n\n "); puts(s); fun ( s ); //函数调用 printf ("\n\n The Cords :\n\n "); puts(s); } 程序填空 共2题 第1题 /*------------------------------------------------------- 【程序填空】 --------------------------------------------------------- 功能:计算圆周率近似值。 -------------------------------------------------------*/ #include <stdio.h> #include <math.h> main() { int s,n; /***********SPACE***********/ double 【pi】,t; t=1;pi=0;n=1;s=1; /***********SPACE***********/ while(【fabs(t)】>=2e-6) //fabs()为求绝对值函数 { pi+=t;n+=2;s=-s;t=s/n; } /***********SPACE***********/ pi*=【4】; printf("pi=%.6f\n",pi); } 第2题 /*------------------------------------------------------- 【程序填空】 --------------------------------------------------------- 功能:输入一奇数n,打印由1->n*n构成魔方矩阵。魔方矩阵 行列及对角线和都相等。 魔方矩阵:8 1 6 3 5 7 4 9 2 -------------------------------------------------------*/ #include <stdio.h> #include <stdlib.h> #define MAXSiZE 20 void main(void) { int matrix[MAXSiZE][MAXSiZE]; int count; int row; int column; int n; char line[100]; printf("\nOdd n Magic Square Generator"); printf("\n================================"); printf("\n\nn Please --> "); gets(line); n = atoi(line); if (n > MAXSiZE) printf("\n*** ERROR *** n should be <= %d",MAXSiZE); else if (n % 2 == 0) printf("\n*** ERROR *** n must be an odd integer"); else { row = 0; column = n/2; for (count = 1;count <= n*n;count++) { matrix[row][column] = count; /***********SPACE***********/ if (【count/n】 == 0) row++; else { /***********SPACE***********/ row= (row == 【0】) ?n - 1 :row - 1; /***********SPACE***********/ column = (column == 【n-1】) ?0 :column + 1; } } printf("\n\nMagic Square of n %d :\n\n",n); for (row = 0;row < n;row++) { for (column = 0;column < n;column++) printf("%4d",matrix[row][column]); printf("\n"); } } } 程序改错 共1题 /*------------------------------------------------------ 【程序改错】 -------------------------------------------------------- 功能:求1到10阶乘和。 ------------------------------------------------------*/ #include <stdio.h> float fac(int n); //函数声明 main() { int i; float s=0; float fac(int n); //函数声明应放在主函数之前,这句放错位置了,删了吧,也可以不改,可以运营,但良好习惯就是改 /**********FOUND**********/ for(i=1;i<10;i++) /**********FOUND**********/ s+=fac(i); //函数调用 printf("%f\n",s); } float fac(int n) //函数定义 { /**********FOUND**********/ float y=1; //int改为float,否则会导致数据丢失,固然也可以不改,毕竟你们没学到 int i; for(i=1 ;i<=n;i++) y=y*i; /**********FOUND**********/ return y; } 程序填空 共2题 第1题 /*------------------------------------------------------- 【程序填空】 --------------------------------------------------------- 功能:删除字符串中指定字符,字符串和要删除字符均由键盘 输入。 -------------------------------------------------------*/ #include <stdio.h> main() { char str[80],ch; int i,k=0; /***********SPACE***********/ gets(【str】); //输入字符串 放入str[] ch=getchar(); /***********SPACE***********/ for(i=0;【str[i]】;i++) //循环到字符串结束为止 if(str[i]!=ch) //如果没找到就将原字符赋值过去 找到要删除就跳过继续找 { /***********SPACE***********/ 【str[k]=str[i]】; //在同一种数组中操作,背面字符覆盖了前面要删除位置 k++; } /***********SPACE***********/ 【str[k]=’\0’】; //在新字符串结尾处加结束符 puts(str); //输出 } 第2题 /*------------------------------------------------------- 【程序填空】 --------------------------------------------------------- 功能:输入一种整数,计算它也许是哪两个整数平方和,并打印 成果数据。 如:34是5和3或3和5平方和。 -------------------------------------------------------*/ #include <stdio.h> /* for i/O functions */ #include <stdlib.h> /* for atoi() */ #include <math.h> /* for sqrt() */ void main(void) { int given; /* the given number */ int row,column; /* row and column indicators*/ int count; /* number of solutions */ char line[100]; printf("\nRepresenting a Given Number as the Sum of Two Squares"); printf("\n=====================================================\n"); printf("\nAn integer Please ---> "); gets(line); given = atoi(line); printf("\nCount X Y"); printf("\n----- ----- -----"); row = 1; /* starts from far enough */ column = (int) (sqrt((double) given) + 0.5); count = 0; /* so solution yet */ while (row <= given && column > 0) /* scan down... */ if (row*row + column*column == given) { /***********SPACE***********/ 【count++】; printf("\n%5d%7d%7d",count,row,column); row++; column--; } else if (row*row + column*column > given) /***********SPACE***********/ 【column--】; else /***********SPACE***********/ 【row++】; if (count == 0) printf("\n\nSorry,NO ANSWER found."); else printf("\n\nThere are %d possible answers.",count); } 程序改错 共1题 第1题 /*------------------------------------------------------ 【程序改错】 -------------------------------------------------------- 功能:计算并输出k以内最大10个能被13或17整除自然数之和。 k值由主函数传入。 例如:若k值为500,则函数值为4622。 ------------------------------------------------------*/ #include <stdio.h> int fun(int k) { int m=0,mc=0; /**********FOUND**********/ while ((k>=2)&&(mc<10)) { /**********FOUND**********/ if((k%13==0)||(k%17==0)) { m=m+k; mc++; } /**********FOUND**********/ k--; } /**********FOUND**********/ return m; } void main() { printf("%d\n",fun(500)); } 程序填空 共2题 第1题 /*------------------------------------------------------- 【程序填空】 --------------------------------------------------------- 功能:当输入“2,5”时候输出为“2 5 5” -------------------------------------------------------*/ #include <stdio.h> #define max 100 main() { int f[max],i,j,k,m; scanf("%d,%d",&k,&m); /***********SPACE***********/ for(i=0;i<=【2】;i++) f[i]=0; /***********SPACE***********/ f[【k-1】]=1; for(i=k;i<=m;i++) /***********SPACE***********/ for(j=i-k;j<=i-1;j++) f[i]【=1+】f[j]; printf("%d%10d%10d\n",k,m,f[m]); } 第2题 /*------------------------------------------------------- 【程序填空】 --------------------------------------------------------- 功能:辨认输入字符串,每个单词输出一行 -------------------------------------------------------*/ #include <stdio.h> #include <string.h> void main() { int c; int inspace; /***********SPACE***********/ 【inspace=0】; while((c = getchar()) != '\n') { if(c == ' ' || c == '\t' || c == '\n') { /***********SPACE***********/ if(【inspace=0】) { inspace = 1; putchar('\n'); } } else { inspace = 0; /***********SPACE***********/ 【putchar(c)】; } } } 程序改错 共1题 第1题 /*------------------------------------------------------ 【程序改错】 -------------------------------------------------------- 功能:用选取法对数组中n个元素按从小到大顺序进行排序。 ------------------------------------------------------*/ #include <stdio.h> #define N 20 void fun(int a[],int n) { int i,j,t,p; for (j = 0 ;j < n-1 ;j++) { /**********FOUND**********/ p = j; for (i = j;i < n;i++) /**********FOUND**********/ if(a[i] >a[p]) /**********FOUND**********/ p=i; t = a[p] ; a[p] = a[j] ; a[j] = t; } } main() { int a[N]={9,6,8,3,-1},i,m = 5; printf("排序前数据:") ; for(i = 0;i < m;i++) printf("%d ",a[i]); printf("\n"); fun(a,m); printf("排序后数据:") ; for(i = 0;i < m;i++) printf("%d ",a[i]); printf("\n"); } 程序填空 共2题 第1题 /*------------------------------------------------------- 【程序填空】 --------------------------------------------------------- 功能:产生10个[30,90]区间上随机整数,然后对其用选取法 进行由小到大排序。 -------------------------------------------------------*/ #include <stdio.h> #include<stdlib.h> main() { /***********SPACE***********/ int t; int i,j,k; int a[10]; for(i=0;i<10;i++) a[i]=rand()%61+30; for(i=0;i<9;i++) { /***********SPACE***********/ k=i; for(j=i+1;j<10;j++) /***********SPACE***********/ if(a[k]>a[j]) k=j; if(k!=i) { t=a[k]; a[k]=a[i]; a[i]=t; } } /***********SPACE***********/ for(i=0;i<10;i++ ) printf("%5d",a[i]); printf("\n"); } 第2题 /*------------------------------------------------------- 【程序填空】 --------------------------------------------------------- 功能:输入一正整数n、打印1-n可以构成所有自然数集合 (包括空集)。 -------------------------------------------------------*/ #include <stdio.h> #include <stdlib.h> #define MAXSiZE 20 #define LOOP 1 void main(void) { int set[MAXSiZE]; int n,i; int position; char line[100]; printf("\nAll Possible Subsets Generation by Lexical Order"); printf("\n================================================"); printf("\n\nNumber of Elements in the Set --> "); gets(line); n = atoi(line); printf("\n{}"); position = 0; set[position] = 1; while (LOOP) { /***********SPACE***********/ printf("\n{%d",【?】); for (i = 1;i <= position;i++) printf(",%d",set[i]); printf("}"); if (set[position] < n) { /***********SPACE***********/ set[【?】] = set[position] + 1; position++; } else if (position != 0) set[--position]++; else /***********SPACE***********/ 【?】; } } 程序改错 第1题 /*------------------------------------------------------ 【程序改错】 -------------------------------------------------------- 功能:求出如下分数序列前n项之和。和值通过函数值返回main 函数。 2/1+3/2+5/3+8/5+13/8+21/13 …… 例如:若n = 5,则应输出:8.391667。 ------------------------------------------------------*/ #include <conio.h> #include <stdio.h> /**********FOUND**********/ fun ( int n ) { int a,b,c,k;double s; s = 0.0;a = 2;b = 1; for ( k = 1;k <= n;k++ ) { /**********FOUND**********/ s = (double)a / b; c = a; a = a + b; b = c; } /**********FOUND**********/ return c; } main( ) { int n = 5; printf( "\nThe value of function is:%lf\n",fun ( n ) ); } 程序填空 共2题 第1题 /*------------------------------------------------------- 【程序填空】 --------------------------------------------------------- 功能:输出成果为: * * * * * * * * * * * * * * * * * * * * -------------------------------------------------------*/ #include <stdio.h> main() { /***********SPACE***********/ static char 【?】={'*','*','*','*','*'}; int i,j,k; char space=' '; for(i=0;i<5;i++) { printf("\n"); for(j=1;j<=3*i;j++) /***********SPACE***********/ printf("%1c",【?】); /***********SPACE***********/ for(k=0;k<【?】;k++) printf("%3c",a[k]); } printf("\n");; } 第2题 /*------------------------------------------------------- 【程序填空】 --------------------------------------------------------- 功能:给出一种正整数,找到一组持续数,使之累加和等于给 定正整数。输出存在多少组这样持续数,及每组左 右边界。 例如:15=1+2+3+4+5 15=4+5+6 15=7+8 因此成果有3组值分别是1->5,4->6,7->8 -------------------------------------------------------*/ #include <stdio.h> #include <stdlib.h> void main(void) { long left,right; long sum; long GiVEN; int count = 0; char line[100]; printf("\nConsecutive sum to a fixed given number"); printf("\n=======================================\n"); printf("\nYour number (> 0) please ---> "); gets(line); GiVEN = atol(line); for (sum = 0,right = 1;sum < GiVEN;sum += right,right++) ; for (left = 1,right--;left <= GiVEN/2;) if (sum > GiVEN) { sum -= left; /***********SPACE***********/ 【?】; } else { if (sum == GiVEN) { printf("\n%ld = sum from %ld to %ld", GiVEN,left,right); /***********SPACE***********/ 【?】;
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服