《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 ...
随机推荐
- Making Your ActionBar Not Boring
这篇文章转自国外一个技术大牛的博客,首先感谢这位大牛的无私奉献. Android应用中有一名位 Google书报摊的应用,他实现了一种新的ActionBar风格.当用户初始进入该界面的时候,为一个透明 ...
- 自定义ISPF面板
1)登录的时候可以看到登录执行的PROCEDURE,此处为DBSPROC 2.登录后,进入SDSF,再进入LOG,输入命令TOP,再输入命令F JOB,按F11把屏幕向右翻页,看到哪下界面 找到//I ...
- 理解js中的自由变量以及作用域的进阶
如果你不知道什么是作用域,建议你先看什么是作用域链,什么是原型链.这篇文章,因为这些内容都是有关联性的. 什么是自由变量? 如我在全局中定义了一个变量a,然后我在函数中使用了这个a,这个a就可以称之为 ...
- eclipse开发web应用程序步骤(图解)
*运行环境(也就是服务器的选择) 环境搭建好之后开始编写web程序!然后右键->Run as -> Run on Server!
- [Node.js] Node + Redis 实现分布式Session方案
原文地址: http://www.moye.me/?p=565 Session是什么? Session 是面向连接的状态信息,是对 Http 无状态协议的补充. Session 怎么工作? Sessi ...
- Android学习笔记之dispatchTouchEvent和OnInterceptTouchEvent和OnTouchEvent三个方法之间的联系...
PS:好久没有写博客了,项目正式开始启动了,但是怎么也打不起精神来...可能还是不适应放假留校...这下一年只能回家一次了...伤感...写篇博客舒坦下... 学习内容: Android中disp ...
- IOS开发UI基础UITableView的属性
UITableView UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped <UITableViewDataSour ...
- DOM中 property 和 attribute 详解
被问到 property 和 attribute 的区别,想来也是要好好看一下. 一.基本概念区别 其实Attribute和Property这两个单词,翻译出来都是“属性”,<js高级程序设计& ...
- 有关C#标签Attribute的熟悉
Attribute 简单用法: 最近用到了,所以静下心来找些资料看了一下,终于把这东西搞清楚了. 一.什么是Attribute 先看下面的三段代码: 1.自定义Attribute类:VersionAt ...
- Dev 饼图
// 添加引用命名空间 using DevExpress.XtraCharts; /* *具体步骤:(1)先构建饼图对象的数据源DataTable * (2)再设置饼图对象的相关参数 * (3)饼图空 ...