本文主要是介绍某单据1x4x7C语言编程,C语言入门必学——刚需帖子详情 - 网易云课堂,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
置顶
精华
老师参与
计算元音个数:
/*
* 从终端输入若干的字符,对其中的元音字母进行统计
*/
#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.
置顶
精华
老师参与
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
置顶
精华
老师参与
水仙花数:
/*
* 求出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
置顶
精华
老师参与
字符串问题:
/*
* 在终端上实现如下输出:
* 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
置顶
精华
老师参与
圆的面积问题:
/*
* 从半径为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
置顶
精华
老师参与
九九乘法表
/*
* 输出九九乘法表
*
*/
#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
置顶
精华
老师参与
计算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
置顶
精华
老师参与
计算奇数和偶数问题
/*
* 从终端输入数据,直到输入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
置顶
精华
老师参与
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
置顶
精华
老师参与
银行利率问题:
/**
* 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
置顶
精华
老师参与
百钱买百鸡:
/*
* 百钱买百鸡:鸡翁一,值钱五;鸡母一,值钱三;三鸡雏,值钱一,百钱买百鸡,
* 问鸡翁,鸡母,鸡雏各几何?
*
*/
#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语言入门必学——刚需帖子详情 - 网易云课堂的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!