代码练习记录,代码可能存在未发现的逻辑错误
Hello World!
#include <stdio.h>
int main (){
printf("************************\n");
printf("* *\n");
printf("* Hello World! *\n");
printf("* *\n");
printf("************************\n");
return 0;
}
三个数中取最大值
#include <stdio.h>
int main(){
int a,b,c,max;
printf("please input a,b,c:\n");
scanf("%d,%d,%d",&a,&b,&c);
max=a;
if(max<b)
max=b;
if(max<c)
max=c;
printf("The largest number is %d\n",max);
return 0;
}
两数之和
#include <stdio.h>
int main() {
int a, b, sum;
printf("please input two number:\n");
scanf("%d%d", &a, &b);
sum = a + b;
printf("The sum is: %d\n", sum);
return 0;
}
两个整数比大小
#include <stdio.h>
int main() {
int a, b, max, min;
printf("please input two number:\n");
scanf("%d%d", &a, &b);
if (a > b) {
max = a;
min = b;
printf("max=%d,min=%d\n", max, min);
} else if (a < b) {
max = b;
min = a;
printf("max=%d,min=%d\n", max, min);
} else if (a = b) {
printf("Two number are equal!\n");
}
return 0;
}
求两个整数中的较大者
#include <stdio.h>
int main()
{
int max(int x, int y);
int a, b, c;
printf("please input two number:");
scanf("%d%d", &a, &b);
c = max(a, b);
printf("max=%d\n", c);
return 0;
}
int max(int x, int y) {
int z;
if (x > y) z = x;
else z = y;
return (z);
}
运行时输入a,b,c三个值,输出其中值最大者
#include <stdio.h>
int main() {
int a, b, c, max;
printf("please input a,b,c:");
scanf("%d,%d,%d", &a, &b, &c);
a > b ? (a > c ? (max = a) : (max = c))
: (b > c ? (max = b) : (max = c));
printf("The largest number is %d\n", max);
return 0;
}
求阶乘n!
#include <stdio.h>
int main() {
int t = 1, n;
printf("please input n:");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
t = t * i;
}
printf("The multiplicative of n is :%d", t);
return 0;
}
求5!
#include <stdio.h>
int main() {
int i = 2, t = 1;
while (i <= 5) {
t = t * i;
i = i + 1;
}
printf("%d\n", t);
return 0;
}
输出成绩在80分以上的学生的学号和成绩
#include <stdio.h>
int main() {
int a[10] = {91, 82, 73, 54, 95, 66, 77, 48, 99, 76},
b[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
printf("Student number and grades of students with scores over 80:\n");
for (int i = 1; i <= 10; i++) {
if (a[i] >= 80) printf("%d,%d\n", b[i], a[i]);
}
return 0;
}
判断2000-2500间的闰年
#include <stdio.h>
int main() {
int n;
printf("Please enter a year from 2000 to 2500:");
scanf("%d", &n);
if (n >= 2000 && n <= 2500) {
if (n % 4 == 0) {
if (n % 100 == 0) {
if (n % 400 == 0) {
printf("%d is a leap year.", n);
} else
printf("%d is not a leap year.", n);
} else {
printf("%d is a leap year.", n);
}
} else {
printf("%d is not a leap year.", n);
}
} else {
printf("Input error, please re-enter a year from 2000 to 2500:");
}
return 0;
}
输出2000-2500间的闰年
#include <stdio.h>
int main() {
int i=0;
printf("The leap years in 2000-2500 are:");
for(int n = 2000; n <= 2500; n++ ) {
if (n % 4 == 0) {
if (n % 100 == 0) {
if (n % 400 == 0) {
printf("'%d'", n);
++i;
}
} else {
printf("'%d'", n);
++i;
}
}
}
printf("\nThere are %d leap years in 2000-2500.",i);
return 0;
}
#include <stdio.h>
int main() {
int year;
printf("Enter year:");
scanf("%d", &year);
if(year > 2000 && year <2500)
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? printf("yes") : printf("no");
else printf("error");
return 0;
}
求多项式1-1/2+1/3-1/4+1/5-1/6+···+1/99-1/100
求:
1
?
1
2
+
1
3
?
1
4
+
1
5
?
1
6
+
?
+
1
97
?
1
98
+
1
99
?
1
100
\displaystyle {1 - \frac{1}{2} + \frac{1}{3} - \frac{1}{4} + \frac{1}{5} - \frac{1}{6} +\cdots +\frac{1}{97} - \frac{1}{98} + \frac{1}{99} - \frac{1}{100}}
1?21?+31??41?+51??61?+?+971??981?+991??1001?的值。
#include <stdio.h>
int main() {
int sign = 1;
double deno = 2.0, sum = 1.0, term;
while (deno <= 100) {
sign = -sign;
term = sign / deno;
sum = sum + term;
deno = deno + 1;
}
printf("%f\n", sum);
return 0;
}
温度转换:华氏法(℉)转换为摄氏法(℃)
#include <stdio.h>
int main() {
float f, c;
printf("f=");
scanf("%f", &f);
c = (5.0 / 9) * (f - 32);
printf("f=%f\nc=%f", f, c);
return 0;
}
编辑器中各数据类型在内存中占用的存储单元长度
#include <stdio.h>
int main() {
int sizeof_int, sizeof_short, sizeof_long, sizeof_long_long_int,
sizeof_char, sizeof_float, sizeof_double;
sizeof_int = sizeof(int);
sizeof_short = sizeof(short);
sizeof_long = sizeof(long);
sizeof_long_long_int = sizeof(long long int);
sizeof_char = sizeof(char);
sizeof_float = sizeof(float);
sizeof_double = sizeof(double);
printf(
"int=%d\nshort int=%d\nlong int=%d\nlong long int=%d\n"
"char=%d\nfloat=%d\ndouble=%d\n",
sizeof_int, sizeof_short, sizeof_long, sizeof_long_long_int,
sizeof_char, sizeof_float, sizeof_double);
return 0;
}
无符号位整形数据用“%u”格式输出
#include <stdio.h>
int main() {
unsigned int a;
a=2147483648;
printf("%u",a);
}
字符常量有符号无符号
#include <stdio.h>
int main() {
unsigned char a = 128;
signed char b = 128;
printf("%c = %d\n", a, a);
printf("%c = %d\n", b, b);
}
大小写字母转换
#include <stdio.h>
int main() {
char letter, big, small;
printf("Enter a letter:");
scanf("%c", &letter);
if (letter > 64 && letter < 91) {
big = letter;
small = big + 32;
} else if (letter > 96 && letter < 123) {
small = letter;
big = small - 32;
} else {
printf("Input error");
}
printf("big=%c\nsmall=%c", big, small);
return 0;
}
输入的字母统一以小写形式输出
#include <stdio.h>
int main() {
char ch;
printf("Enter a letter:");
scanf("%c", &ch);
if (ch > 'A' && ch < 'Z' || ch > 'a' && ch < 'z') {
ch = (ch > 'A' && ch < 'Z') ? (ch + 32) : ch;
printf("%c", ch);
} else
printf("error");
return 0;
}
求已知三角形三边长,求三角形面积
已知三角形面积公式:
a
r
e
a
=
s
(
s
?
a
)
(
s
?
b
)
(
s
?
c
)
area=\sqrt{s(s-a)(s-b)(s-c)}
area=s(s?a)(s?b)(s?c)
? 其中,
s
=
a
+
b
+
c
2
s=\frac{a+b+c}{2}
s=2a+b+c?,a,b,c为三角形的边长。
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, s, area;
printf("Enter the triangle side length a,b,c:");
scanf("%lf%lf%lf", &a, &b, &c);
if (a + b > c && a + c > b && b + c > b) {
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("a=%f\tb=%f\tc=%f\narea=%f\n", a, b, c, area);
} else {
printf("error");
}
return 0;
}
求一元二次方程
a
x
2
+
b
x
+
c
=
0
ax^2+bx+c=0
ax2+bx+c=0的根。a,b,c由键盘输入,设
b
2
?
4
a
c
≥
0
b^2-4ac≥0
b2?4ac≥0.
已知,当
b
2
?
4
a
c
≥
0
b^2-4ac≥0
b2?4ac≥0时,一元二次方程
a
x
2
+
b
x
+
c
=
0
ax^2+bx+c=0
ax2+bx+c=0有根。 实数根:
x
1
=
?
b
+
b
2
?
4
a
c
2
a
,
x
2
=
?
b
?
b
2
?
4
a
c
2
a
x_1=\frac{-b+\sqrt{b^2-4ac}}{2a},x_2=\frac{-b-\sqrt{b^2-4ac}}{2a}
x1?=2a?b+b2?4ac
??,x2?=2a?b?b2?4ac
?? 令
p
=
?
b
2
a
,
q
=
b
2
?
4
a
c
2
a
p=\frac{-b}{2a},q=\frac{\sqrt{b^2-4ac}}{2a}
p=2a?b?,q=2ab2?4ac
??,则
x
1
=
p
+
q
,
x
2
=
p
?
q
x_1=p+q,x_2=p-q
x1?=p+q,x2?=p?q.
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, disc, x1, x2, p, q;
printf("Please enter a, b, c:");
scanf("%lf%lf%lf", &a, &b, &c);
disc = b * b - 4 * a * c;
if (disc >= 0) {
p = -b / (2.0 * a);
q = sqrt(disc) / (2.0 * a);
x1 = p + q;
x2 = p - q;
printf("x1=%7.2f\nx2=%7.2f", x1, x2);
} else {
printf("The equation has no real root.");
}
return 0;
}
对输入的三个数按从小到大的顺序排列
#include <stdio.h>
int main() {
int a, b, c, t;
printf("Enter three number:");
scanf("%d%d%d", &a, &b, &c);
if (a > b) {
t = a;
a = b;
b = t;
}
if (a > c) {
t = a;
a = c;
c = t;
}
if (b > c) {
t = b;
b = c;
c = t;
}
printf("%d<%d<%d", a, b, c);
return 0;
}
阶跃函数
y
=
{
?
1
(x<0)
0
(x=0)
1
(x>0)
y=\begin{cases}-1& \text{(x<0)}\\0 & \text{(x=0)}\\1& \text{(x>0)}\end{cases}
y=???????101?(x<0)(x=0)(x>0)?输入一个x值,输出相应y值。
#include <stdio.h>
int main() {
float x;
int y;
printf("Enter x:");
scanf("%f", &x);
if (x < 0) {
y = -1;
} else if (x == 0) {
y = 0;
} else {
y = 1;
}
printf("y=%d", y);
return 0;
}
#include <stdio.h>
int main() {
float x;
int y;
printf("Enter x:");
scanf("%f", &x);
y = (x < 0) ? -1 : ((x == 0) ? 0 : 1);
printf("y=%d", y);
return 0;
}
成绩等级
#include <stdio.h>
int main() {
char grade;
scanf("%c",&grade);
printf("Your score:");
switch(grade){
case'A':printf("85~100\n");break;
case'B':printf("70~84\n");break;
case'C':printf("60~69\n");break;
case'D':printf("<60\n");break;
default:printf("enter data error!\n");
}
return 0;
}
|