资源描述
(二)程序填空
1,求最大公约数
,下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:输入m、n(规定输入数均大于0),输出它们的最大公约数。
#include <stdio.h>
void main()
{ int m,n,k;
while(scanf("%d%d",&m,&n), _______1______ );
for( ____2____; n%k!=0||m%k!=0; k--);
printf("%d\n",k);
}
· #include <stdio.h>
· void main()
· {int m,n,k;
· while(scanf("%d%d",&m,&n)!=EOF&&m<=0||n<=0);
· {for(k=m; n%k!=0||m%k!=0; k--);
· printf("%d\n",k);
· }
· }
2,分别记录字符串中各字符
下面程序中"____ N ____"是根据程序功能需要填充部分, 请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:分别记录字符串中英文字母、数字和其他字符出现的次数。
#include <stdio.h>
#include <string.h>
void main()
{ char a[80]; int n[3]={0},i;
gets(a);
for(i=0;a[i]!='\0';i++)
{ if (/*-------1---------*/) /*记录字母个数*/
n[0]++;
else if (/*------2------*/) /*记录数字个数*/
n[1]++;
else
n[2]++;
}
for(i=0;i<3;i++) printf(/*------3------*/);
}
· #include<stdio.h>
· #include<string.h>
· int main()
· { char a[80]; int n[3]={0},i;
· gets(a);
· for(i=0;a[i]!='\0';i++)
· { if (a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z')
· n[0]++;
· else if (a[i]>='0'&&a[i]<='9')
· n[1]++;
· else
· n[2]++;
· }
· for(i=0;i<3;i++) printf("%d\n",n[i]);
· }
3,求相邻两元素和
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:求出a中各相邻两个元素的和,并将这些和存放在数组 b中,按每行3个元素的形式输出。
例如: b[1]=a[1]+a[0],…………,b[9]=a[9]+a[8]。
#include <stdio.h>
void main()
{
int a[10],b[10],i;
printf("\nInput 10 numbers: ");
for (i=0; i<10;i++) /* 数组输入 */
scanf("%d", &a[i]);
for (i=1; i<10; i++)
b[i]=_______1_____; /* 计算b数组中的元素 */
for (i=1; i<10; i++)
{
printf("%3d",b[i]);
if (_____2_______) printf("\n"); /* 每行打印3个数据 */
}
}
· #include<stdio.h>
· void main()
· {
· int a[10],b[10],i;
· printf("\nInput 10 numbers: ");
· for (i=0; i<10;i++)
· scanf("%d", &a[i]);
· for (i=1; i<10; i++)
· b[i]=a[i]+a[i-1];
· for (i=1; i<10; i++)
· {
· printf("%3d",b[i]);
· if (i%3==0) printf("\n");
· }
· }
4求数字和
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:求输入的整数各位数字之和,如输入234则输出9,输入-312则输出6。
#include <stdio.h>
#include <math.h>
void main()
{
int n,s=0;
scanf("%d",&n);
______ 1 ______
while(n!=0) {
______ 2 ______
n=n/10;
}
printf("%d\n",s);
}
· #include<stdio.h>
· #include<math.h>
· void main()
· {
· int n,s=0;
· scanf("%d",&n);
· if(n<0){n=-n;}
· while(n!=0) {
· s=s+n%10;
· n=n/10;
· }
· printf("%d\n",s);
· }
5,求整数位数
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:输入1个整数后,输出该数的位数(若输入3214则输出4,输入-23156则输出5)。
#include <stdio.h>
void main()
{ int n,k=0;
scanf("%d",&n);
while( n!=0 ){
k++;
n=n/10;
}
printf("%d\n",k);
}
· #include <stdio.h>
· void main()
· { int n,k=0;
· scanf("%d",&n);
· while(n!=0){
· k++;
· n=n/10;
· }
· printf("%d\n",k);
· }
6,数列2项和
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:数列的第1、2项均为1,此后各项值均为该项前二项之和。计算数列第30项的值。
#include <stdio.h>
long f(int n);
void main()
{
printf("%ld\n",_____1____);
}
long f(int n)
{
if(_______2______)
return 1;
else
return ______3_____;
}
· #include<stdio.h>
· long f(int n);
· int main()
· {
· printf("%ld\n",f(30));
· }
· long f(int n)
· {
· if(n==1||n==2)
· return 1;
· else
· return f(n-1)+f(n-2);
· }
7,数倒置
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:调用函数f,将1个整数首尾倒置,若程序输入12345,则输出54321;若程序输入-34567,则输出-76543。
#include <stdio.h>
#include <math.h>
long f(long n)
{ long m,y=0;
m=fabs(n);
while(m!=0) {
y=y*10+m%10;
______1______;
}
if(_______2_____) return y;
else return -y;
}
int main()
{
printf("%ld\n",f(12345)); printf("%ld\n",f(-34567));
return 0;
}
· #include <stdio.h>
· #include <math.h>
· long f(long n)
· { long m,y=0;
· m=fabs(n);
· while(m!=0) {
· y=y*10+m%10;
· m=m/10;
· }
· if(n>=0) return y;
· else return -y ;
· }
· void main()
· {
· printf("%ld\n",f(12345)); printf("%ld\n",f(-34567));
· }
8,删除数字字符
下面程序中中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:调用函数f,从字符串中删除所有的数字字符。
#include < stdio.h >
#include < string.h >
#include < ctype.h >
void f(char *s)
{ int i=0;
while(s[i]!='\0'){
if(isdigit(s[i])) ____1____;
else ______2___;}
}
void main()
{ char str[80];
gets(str); f(str); puts(str);
}
· #include <stdio.h>
· #include <string.h>
· #include <ctype.h>
· void f(char *s)
· { int i=0;
· while(s[i]!='\0'){
· if(isdigit(s[i])) strcpy(s+i,s+i+1);
· else i++;}
· }
· void main()
· { char str[80];
· gets(str); f(str); puts(str);
· }
9,删除c
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:将字符串s中所有的字符'c'删除。
#include <stdio.h>
void main()
{
char s[80];
int i,j;
gets(s);
for(i=j=0;str[i]!= '\0';i++)
if(______1_______)
{
s[j]=s[i];
j++
}
______2______;
puts(s);
}
· #include <stdio.h>
· void main()
· { char s[80];
· int i,j;
· gets(s);
· for(i=j=0; s[i] != '\0'; i++)
· if(s[i] != 'c')
· { s[j]=s[i];
· j++;
· }
· s[j]='\0';
· puts(s);
· }
10,去数组负数
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:输入数组x[7],调用函数f,去除数组中的负数,输出结果为:1 3 4 6
#include < stdio.h >
void f(int *a,int *m)
{ int i,j;
for(i=0;i < *m;i++)
if(a[i] < 0) {
for(j=i--;j < *m-1;j++) a[j]=a[j+1];
_____1_____;
}
}
void main()
{ int i,n=7,x[7]={1,-2,3,4,-5,6,-7};
_______2_______;
for(i=0;i < n;i++) printf("%5d",x[i]);
printf("\n");
}
· #include <stdio.h>
· void f(int *a,int *m)
· { int i,j;
· for(i=0;i < *m;i++)
· if(a[i] < 0) {
· for(j=i--;j < *m-1;j++) a[j]=a[j+1];
· *m=*m-1;
· }
· }
· int main()
· { int i,n=7,x[7]={1,-2,3,4,-5,6,-7};
· f(x,&n);
· for(i=0;i < n;i++) printf("%5d",x[i]);
· printf("\n");
· }
11,平均成绩
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:该程序计算四位学生的平均成绩,保存在结构中,然后列表输出这些学生的信息。
#include
struct STUDENT
{
char name[16];
int math;
int english;
int computer;
int average;
};
void GetAverage(struct STUDENT *pst) /* 计算平均成绩 */
{
int sum=0;
sum = ______1______; //pst->math+pst->english+pst->computer
pst->average = sum/3;
}
void main()
{
int i;
struct STUDENT st[4]={{"Jessica",98,95,90},{"Mike",80,80,90},
{"Linda",87,76,70},{"Peter",90,100,99}};
for(i=0;i<4;i++)
{
GetAverage(______2______); //st + i
}
printf("Name\tMath\tEnglish\tCompu\tAverage\n");
for(i=0;i<4;i++)
{
printf("%s\t%d\t%d\t%d\t%d\n",st[i].name,st[i].math,st[i].english,
st[i].computer,st[i].average);
}
}
12,后移5位
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:输入的一个小写字母,将字母循环后移5个位置后输出,如’a’变成’f’, ‘w’变成’b’。
#include
void main()
{
char c;
c=getchar();
if(______1______)
c=c+5;
else
if (c>='v' && c<='z')
______2______
putchar(c);
}
· #include<stdio.h>
· int main()
· {
· char c;
· c=getchar();
· if(c>='a'&&c<'v')
· c=c+5;
· else
· if (c>='v' && c<='z')
· c='a'+'z'-c;
· putchar(c);
· }
13,函数最大值
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:对x=1,2,...,10,求f(x)=x*x-5*x+sin(x)的最大值。
#include<stdio.h>
#include<math.h>
#define f(x) x*x-5*x+sin(x)
void main()
{
int x; float max;
/*----1---*/;
for(x=2;x<=10;x++)
if(max
fprintf(p,"%f\n",max);
}
· #include<stdio.h>
· #include<math.h>
· #define f(x) x*x-5*x+sin(x)
· int main()
· {
· int x; float max;
· max=f(1);
· for(x=2;x<=10;x++)
· if(max<f(x)) max=f(x);
· printf("%f\n",max);
· }
14,2n小于m
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:输入整数n(n>0),求m使得2的m次方小于或等于n、2的m+1次方大于或等于n。
#include "stdio.h"
void main()
{ int m=0,t=1,n;
while( _____ 1 ________);
while(!(t<=n&&t*2>=n)){
_____ 2 _____
m++;
}
printf("%d\n",m);
}
· #include "stdio.h"
· int main()
· { int m=0,t=1,n;
· while(scanf("%d",&n),n<=0);
· while(!(t<=n&&t*2>=n)){
· t=t*2;
· m++;
· }
· printf("%d\n",m);
· }
15,findthe
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:调用find函数在输入的字符串中查找是否出现"the"这个单词。假如查到返回出现的次数;假如未找到返回0。
#include "stdio.h"
int find(char *str)
{
char *fstr="the";
int i=0,j,n=0;
while (str[i]!='\0')
{
for(______1______)
if (str[j+i]!=fstr[j]) break;
if (______2______) n++;
i++;
}
return n;
}
void main()
{ char a[80];
gets(a);
printf("%d",find(a));
}
· #include "stdio.h"
· int find(char *str)
· {
· char *fstr="the";
· int i=0,j,n=0;
· while (str[i]!='\0')
· {
· for(j=0;j<3;j++)
· {if (str[j+i]!=fstr[j]) break;
· if (j==2) n++;}
· i++;
· }
· return n;
· }
·
· main()
· { char a[80];
· gets(a);
· printf("%d",find(a));
· }
16,ditoh
下面程序中中"____ N ____"是根据程序功能需要填充部分, 请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:将输入的十进制整数n通过函数DtoH转换为十六进制数,并将转换结果以字符串形式输出。
例如:输入十进制数79,将输出十六进制4f。
# include "stdio.h"
# include "string.h"
char trans(int x)
{
if(x < 10) return '0'+x;
else _______1________
}
int DtoH(int n,char *str)
{
int i=0;
while(n!=0)
{
________2________
n/=16;i++;}
return i-1;
}
void main()
{
int i,k,n;
char str[30];
scanf("%d",&n);
k=DtoH(n,str);
for (i=0;i <= k;i++) printf("%c",str[k-i]);
}
· # include "stdio.h"
· # include "string.h"
· char trans(int x)
· {
· if(x < 10) return '0'+x;
· else return 'a'+x-10;
· }
·
· int DtoH(int n,char *str)
· {
· int i=0;
· while(n!=0)
· {
· str[i]=trans(n%16);
· n/=16;i++;}
· return i-1;
· }
·
· int main()
· {
· int i,k,n;
· char str[30];
· scanf("%d",&n);
· k=DtoH(n,str);
· for (i=0;i <= k;i++) printf("%c",str[k-i]);
· }
17,dec2bin
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:将输入的十进制正整数n通过函数Dec2Bin转换为二进制数,并将转换结果输出。
#include "stdio.h"
void Dec2Bin(int m)
{
int bin[32],j;
for(j=0;m!=0;j++)
{
bin[j]= m%2;
m=m/2;
}
for(;j!=0;j--)
printf("%d",bin[j-1]);
}
void main()
{
int n;
scanf("%d",&n);
Dec2Bin(n);
}
· #include "stdio.h"
· void Dec2Bin(int m)
· {
· int bin[32],j;
· for(j=0;m!=0;j++)
· {
· bin[j]= m%2;
· m=m/2;
· }
· for(;j!=0;j--)
· printf("%d",bin[j-1]);
· }
· void main()
· {
· int n;
· scanf("%d",&n);
· Dec2Bin(n);
· }
18,armstrong.c
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:找出100~999之间所有的Armstrong数。所谓Armstrong数是指这个3位数各位上数字的立方和等于自身。
例如:371=3*3*3+7*7*7+1*1*1,那么371就是Armstrong数。
#include "stdio.h"
#include "math.h"
void main()
{
int i,a,b,c;
for(i=100;i<=999;i++)
{
a=i/100;
_______1_______
c=i%10;
if (________2________)
printf("%d is a Armstrong number!\n",i);
}
}
· #include "stdio.h"
· #include "math.h"
· int main()
· {
· int i,a,b,c;
· for(i=100;i<=999;i++)
· {
· a=i/100;
· b=i%100/10;
· c=i%10;
· if (a*a*a+b*b*b+c*c*c==i)
· printf("%d is a Armstrong number!\n",i);
· }
· }
19,modify_c
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:调用函数f计算代数多项式 1.1+2.2*x+3.3*x*x+4.4*x*x*x+5.5*x*x*x*x;当x=1.7时的值。
#include "stdio.h"
float f(float,float*,int);
int main()
{ float b[5]={1.1,2.2,3.3,4.4,5.5};
float a;
while(scanf("%f",&a)!=EOF)
printf("%.2f\n",f(_____1_____));
}
float f(float x,float *a,int n)
{ float y=a[0],t=1;
int i;
for(i=1;i < n;i++) {
t=t*x;
______2______;
}
return(y);
}
· #include "stdio.h"
· float f(float,float*,int);
· int main()
· { float b[5]={1.1,2.2,3.3,4.4,5.5};
· float a;
· while(scanf("%f",&a)!=EOF)
· printf("%.2f\n",f(a,b,5));
· }
· float f(float x,float *a,int n)
· { float y=a[0],t=1;
· int i;
· for(i=1;i < n;i++) {
· t=t*x;
· y=y+a[i]*t;;
· }
· return(y);
· }
20,3数排序
下面程序中"____ N ____"是根据程序功能需要填充部分,请完毕程序填充(注意:不得加行、减行、加句、减句,否则后果自负)。
该程序功能:输入三个整数,按由小到大的顺序输出这三个数。
#include "stdio.h"
void swap(int *pa,int *pb)
{
int temp;
temp = *pa;
*pa = *pb;
*pb = temp;
}
void main()
{
int a,b,c,temp;
scanf("%d,%d,%d",&a,&b,&c);
if(a > b)
_______1_____;
if(b > c)
_____2______;
if(a > c)
_____3____;
printf("%d,%d,%d",a,b,c);
}
· #include "stdio.h"
· void swap(int *pa,int *pb)
· {
· int temp;
· temp=*pa;
· *pa=*pb;
· *pb=temp;
· }
· int main()
· {
· int a,b,c;
· scanf("%d,%d,%d",&a,&b,&c);
· if(a>b)
· swap(&a,&b);
· if(a>c)
· swap(&a,&c);
· if(b>c)
· swap(&b,&c);
· if(a>c)
· swap(&a,&c);
· printf("%d,%d,%d",a,b,c);
· }
展开阅读全文