文章目录
1.判断整数奇偶性
#include<stdio.h>
int main()
{
int a;
while(scanf("%d", &a)!=EOF)
{
for (int i = 0; i <= 1000000;i+=2)
{
if(a==i)
{
printf("Even\n");
}
}
for (int j = 1; j <= 1000000;j+=2)
{
if(a==j)
{
printf("Odd\n");
}
}
}
return 0;
}
2.最高分数
#include<stdio.h>
int main()
{
int score[3] = {0};
for (int i = 0; i < 3;i++)
{
scanf("%d", &score[i]);//输入三个分数
}
int swap;
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 3;j++)//遍历数组比较
{
if(score[i]<score[j])
{
swap = score[i];
score[i] = score[j];
score[j] = swap;
}
}
}
printf("%d", score[0]);//最大数放在数组第一个位置
}
3.判断是元音还是辅音
#include<stdio.h>
int main()
{
char a[10] = {'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u'};
char ch = {0};
int i = 0;
while (scanf("%c", &ch) != EOF)
{
if(ch!='\n')
{
for (i = 0; i < 10;i++)
{
if(ch==a[i])
{
printf("Vowel\n");
i = 11;//跳出循环
}
}
if(i==10)//为假
{
printf("Consonant\n");
}
}
}
return 0;
}
4.判断是不是字母
#include <stdio.h>
int main() {
char a=0;
while ((a=getchar()) != EOF)
{
getchar();
if(a>='A'&&a<='Z'||a>='a'&&a<='z')
{
printf("%c is an alphabet.\n",a);
}
else {
printf("%c is not an alphabet.\n",a);
}
}
return 0;
}
5.字母大小写转换
#include<stdio.h>
int main()
{
char arr=0;
while(scanf("%c",&arr)!=EOF)
{
getchar();
if(arr>='A'&&arr<='Z')
{
arr+=32;//ASCII
}
else if(arr>='a'&&arr<='z')
{
arr-=32;//ASCII
}
printf("%c\n",arr);
}
return 0;
}
6.判断两个数的大小关系
#include <stdio.h>
int main() {
long int a=0;
long int b=0;//long int类型
while(scanf("%ld %ld",&a,&b)!=EOF)
{
if(a>b)
{
printf("%ld>%ld\n",a,b);
}
else if(a<b)
{
printf("%ld<%ld\n",a,b);
}
else {
printf("%ld=%ld\n",a,b);
}
}
return 0;
}
7.计算单位阶跃函数
#include <stdio.h>
int main() {
signed int t=0;
while(scanf("%d",&t)!=EOF)
{
if(t>0)
{
printf("1\n");
}
if(t==0)
{
printf("0.5\n");
}
if(t<0)
{
printf("0\n");
}
}
return 0;
}
8.三角形判断
#include<stdio.h>
int main()
{
int a,b,c;
while(scanf("%d %d %d",&a,&b,&c)!=EOF)
{
//三角形三边的关系:两边之和大于第三边,两边之差小于第三边
if(a + b > c && a + c > b && b + c > a )
{
if(a==b&&b==c)//等边三条边相等
printf("Equilateral triangle!\n");
else if(a==b||b==c||a==c)//等腰,两条边相等
printf("Isosceles triangle!\n");
else//其余三角形
printf("Ordinary triangle!\n");
}
//第一个if语句以外不为三角形
else {
printf("Not a triangle!\n");
}
}
return 0;
}
9. 衡量人体胖瘦程度
#include<stdio.h>
#include<math.h>
int main()
{
int weight;
float height;
while(scanf("%d %f",&weight,&height)!=EOF)
{
float height2 =(height/100)*(height/100);//换算为单位
float BMI=weight/height2;
if(BMI<18.5)
printf("Underweight\n");
else if (BMI>=18.5&&BMI<=23.9)
printf("Normal\n");
else if(BMI>23.9&&BMI<=27.9)
printf("Overweight\n");
else
printf("Obese\n");
}
return 0;
}
10.计算一元二次方程
#include<stdio.h>
#include<math.h>
int main(){
float a = 0;
float b = 0;
float c = 0;
while(scanf("%f %f %f",&a, &b, &c)!= EOF ){
if(a == 0){
printf("Not quadratic equation\n");
}else{
float t = b*b - 4*a*c;
if(t==0){
//两个实根相等
float mp = -b + sqrt(t);
if(mp == 0){
printf("x1=x2=%.2f\n",mp);
}else{
printf("x1=x2=%.2f\n",(-b+sqrt(t))/2.0/a);
}
}else if(t > 0){
//两个实根不等
printf("x1=%.2f;x2=%.2f\n",(-b-sqrt(t))/2.0/a,(-b+sqrt(t))/2.0/a);
}else{
//虚根
float xu = sqrt(-t)/2.0/a;
if(xu < 0) {
//如果是负数,取反作为绝对值,abs取绝对值会丢失精度
xu = -xu;
}
printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",-b/2/a,xu,-b/2/a,xu);
}
}
}
return 0;
}
最后一题因为太晚了,复制了一份大佬的解析,明天复习一下(#.#)