C语言练习题1

1、设计一个程序,从键盘输入3个整数,按由大到小的顺序输出。

// 方案一:将最大的数值赋给a,通过连续的三个比较排序赋值
#include<stdio.h>
int main()
{
	int a, b, c, temp;
	printf("请输入三个整数(使用空格相隔)\n");
	scanf("%d%d%d", &a, &b, &c);
	if (a < b)
	{
		temp = a;
		a = b;
		b = temp;
	}
	if (a < c)
	{
		temp = a;
		a = c;
		c = temp;
	}
	if (b < c)
	{
		temp = b;
		b = c;
		c = temp;
	}
	printf("%d %d %d \n", a, b, c);
	return 0;
}

2、求1+3+5+7+……+95+97+99的和。

#include<stdio.h>
int main()
{
	int i,num=0;
	for (i = 1; i < 100; i++)
	{
		num += i;
		i += 2;
	}
	printf("%d\n", num);
	return 0;
}

3、写一个函数,从键盘输入一个整数,如果该整数为素数,则输出“此整数为素数”,否则输出“整数非素数”。(要求从主函数输入整数)

#include<stdio.h>
#include<math.h>
void prime_number(int num);
int main()
{
	int num;
	do {
		printf("请输入一个整数(n>0):\n");
		scanf("%d", &num);
	} while (!num);           //对输入数据进行错误排除,但是还有待改进,没有排除负数的检查
	prime_number(num);
	return 0;
}
void prime_number(int num)
{
	int i;
	for (i = 2; i < sqrt(num); i++)
	{
		if (num % i == 0)           //说明有因子可以让num整除,不是质数
			break;
	}
	if (i > sqrt(num))
		printf("此整数为素数\n");
	else
		printf("整数非素数\n");
}

4、从键盘输入10个整数,保存在一个数组中,将这10个数逆序输出,然后求出这10个数的和并输出。

#include<stdio.h>
int main()
{
	int num[15], sum = 0, i;
	printf("请输入10个整数:\n");
	for (i = 0; i < 10; i++)
	{
		scanf("%d", &num[i]);
	}
	for (i = 9; i >= 0; i--)
	{
		printf("%d  ", num[i]);         //逆序输出数组
		sum += num[i];           //求和
	}
	printf("\n这10个整数的和是:%d\n", sum);

	return 0;
}

5、输入n个国家的英文名,要求按照字母先后顺序,并按照顺序输出。

#include<stdio.h>
#include<string.h>
int main()
{
	int count = 0, i, j;
	char nation_name[10][50] = { 0 }, temp[50] = { 0 };
	printf("请输入将要输入的国家名称个数:\n");
	scanf("%d", &count);
	printf("请输入国家名称(中间用空格空开):\n");
	for (i = 0; i < count; i++)
		scanf("%s", nation_name[i]);

	/* 此处可以使用nation_name[i][0]进行比较判断,然后排序
	也可以使用strcmp函数进行判断:strcmp(str1,str2),对两字符串
	相同位置的字符进行比较,若相等,返回0,前者大则返回正整数,否则返回负整数
	stricmp 函数:忽略大小写后的大小比较,不会出现相同的情况,出现则不交换
	*/
	for (j = 0; j + 1 < count; j++)
	{
		for (i = 0; i + 1 < count; i++)        //使用循环嵌套对每一个字符串进行从前往后判断
		{      //时间复杂度较高,且存在问题:当第一个字符串换一次到第二个位置时没有再次交换,这样到第二个
			if (stricmp(nation_name[i], nation_name[i + 1]) > 0)    //大循环时会再次循环判断,
			{                                      //增加程序无效运行时间,可优化
				strcpy(temp, nation_name[i]);
				strcpy(nation_name[i], nation_name[i + 1]);
				strcpy(nation_name[i + 1], temp);
			}
		}
	}
	for (i = 0; i < count; i++)
		printf("%s\n", nation_name[i]);

	return 0;
}

6、某年级共有4个班,每班各有30名学生,有6个科目的考试成绩,要求输出每门课程最高分的学生的班级、学号和姓名,并计算各班每个学生的平均成绩并输出。

