某单据1x4x7C语言编程,C语言入门必学——刚需帖子详情 - 网易云课堂

本文主要是介绍某单据1x4x7C语言编程,C语言入门必学——刚需帖子详情 - 网易云课堂,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

A164DD3D488647169E193BC86D0CEBB5.png?imageView&thumbnail=120y120&quality=100_49x49x1x95.png

置顶

精华

老师参与

计算元音个数:

/*

* 从终端输入若干的字符,对其中的元音字母进行统计

*/

#include 

#include 

#define BUFFER 128

int is_vowel(char);

int main(void)

{

char str[BUFFER];

printf("Input string: ");

scanf("%s", str);

int count = 0;

int len = strlen(str);

for (int i = 0; i 

if (!is_vowel(str[i]))

count++;

printf("The string have %d vowel words.\n", count);

return 0;

}

int is_vowel(char c)

{

static const char vowel[] = "aeiou";

int len = sizeof vowel;

for (int i = 0; i 

if (vowel[i] == c)

return 0;

}

return 1;

}

[lhf@localhost work]$ ./vowel

Input string: hello world

The string have 2 vowel words.

A164DD3D488647169E193BC86D0CEBB5.png?imageView&thumbnail=120y120&quality=100_49x49x1x95.png

置顶

精华

老师参与

1000以内的质数:

/*

* 求出1000以内的所有质数

*

*/

#include 

#define MAX 1000

int is_prime(int);

void print_prime(int *, int);

int main(void)

{

int p[MAX] = {2};

int count = 0;

for (int i = 3; i <= MAX; i += 2) {

if (!is_prime(i)) {

p[++count] = i;

}

}

print_prime(p, count);

return 0;

}

int is_prime(int n)

{

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

if (!(n % i))

return 1;

}

return 0;

}

void print_prime(int *p, int n)

{

for (int i = 0; i 

printf("%3d", p[i]);

printf("%c", (i + 1) % 10 ? ' ' : '\n');

}

printf("\n");

return;

}

[lhf@localhost work]$ ./prime

2   3   5   7  11  13  17  19  23  29

31  37  41  43  47  53  59  61  67  71

73  79  83  89  97 101 103 107 109 113

127 131 137 139 149 151 157 163 167 173

179 181 191 193 197 199 211 223 227 229

233 239 241 251 257 263 269 271 277 281

283 293 307 311 313 317 331 337 347 349

353 359 367 373 379 383 389 397 401 409

419 421 431 433 439 443 449 457 461 463

467 479 487 491 499 503 509 521 523 541

547 557 563 569 571 577 587 593 599 601

607 613 617 619 631 641 643 647 653 659

661 673 677 683 691 701 709 719 727 733

739 743 751 757 761 769 773 787 797 809

811 821 823 827 829 839 853 857 859 863

877 881 883 887 907 911 919 929 937 941

947 953 967 971 977 983 991

A164DD3D488647169E193BC86D0CEBB5.png?imageView&thumbnail=120y120&quality=100_49x49x1x95.png

置顶

精华

老师参与

水仙花数:

/*

* 求出1000以内的水仙花数:153 = 1^3 + 5^3 + 3^3

*

*/

#include 

#define MAX 1000

int main(void)

{

for (int i = 0; i <= MAX; i++) {

int sum = 0;

int narc = i;

int mod;

do {

mod = narc % 10;

sum += mod * mod * mod;

} while (narc /= 10);

if (sum == i)

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

}

return 0;

}

[lhf@localhost work]$ ./narc

0

1

153

370

371

407

A164DD3D488647169E193BC86D0CEBB5.png?imageView&thumbnail=120y120&quality=100_49x49x1x95.png

置顶

精华

老师参与

字符串问题:

/*

* 在终端上实现如下输出:

* ABCDEF

* BCDEF

* CDEF

* DEF

* EF

* F

*

*/

#include 

int main(void)

{

const char str[] = "ABCDEF";

char *p;

for (p = str; *p != '\0'; p++)

puts(p);

return 0;

}

[lhf@localhost work]$ ./disappear

ABCDEF

BCDEF

CDEF

DEF

EF

F

A164DD3D488647169E193BC86D0CEBB5.png?imageView&thumbnail=120y120&quality=100_49x49x1x95.png

置顶

精华

老师参与

圆的面积问题:

/*

* 从半径为1开始,输出圆的面积,直到面积大于100为止。

*

*/

#include 

#include 

#define PI 3.14

#define MAX 100

int main(void)

{

double area = 0;

for (int i = 1; true; i++) {

area = PI * i * i;

if (area <= MAX)

printf("Radius of %2d, area of circle is %.2lf\n",

i, area);

else

break;

}

return 0;

}

[lhf@localhost work]$ ./circle

Radius of  1, area of circle is 3.14

Radius of  2, area of circle is 12.56

Radius of  3, area of circle is 28.26

Radius of  4, area of circle is 50.24

Radius of  5, area of circle is 78.50

