教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 教学研究 >

C语言程序设计上机习题与参考答案(2)

来源:网络收集 时间:2026-05-06
导读: for(int t=2; t*t=i; t++) { if(i%t==0) break; else { count++; if(count=5) printf(%d\t,i); break; } } printf(\n); } 11. #include stdio.h #includemath.h float fun(float esp) { int n=0; float s=0; do{ n+

for(int t=2; t*t<=i; t++) {

if(i%t==0)

break;

else

{

count++;

if(count<=5)

printf("%d\t",i); break;

}

}

printf("\n");

}

11.

#include <stdio.h>

#include<math.h>

float fun(float esp)

{

int n=0;

float s=0;

do{

n++;

s+=pow(-1,n-1)*1.0/pow(n,n); }while(1.0/pow(n,n)>esp);

return s;

}

void main()

{float esp;

C语言程序设计上机习题与参考答案

scanf("%f",&esp);

printf("%f\n",fun(esp));

}

12.

#include<stdio.h>

void change(int x,int r)

{

int a[32],i;

for(i=0;x!=0;i++)

{

a[i]=x%r;

x/=r;

}

for(i--;i>=0;i--)

printf("%d",a[i]);

printf("\n");

}

void main()

{

int x,r;

printf("请输入x和r:");

scanf("%d%d",&x,&r);

change(x,r);

}

13.

#include <stdio.h>

int fun(int x)

{

int y=0;

for(int i=1;i<x;i++)

{

if(x%i==0)

y+=i;

}

return y;

}

void main()

{

int n,a,b;

for(n=1;n<1000;n++)

C语言程序设计上机习题与参考答案

} { b=fun(n); a=fun(b); if(a!=b&&a==n) printf("%d %d\n",a,b); }

14.

#include <stdio.h>

int func(int n)

{

if(n==1||n==0)

return 1;

else return 2*func(n-1)+func(n-2); }

void main()

{

printf("%d\n",func(20));

}

15.

#include <stdio.h>

#include<math.h>

long f(char s[ ])

{ long t=0;

for(int i=0;i<10;i++)

{

t+=(s[i]-48)*pow(10,9-i); }

return t;

}

void main()

{ char s[10];

long int res;

gets(s);

res=f(s);

printf("%ld\n",res);

}

16.

C语言程序设计上机习题与参考答案

#include <stdio.h>

int fun(float aa[],int n )

{ int max=0;

for(int i=0;i<10;i++)

if(aa[i]>=aa[max])

max=i;

return max;

}

void main()

{

float a[10];

int k,max;

for(k=0;k<10;k++)

scanf("%f",&a[k]);

max=fun(a,10);

printf("Max=%f\n",a[max]); }

17.

#include <stdio.h>

#include <string.h>

void fun(char s[ ],int n) {

char a[200]="";

memcpy(a,s,n);

strcat(strcpy(s,s+n),a); }

void main()

{

char ss[255];

int n;

gets(ss);

printf("Input data:\n"); scanf("%d",&n);

fun(ss,n);

puts(ss);

}

18.

#include <stdio.h>

void fun(char *ss,char c)

{

C语言程序设计上机习题与参考答案

int i,j;

for(i=0;ss[i]!=0;i++)

{

if(ss[i]==c)

for(j=i;ss[j]!=0;j++) ss[j]=ss[j+1]; }

}

void main()

{

char s[255],ch;

printf("Input a string:\n"); gets(s);

printf("Input a character:\n"); scanf("%c",&ch);

fun(s,ch);

puts(s);

}

19.

#include<stdio.h>

void fun(int *a,int *b,int k,int *n) {

float sum=0,avg=0;

int i,j=0;

for(i=0;i<k;i++)

sum+=*(a+i);

avg=sum/k;

for(i=0;i<k;i++)

{

if(a[i]>avg)

{

b[j]=a[i];

j++;

}

}

*n=j;

}

void main()

{ int a[10],b[10],kk,sum;

for(kk=0;kk<10;kk++)

scanf("%d",&a[kk]);

C语言程序设计上机习题与参考答案

fun(a,b,10,&sum);

for(kk=0;kk<sum;kk++)

printf("%6d",*(b+kk));

}

20.

#include <stdio.h>

#include<math.h>

void fun(char *s,long int *n)

{

long int t=0;

int count=0,i;

int a;

for(i=0;i<20;i++)

{

if(s[i]>=48&&s[i]<=57) {

count++;

}

}

for(i=0;i<20;i++)

{

if(s[i]>='0'&&s[i]<='9') { count--;

t=t+(s[i]-48)*pow(10,count); }

}

*n=t;

}

void main()

{ char s[20];

long int d;

gets(s);

fun(s,&d);

printf("%ld\n",d);

}

…… 此处隐藏:597字,全部文档内容请下载后查看。喜欢就下载吧 ……
C语言程序设计上机习题与参考答案(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/50918.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)