《C程序设计的抽象思维》2.10编程练习(未完)
本文地址:http://www.cnblogs.com/archimedes/p/programming-abstractions-in-c-2.html,转载请注明源地址。
2、按照规定求圆柱的表面积和体积
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#define PI 3.1415926
void 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 = ( * 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 ;
}
3、按照规则计算平均值
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#define MaxJudges 100
double Mean(double array[], int n)
{
int i;
double total, max, min;
max = min = array[];
total = 0.0;
for(i = ; 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 = ; 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 ;
}
4、判断一个数列是否升序
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
bool IsSorted(int *a, int n)
{
int i;
for(i = ; i <= n - ; i++) {
if(a[i] < a[i-])
return false;
}
return true;
}
int main()
{
int n, array[];
scanf("%d", &n);
for(int i = ; i < n; i++)
scanf("%d", &array[i]);
if(IsSorted(array, n))
printf("TRUE\n");
else
printf("FALSE\n");
return ;
}
5、埃拉托斯特尼筛生成2~1000之间的素数
#include<stdio.h>
#include<stdbool.h>
#include<math.h>
#define N 1000000
bool isprime[N];
void initPrime()
{
int n, i, j;
int k;
for(k = ; k < N; k++)
isprime[k] = true;
n = (int)sqrt(N);
for(i = ; i <= n; i++) {
if(isprime[i]) {
j = i * i;
while(j <= n) {
isprime[j] = false;
j += i;
}
}
}
}
int main()
{
int i = ;
initPrime();
while() {
if(i > ) break;
if(isprime[i])
printf("%d\n", i);
i++;
}
return ;
}
6、按照格式输出一组数据的柱状图
#include<stdio.h>
#include<stdbool.h>
#include<math.h>
void printStar(int n)
{
for(int i = ; i <= n; i++)
putchar('*');
printf("\n");
}
int main()
{
int a[], b[];
int n, t, i;
scanf("%d", &n);
for(i = ; i < ; i++)
a[i] = ;
for(i =; i < n; i++) {
scanf("%d", &b[i]);
t = b[i] / ;
switch(t) {
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
case : a[]++; break;
default: break;
}
}
for(i = ; i < ; i++) {
printf("%3d: ", i * );
printStar(a[i]);
}
return ;
}
8、扫描数组,出去数组中含0的元素,返回0的数目
#include<stdio.h>
#include<stdbool.h>
#include<math.h>
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 = ;
if(a[]) num++;
for(i = ; i < n; i++) {
if(!a[i] && i < n - ) {
for(j = i + ; 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 = ; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
int main()
{
int a[] = {,,,,,,,,,,,,};
int n, zeronum;
n = sizeof(a) / sizeof(a[]);
printArray(a, n);
zeronum = RemoveZeroElements(a, n);
printArray(a, n - zeronum);
printf("zreo numbers: %d\n", zeronum);
return ;
}
10、找出一组输入数字中的最小值与最大值
#include<stdio.h>
#include<stdbool.h>
#include<math.h>
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 != -)) {
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 ;
}
11、按要求动态分配一个整型数组并赋值
#include<stdio.h>
#include<stdbool.h>
#include<math.h>
#include<stdlib.h>
int *indexArray(int n)
{
int *p = (int *)malloc(n * sizeof(int));
for(int i = ; i < n; i++)
p[i] = i;
return p;
}
int main()
{
int *ip, n;
scanf("%d", &n);
ip = indexArray(n);
for(int i = ; i < n; i++)
printf("%d ", ip[i]);
printf("\n");
free(ip);
return ;
}
《C程序设计的抽象思维》2.10编程练习(未完)的更多相关文章
- Python之路第一课Day7--随堂笔记(面向对象编程进阶...未完待续 )
本节内容: 面向对象高级语法部分 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 Socket开发基础 作业:开发一个支持多用户在线的FTP程序 面向对象高级语法部分 一.静态方法 通过@s ...
- 使用 Python 进行并发编程 -- asyncio (未完)
参考地址 参考地址 参考地址 Python 2 时代, 高性能的网络编程主要是使用 Twisted, Tornado, Gevent 这三个库. 但是他们的异步代码相互之间不兼容越不能移植. asyn ...
- Siki_Unity_3-8_Lua编程(未完)
Unity 3-8 Lua编程 任务1&2&3:前言 课程内容: Lua从入门到掌握 为之后的xLua和其他热更新方案打下基础 任务4:Lua简介 Lua是轻量小巧的脚本语言--无需编 ...
- 10.31-11.1Test(未完)
10.31-11.1Test 题目 描述 做法 \(BSOJ5177\) 求在\(n\)个数里选\(K\)个的所有方案的异或和之和 按位讨论,组合数算 \(BSOJ5178\) 化简\(\displa ...
- C#中正则表达式编程(未完,待补充)
对于只存储一个匹配,可用Match类: 一般模式: Regex reg = new Regex(string pattern); string str = "###############& ...
- 20172319 《Java程序设计教程》 第10周学习总结
20172319 2018.05.09-05.21 <Java程序设计教程>第10周学习总结 目录 教材学习内容总结 教材学习中的问题和解决过程 代码调试中的问题和解决过程 代码托管 上周 ...
- 已看1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架、多线程(并发编程)、I/O(NIO)、Socket、JDBC、XML、反射等。[泛型]\
1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架.多线程(并发编程).I/O(NIO).Socket.JDBC.XML.反射等.[泛型]\1* ...
- 20155324 《Java程序设计》实验五 网络编程与安全
20155324 <Java程序设计>实验五 网络编程与安全 实验内容 任务一 编写MyBC.java实现中缀表达式转后缀表达式的功能 编写MyDC.java实现从上面功能中获取的表达式中 ...
- Fedora 10编程开发工具
1请问Fedora 10编程开发工具有什么 编辑器就用vim,编译用gcc,当然个人爱好随意 IDE的话推荐eclipse,如果做C/C++的,用codeblocks也是个不错的选择 输入gcc -v ...
- 20155326 《Java程序设计》实验五网络编程与安全实验报告
20155326 <Java程序设计>实验五网络编程与安全实验报告 实验内容 任务一 1.两人一组结对编程: 参考http://www.cnblogs.com/rocedu/p/67667 ...
随机推荐
- [CS231n-CNN] Training Neural Networks Part 1 : parameter updates, ensembles, dropout
课程主页:http://cs231n.stanford.edu/ ___________________________________________________________________ ...
- ArcGIS应用——四种计算图斑面积的方法
ArcGIS中有多种方法可计算出图斑面积,本文总结了四种方法,是否可堪称史上最全? 1.计算几何 本人认为这是最适合非专业人士的方法,直接利用ArcGIS中的计算几何功能进行计算. a.首先添加一do ...
- 当应用程序不是以 UserInteractive 模式运行时显示模式对话框或窗体是无效操作
在Web程序中引用了WinForm的类库引起了 主要是为了在web程序中使用Message.Show()以及SaveFileDialog类,能web程序中引用了WinForm类库 在Visual St ...
- Java知多少(109)数据库更新
数据库更新操作包括数据表创建.删除.以及数据表记录的增加.删除.修改等操作.如果利用数据 SQL命令实现,则利用Statement对旬的executeUpdate()方法,执行SQL的update语句 ...
- IIS限制ip访问
1.禁止IP访问 http://jingyan.baidu.com/article/22fe7ced0462633002617f39.html 2.限制IP访问频率 http://q.cnblogs. ...
- bootstrap插件学习-bootstrap.tooltip.js
先看bootstrap-tooltip.js的结构 var Tooltip = function ( element, options ){} // 构造器 Tooltip.prototype ={} ...
- djngo快速实现--使用Bootstrap
继续django学习之旅,之前我们所做的Django练习前端都非常丑.这节我们使用Bootstrap,顿时使丑陋的页面变成白天鹅. 安装Bootstrap ...
- [python]python元类
这两天在看Django框架,里面的filter实现原理搞不明白,最后发现跟python的元类有关系. 原文:http://stackoverflow.com/questions/100003/what ...
- [Logstash]使用详解
Logstash是一款轻量级的日志搜集处理框架,可以方便的把分散的.多样化的日志搜集起来,并进行自定义的处理,然后传输到指定的位置,比如某个服务器或者文件. 本文针对官方文档进行翻译以及实践,希望有更 ...
- js-对象-2
对象: 对象是一组具有属性和方法的经过组织的数据. 默认对象: 日期对象:(日期基线:1970年1月1日00:00:00) 建立日期对象(实例): 格式:日期对象名称=new Date([日期参数]) ...