A164DD3D488647169E193BC86D0CEBB5.png?imageView&thumbnail=120y120&quality=100_49x49x1x95.png

置顶

精华

老师参与

九九乘法表

/*

* 输出九九乘法表

*

*/

#include 

int main(void)

{

for (int i = 0; i 

for (int j = 0; j <= i; j++) {

printf("%dx%d = %-2d  ", j + 1, i + 1, (i + 1) * (j + 1));

}

printf("\n");

}

return 0;

}

[lhf@localhost work]$ ./9x9

1x1 = 1

1x2 = 2   2x2 = 4

1x3 = 3   2x3 = 6   3x3 = 9

1x4 = 4   2x4 = 8   3x4 = 12  4x4 = 16

1x5 = 5   2x5 = 10  3x5 = 15  4x5 = 20  5x5 = 25

1x6 = 6   2x6 = 12  3x6 = 18  4x6 = 24  5x6 = 30  6x6 = 36

1x7 = 7   2x7 = 14  3x7 = 21  4x7 = 28  5x7 = 35  6x7 = 42  7x7 = 49

1x8 = 8   2x8 = 16  3x8 = 24  4x8 = 32  5x8 = 40  6x8 = 48  7x8 = 56  8x8 = 64

1x9 = 9   2x9 = 18  3x9 = 27  4x9 = 36  5x9 = 45  6x9 = 54  7x9 = 63  8x9 = 72  9x9 = 81

A164DD3D488647169E193BC86D0CEBB5.png?imageView&thumbnail=120y120&quality=100_49x49x1x95.png

置顶

精华

老师参与

计算N个数的和:

/*

* 从终端输入N个数(以字母Q/q作为终止),求和。

*

*/

#include 

#include 

#define BUFFER 128

int main(void)

{

char line[BUFFER];

int sum = 0;

int num = 0;

printf("Input N number, without 'Q' or 'q', summarize.\n");

do {

printf("Input a number: ");

scanf("%s", line);

if (!strcmp(line, "Q") || !strcmp(line, "q"))

break;

sum += atoi(line);

} while (true);

printf("sum is %d\n", sum);

return 0;

}

[lhf@localhost work]$ ./sum

Input N number, without 'Q' or 'q', summarize.

Input a number: 2

Input a number: 4

Input a number: 43

Input a number: 23

Input a number: q

sum is 72

A164DD3D488647169E193BC86D0CEBB5.png?imageView&thumbnail=120y120&quality=100_49x49x1x95.png

置顶

精华

老师参与

计算奇数和偶数问题

/*

* 从终端输入数据,直到输入0值为止,计算出其中偶数的个数和平均值,以及

* 奇数的个数和平均值。

*/

#include 

#include 

int main(void)

{

int num;

int count_odd = 0, count_even = 0;

long sum_odd = 0, sum_even = 0;

float average_odd, average_even;

do {

printf("Input a Number(until 0): ");

scanf("%d", &num);

if (!num)

break;

else if (num % 2) {

count_odd++;

sum_odd += num;

} else {

count_even++;

sum_even += num;

}

} while (true);

average_odd = (double)sum_odd / count_odd;

average_even = (double)sum_even / count_even;

printf("%d numbers of odd, average is %.2f\n", count_odd, average_odd);

printf("%d numbers of even, average is %.2f\n", count_even, average_even);

return 0;

}

[lhf@localhost work]$ ./odd_even

Input a Number(until 0): 2

Input a Number(until 0): 4

Input a Number(until 0): 3

Input a Number(until 0): 5

Input a Number(until 0): 8

Input a Number(until 0): 9

Input a Number(until 0): 0

3 numbers of odd, average is 5.67

3 numbers of even, average is 4.67

A164DD3D488647169E193BC86D0CEBB5.png?imageView&thumbnail=120y120&quality=100_49x49x1x95.png

置顶

精华

老师参与

fibonacci 前40项

/*

* 写出fibonacci数列的前40项(不能用数组实现)

* 0, 1, 1, 2, 3, 5, 8, 13, 21...

*

*/

#include 

int main(void)

{

int fib = 0, fibo = 1;

int tmp;

for (int i = 0; i 

printf("%ld ", fib);

tmp = fibo;

fibo = fib + fibo;

fib = tmp;

if (!(i % 10))

putchar('\n');

}

putchar('\n');

return 0;

}

[lhf@localhost work]$ ./fibonacci

0

1 1 2 3 5 8 13 21 34 55

89 144 233 377 610 987 1597 2584 4181 6765

10946 17711 28657 46368 75025 121393 196418 317811 514229 832040

1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986

A164DD3D488647169E193BC86D0CEBB5.png?imageView&thumbnail=120y120&quality=100_49x49x1x95.png

置顶

精华

老师参与

银行利率问题:

/**

* A以每年10%的单利息投资了100美元,B以每年5%的复合利息投资了100美元,

* 编写一程序,计算需要多少年B的投资总额才会超过A,并且显示出各自的资产总额

*

*/