//某年级共有4个班,每班各有30名学生,有6个科目的考试成绩,要求输出每门课程最高分的学生的班级、学号和姓名,
//并计算各班每个学生的平均成绩并输出。

#include<stdio.h>

struct student     //学生信息结构体
{
	char class_number[30];
	char student_ID[30];
	char student_name[20];
	int Chinese_score, Math_score, English_score;
	int History_score, Geography_score, Politics_score;
};

int main()
{
	struct student stu[130];
	int i, max_score, flag, count = 0;
	float average_score;

	printf("请确认你要输入的学生信息的个数:\n");
	scanf("%d", &count);
	printf("请输入学生的班级号(数字)、学号、姓名、语文、数学、英语、历史、地理、政治各科成绩");
	printf("(使用空格隔开),并且每三个信息之后用Enter键换行:\n");
	for (i = 0; i < count; i++)
	{
		scanf("%s%s%s", stu[i].class_number, stu[i].student_ID, stu[i].student_name);
		scanf("%d%d%d", &stu[i].Chinese_score, &stu[i].Math_score, &stu[i].English_score);
		scanf("%d%d%d", &stu[i].History_score, &stu[i].Geography_score, &stu[i].Politics_score);
	}

	for (i = 0; i < count; i++)
	{
		average_score = (stu[i].Chinese_score + stu[i].Math_score + stu[i].English_score + stu[i].History_score + stu[i].Geography_score + stu[i].Politics_score) / 6.0;
		printf("学生信息:%s   %s   %s", stu[i].class_number, stu[i].student_ID, stu[i].student_name);
		printf("    该生平均成绩是:%f\n", average_score);
	}

	//懒得写函数了
	max_score = stu[0].Chinese_score;
	flag = 0;                               //使用flag标记最高分学生的位置,否则直接用i的话会越界
	for (i = 1; i < count; i++)
	{
		if (max_score < stu[i].Chinese_score)
		{
			max_score = stu[i].Chinese_score;
			flag = i;
		}
	}
	printf("语文成绩的最高分是:%d   %s   %s   %s\n", max_score, stu[flag].class_number, stu[flag].student_ID, stu[flag].student_name);

	max_score = stu[0].Math_score;
	flag = 0;
	for (i = 1; i < count; i++)
	{
		if (max_score < stu[i].Math_score)
		{
			max_score = stu[i].Math_score;
			flag = i;
		}
	}
	printf("数学成绩的最高分是:%d   %s   %s   %s\n", max_score, stu[flag].class_number, stu[flag].student_ID, stu[flag].student_name);

	max_score = stu[0].English_score;
	flag = 0;
	for (i = 1; i < count; i++)
	{
		if (max_score < stu[i].English_score)
		{
			max_score = stu[i].English_score;
			flag = i;
		}
	}
	printf("英语成绩的最高分是:%d   %s   %s   %s\n", max_score, stu[flag].class_number, stu[flag].student_ID, stu[flag].student_name);

	max_score = stu[0].History_score;
	flag = 0;
	for (i = 1; i < count; i++)
	{
		if (max_score < stu[i].History_score)
		{
			max_score = stu[i].History_score;
			flag = i;
		}
	}
	printf("历史成绩的最高分是:%d   %s   %s   %s\n", max_score, stu[flag].class_number, stu[flag].student_ID, stu[flag].student_name);

	max_score = stu[0].Geography_score;
	flag = 0;
	for (i = 1; i < count; i++)
	{
		if (max_score < stu[i].Geography_score)
		{
			max_score = stu[i].Geography_score;
			flag = i;
		}
	}
	printf("地理成绩的最高分是:%d   %s   %s   %s\n", max_score, stu[flag].class_number, stu[flag].student_ID, stu[flag].student_name);

	max_score = stu[0].Politics_score;
	flag = 0;
	for (i = 1; i < count; i++)
	{
		if (max_score < stu[i].Politics_score)
		{
			max_score = stu[i].Politics_score;
			flag = i;
		}
	}
	printf("政治成绩的最高分是:%d   %s   %s   %s\n", max_score, stu[flag].class_number, stu[flag].student_ID, stu[flag].student_name);

	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值