本文地址: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 ;
}

最新文章

  1. rsync同步
  2. ios 缺少合规证明
  3. excel导出字符串
  4. [git]用pelican搞一个自己的blog(已完成)
  5. html span标签 不换行(有时span带中文时候是可以自动换行的)
  6. HDU 2236:无题II(二分搜索+二分匹配)
  7. linux软件下载及安装
  8. JavaScript个人学习记录总结(二)——验证表单输入之模式匹配
  9. yii缓存设置使用
  10. POJ 1364 King
  11. 九度OJ 1076 N的阶乘 -- 大数运算
  12. linux搜索命令
  13. ffmpeg编译时freetype2 not found错误
  14. 关于javascript的沙箱模式以及缓存方法
  15. Python基础入门教程(4)(数据类型)
  16. Car HDU - 5935
  17. 一个 developer 的进化
  18. legend_noa 的 EMACS配置
  19. Database Management System 基础01:管理自己的任何事
  20. eclipse中安装windowbuilder插件、应用及卸载

热门文章

  1. 云计算之路-阿里云上:消灭“黑色n秒”第二招——给w3wp进程指定CPU核
  2. [python]自问自答:python -m参数?
  3. Web Service常识
  4. 论python中的作用域
  5. 三分套三分 --- HDU 3400 Line belt
  6. 重构第18天 用条件语句来代替异常(Replace exception with conditional)
  7. Winform开发框架之权限管理系统改进的经验总结(4)--用户分级管理
  8. Equeue初识
  9. Java集合Iterator迭代器的实现
  10. session与cookie的异同