#include 

#define BASE_A 100

#define BASE_B 100

int main(void)

{

int year = 1;

double assets_a = BASE_A, assets_b = BASE_B;

while (assets_a >= assets_b) {

assets_a += BASE_A * 0.1;

assets_b += assets_b * 0.05;

year++;

}

printf("%d year late assets of A 

year, assets_a, assets_b);

return 0;

}

[lhf@localhost work]$ ./assets

28 year late assets of A 

A164DD3D488647169E193BC86D0CEBB5.png?imageView&thumbnail=120y120&quality=100_49x49x1x95.png

置顶

精华

老师参与

百钱买百鸡:

/*

* 百钱买百鸡:鸡翁一,值钱五;鸡母一,值钱三;三鸡雏,值钱一,百钱买百鸡,

* 问鸡翁,鸡母,鸡雏各几何?

*

*/

#include 

#define MONEY 100

#define MANY 100

int main(void)

{

int cock = 5;

int hen = 3;

int chick = 1;  // 0.333...

int max_cock = MONEY / cock;

int max_hen = MONEY / hen;

int max_chick = MONEY * 3;

int i = 0;

float money_cock;

float money_hey;

float money_chick;

int money = 0;

int count = 0;

for (int i = 0; i <= max_cock; i++) {

money_cock = cock * i;

for (int j = 0; j <= max_hen; j++) {

money_hey = hen * j;

for (int k = 0; k <= max_chick; k += 3) {

// because chick = 1

money_chick = chick * k / 3;

money = money_cock + money_hey + money_chick;

if ((money == MONEY) && (i + j + k == MONEY))

printf("[%d] cock: %-2d    hen: %-2d                                                    chick: %-2d\n", ++count, i, j, k);

}

}

}

return 0;

}

这篇关于某单据1x4x7C语言编程,C语言入门必学——刚需帖子详情 - 网易云课堂的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/336415

相关文章

Go语言使用sync.Mutex实现资源加锁

《Go语言使用sync.Mutex实现资源加锁》数据共享是一把双刃剑,Go语言为我们提供了sync.Mutex,一种最基础也是最常用的加锁方式,用于保证在任意时刻只有一个goroutine能访问共享... 目录一、什么是 Mutex二、为什么需要加锁三、实战案例:并发安全的计数器1. 未加锁示例(存在竞态)

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

Python实现Word转PDF全攻略(从入门到实战)

《Python实现Word转PDF全攻略(从入门到实战)》在数字化办公场景中,Word文档的跨平台兼容性始终是个难题,而PDF格式凭借所见即所得的特性,已成为文档分发和归档的标准格式,下面小编就来和大... 目录一、为什么需要python处理Word转PDF?二、主流转换方案对比三、五套实战方案详解方案1:

C# async await 异步编程实现机制详解

《C#asyncawait异步编程实现机制详解》async/await是C#5.0引入的语法糖,它基于**状态机(StateMachine)**模式实现,将异步方法转换为编译器生成的状态机类,本... 目录一、async/await 异步编程实现机制1.1 核心概念1.2 编译器转换过程1.3 关键组件解析

Go语言使用select监听多个channel的示例详解

《Go语言使用select监听多个channel的示例详解》本文将聚焦Go并发中的一个强力工具,select,这篇文章将通过实际案例学习如何优雅地监听多个Channel,实现多任务处理、超时控制和非阻... 目录一、前言:为什么要使用select二、实战目标三、案例代码:监听两个任务结果和超时四、运行示例五

C语言中%zu的用法解读

《C语言中%zu的用法解读》size_t是无符号整数类型,用于表示对象大小或内存操作结果,%zu是C99标准中专为size_t设计的printf占位符,避免因类型不匹配导致错误,使用%u或%d可能引发... 目录size_t 类型与 %zu 占位符%zu 的用途替代占位符的风险兼容性说明其他相关占位符验证示

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

C语言进阶(预处理命令详解)

《C语言进阶(预处理命令详解)》文章讲解了宏定义规范、头文件包含方式及条件编译应用,强调带参宏需加括号避免计算错误,头文件应声明函数原型以便主函数调用,条件编译通过宏定义控制代码编译,适用于测试与模块... 目录1.宏定义1.1不带参宏1.2带参宏2.头文件的包含2.1头文件中的内容2.2工程结构3.条件编

Go语言并发之通知退出机制的实现

《Go语言并发之通知退出机制的实现》本文主要介绍了Go语言并发之通知退出机制的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、通知退出机制1.1 进程/main函数退出1.2 通过channel退出1.3 通过cont

Go语言编译环境设置教程

《Go语言编译环境设置教程》Go语言支持高并发(goroutine)、自动垃圾回收,编译为跨平台二进制文件,云原生兼容且社区活跃,开发便捷,内置测试与vet工具辅助检测错误,依赖模块化管理,提升开发效... 目录Go语言优势下载 Go  配置编译环境配置 GOPROXYIDE 设置(VS Code)一些基本