博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《C程序设计的抽象思维》2.10编程练习(未完)
阅读量:5129 次
发布时间:2019-06-13

本文共 5262 字,大约阅读时间需要 17 分钟。

本文地址:,转载请注明源地址。

2、按照规定求圆柱的表面积和体积

#include
#include
#include
#define PI 3.1415926void input(double *r, double *h){ double a, b; printf("请输入半径和高度:"); scanf("%lf %lf", &a, &b); *r = a; *h = b;}void count(double r, double h, double *s, double *v){ *s = (2 * PI * r) * h; *v = PI * r * r * h;}void output(double s, double v){ printf("圆柱的表面积:%lf\n", s); printf("圆柱的体积:%lf\n", v);}int main(){ double r, h, s, v; input(&r, &h); count(r, h, &s, &v); output(s, v); return 0;}

3、按照规则计算平均值

#include
#include
#include
#define MaxJudges 100double Mean(double array[], int n){ int i; double total, max, min; max = min = array[0]; total = 0.0; for(i = 0; i < n; i++) { total += array[i]; if(max < array[i]) max = array[i]; if(min > array[i]) min = array[i]; } return ((total - min - max) / n);}void ReadScore(double scores[], int n){ int i; for(i = 0; i < n; i++) scanf("%lf", &scores[i]);}int main(){ int n; double array[MaxJudges]; scanf("%d", &n); ReadScore(array, n); printf("平均值为:%lf\n", Mean(array, n)); return 0;}

4、判断一个数列是否升序

#include
#include
#include
bool IsSorted(int *a, int n){ int i; for(i = 1; i <= n - 1; i++) { if(a[i] < a[i-1]) return false; } return true;}int main(){ int n, array[1000]; scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d", &array[i]); if(IsSorted(array, n)) printf("TRUE\n"); else printf("FALSE\n"); return 0;}

5、埃拉托斯特尼筛生成2~1000之间的素数

#include
#include
#include
#define N 1000000bool isprime[N];void initPrime(){ int n, i, j; int k; for(k = 0; k < N; k++) isprime[k] = true; n = (int)sqrt(N); for(i = 2; i <= n; i++) { if(isprime[i]) { j = i * i; while(j <= n) { isprime[j] = false; j += i; } } }}int main(){ int i = 2; initPrime(); while(1) { if(i > 1000) break; if(isprime[i]) printf("%d\n", i); i++; } return 0;}

6、按照格式输出一组数据的柱状图

#include
#include
#include
void printStar(int n){ for(int i = 1; i <= n; i++) putchar('*'); printf("\n");}int main(){ int a[11], b[1000]; int n, t, i; scanf("%d", &n); for(i = 0; i < 11; i++) a[i] = 0; for(i =0; i < n; i++) { scanf("%d", &b[i]); t = b[i] / 10; switch(t) { case 0: a[0]++; break; case 1: a[1]++; break; case 2: a[2]++; break; case 3: a[3]++; break; case 4: a[4]++; break; case 5: a[5]++; break; case 6: a[6]++; break; case 7: a[7]++; break; case 8: a[8]++; break; case 9: a[9]++; break; case 10: a[10]++; break; default: break; } } for(i = 0; i < 11; i++) { printf("%3d: ", i * 10); printStar(a[i]); } return 0;}

8、扫描数组,出去数组中含0的元素,返回0的数目

#include
#include
#include
void swap(int *a, int *b){ int t; t = *a; *a = *b; *b = t;}int RemoveZeroElements(int a[], int n){ int num, i, j; num = 0; if(a[0]) num++; for(i = 0; i < n; i++) { if(!a[i] && i < n - 1) { for(j = i + 1; j < n; j++) { if(a[j]) { num++; swap(&a[i], &a[j]); break; } } } } return n - num;}void printArray(int *a, int n){ int i; for(i = 0; i < n; i++) printf("%d ", a[i]); printf("\n");}int main(){ int a[] = {
65,0,95,0,0,79,82,0,84,94,86,90,0}; int n, zeronum; n = sizeof(a) / sizeof(a[0]); printArray(a, n); zeronum = RemoveZeroElements(a, n); printArray(a, n - zeronum); printf("zreo numbers: %d\n", zeronum); return 0;}

10、找出一组输入数字中的最小值与最大值

#include
#include
#include
void solve(){ double n, min, max; printf("Enter the elements of the array, one per line.\n"); printf("use -1 to sigal the end of the list.\n"); printf("? "); scanf("%lf", &n); max = min = n; printf("? "); while(~scanf("%lf", &n) && (n != -1)) { if(n < min) min = n; if(max < n) max = n; printf("? "); } printf("The range of value is %lf-%lf\n", min, max);}int main(){ solve(); return 0;}

11、按要求动态分配一个整型数组并赋值

#include
#include
#include
#include
int *indexArray(int n){ int *p = (int *)malloc(n * sizeof(int)); for(int i = 0; i < n; i++) p[i] = i; return p;}int main(){ int *ip, n; scanf("%d", &n); ip = indexArray(n); for(int i = 0; i < n; i++) printf("%d ", ip[i]); printf("\n"); free(ip); return 0;}

 

 

转载于:https://www.cnblogs.com/wuyudong/p/programming-abstractions-in-c-2.html

你可能感兴趣的文章
shell习题第5题:批量更改文件后缀名
查看>>
SQL基础教程
查看>>
Autofac - 生命周期的理解
查看>>
centos LVM详解
查看>>
debian使用记录
查看>>
input password密码验证跳转页面
查看>>
【HEVC帧间预测论文】P1.3 Fast Inter-Frame Prediction Algorithm of HEVC Based on Graphic Information...
查看>>
ECMAscript v.s. Javascript
查看>>
View State
查看>>
HTML标记参考手册
查看>>
svn服务器搭建
查看>>
使用SpringAop 验证方法参数是否合法
查看>>
关于Microsoft.SharePoint.Security的问题
查看>>
AndroidManifest.xml详解
查看>>
17-09-15模拟赛
查看>>
Educational Codeforces Round 63 Div.2 D - Beautiful Array
查看>>
11.Python标准库_多进程探索 (multiprocessing包)
查看>>
Springboot中读取properties配置
查看>>
jQuery实现简单的tab切换
查看>>
初学编程的人们看过来 [转]
查看>>