c语言经典基础编程题

c语言经典基础编程题

😍适合考研复试,期末考试,小白学习...超绝!!!(自用版)👍

一、输出输出

1.1温度输出


答案:

c 复制代码
#include<stdio.h>
int main(){
	double c,F;
	scanf("%lf",&F);
	c=5*(F-32)/9;
	printf("c=%.2lf",c);
	return 0;
}

注意:注意:如果c,F是float类型,为了保证精度,中间运算编译器还是会隐含的转化为double类型进行运算。

1.2排齐数据


答案:

c 复制代码
#include<stdio.h>
int main(){
	char love;
	int year,height;
	int age;
	scanf("%c,%d,%d",&love,&year,&height);
	age=2025-year;
	printf("love:%-8cage:%-8dheight:%-8d",love,age,height);
	return 0;
}

%-md:-代表左对齐,m是最少占用多少列。

1.3进制转换


答案:

c 复制代码
#include<stdio.h>
int main(){
	int x;
	scanf("%d",&x);
	printf("十进制:%d 八进制:%o 十六进制:%x 指数形式:%e",x,x,x,(float)x);
	return 0;
}

注意:仅需制定相应的格式化输出符,编译器会自动完成进制准换。

指数形式是浮点数的表示形式之一,所以要进行强制类型转换成float类型。

二、选择分支

2.1求最大值


答案: 🎈前两项和后两项分别取大者,然后这两个大者再取更大者。

c 复制代码
#include<stdio.h>
int main(){
	int a,b,c,d;
	int temp;
	scanf("%d%d%d%d",&a,&b,&c,&d);
	if(a<b){
		temp=a,a=b,b=temp;
	}
	if(c<d){
		temp=c,c=d,d=temp;
	}
	if(a<c){
		temp=a,a=c,c=temp;
	}
	printf("%d",a);

	return 0;	
}

2.2成绩评定


答案:

c 复制代码
#include<stdio.h>
int main(){
	int grade;
	scanf("%d",&grade);
	if(grade<60){
		printf("E");
	}else{
		switch(grade/10){
			case 6: printf("D"); break;
			case 7: printf("C"); break;
			case 8: printf("B"); break;
			case 9: 
			case 10:printf("A"); break;
		}
	}
	return 0;	
}

2.3分段函数求值


答案:

c 复制代码
#include<stdio.h>
int main(){
	int x,y;
	scanf("%d",&x);
	if (x<1){
		y=x;
	}
	else if(x<=1&&x<10){
		y=2*x-1;
	}
	else{
		y=3*x-11;
	}
	printf("%d",y);
	return 0;
}

2.4 利润计算


答案:

c 复制代码
#include <stdio.h>

int main() {
    int profit;
    double bonus;
    scanf("%d", &profit);

    if (profit <= 100000) {
        bonus = profit * 0.1;
    } else if (profit <= 200000) {
        bonus = 100000 * 0.1 + (profit - 100000) * 0.075;
    } else if (profit <= 400000) {
        bonus = 100000 * 0.1 + 100000 * 0.075 + (profit - 200000) * 0.05;
    } else if (profit <= 600000) {
        bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (profit - 400000) * 0.03;
    } else if (profit <= 1000000) {
        bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (profit - 600000) * 0.015;
    } else {
        bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + 400000 * 0.015 + (profit - 1000000) * 0.01;
    }

    printf("%d", (int)bonus);
    return 0;
}

2.5判断闰年


答案:

c 复制代码
#include <stdio.h>

int main() {
    int year;
	scanf("%d",&year);
	if((year%4==0&&year%100!=0)||(year%400==0)){
		printf("0");  //闰年
	}
	else{
		printf("1");
	}
    return 0;
}

2.6二次方程根

答案:

c 复制代码
#include <stdio.h>
#include <math.h>
int main() {
    double a,b,c,delta;
	scanf("%lf%lf%lf",&a,&b,&c);
	delta=b*b-4*a*c;
	if(delta>1e-6){
		printf("x1=%.3lf x2=%.3lf",
			  ((-b+sqrt(delta))/2/a),
			  ((-b-sqrt(delta))/2/a)
			  );
	}
	else if(fabs(delta)<=1e-6){
		printf("x1=%.3lf x2=%.3lf",
			 (-b/2/a), 
			 (-b/2/a)
			 );
	}
	else{
		printf("x1=%.3lf+%.3lfi x2=%.3lf-%.3lfi",
			 (-b/2/a),(sqrt(-delta)/2/a),
			 (-b/2/a),(sqrt(-delta)/2/a)
			 );

	}
    return 0;
}

在判断判别式与 0 的关系时,由于浮点数在计算机中存储存在精度问题,直接用 == 判断浮点数等于 0 可能不准确,所以有时会引入一个极小值(如 1e-6 )来进行近似判断。

三、循环结构

3.1倒数求和


答案:

c 复制代码
#include <stdio.h>
int main() {
    int n;
	double sum=0.0;//不可以忘记赋初值
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		sum+=1/(double)i; //注意这里的类型转换
	}
	printf("%.4lf",sum);
    return 0;
}

3.2最大数


答案:

c 复制代码
#include <stdio.h>
int main() {
    int x,max=0;
	scanf("%d",&x);
	while(x!=0){
		if(x>max){
			max=x;
		}
		scanf("%d",&x);
	}
	printf("%d",max);
	return 0;
	
}

tips:我们键盘输入到控制台中的数据,并不会直接输出,一开始都放到键盘的缓冲区中,按回车后才会发送出去!🚗

3.3判断素数


答案:

c 复制代码
#include <stdio.h>
int isPrime(int n) {
    if (n <= 1)
        return 0;  // 不是素数
    for (int i = 2; i * i <= n; i++)
        if (n % i == 0) {
            return 0;  // 不是素数
        }
    return 1;  // 是素数,注意是等到整个循环结束后
}

int main() {
    int n;
    scanf("%d", &n);
    if (isPrime(n)) {
        printf("yes\n");
    } else {
        printf("no\n");
    }
    return 0;
}

3.4判断完全数


答案:

c 复制代码
#include <stdio.h>
int main() {
    int n,sum=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		sum=0;  //注意这个归0位置
		for(int j=1;j<=i/2;j++){ //注意边界=i/2 这个因子
			if(i%j==0){
			sum+=j;
			}	
		}
		if(sum==i){  //注意放在内层for循环的外部,不然这会使得在还没有遍历完所有因子时就进行判断
			printf("%d ",i);
			}
	}
	return 0;
}

3.5打印菱形🚀🚀🚀



答案:

c 复制代码
#include <stdio.h>
int main() {
    int n;
	scanf("%d",&n);
	for(int i=1;i<=n/2+1;i++){ //打印高度n/2+1的上三角
		for(int j=i;j<=n/2;j++){ //前4行空格数 3 2 1 0
			printf(" ");
		}
		for(int j=1;j<=2*i-1;j++){ //前4行*数 1 3 5 7
			printf("*");
		}
		printf("\n");
	}
	for(int i=1;i<=n/2;i++){ //打印高度n/2的上三角
		for(int j=1;j<=i;j++){ //后3行空格数 1 2 3
			printf(" ");
		}
		for(int j=1;j<=n-2*i;j++){ //后3行*数 5 3 1
			printf("*");
		}
		printf("\n");
	}

	return 0;
}

先打印上三角,再打印下三角,结合具体情况弄清空格和*的个数关系,来处理边界情况。

3.6复读机


答案:

c 复制代码
#include <stdio.h>
int main() {
    char c;
	while((c=getchar())!='#'){
		putchar(c);
	}
	return 0;
}

3.7算对了吗


答案:

c 复制代码
#include <stdio.h>
#include <string.h>

int main(){
    char input[100];
    int correct_answers = 0;
    while (1){
        gets(input);
        if (input[0] == '#')
            break;
        int a, b, c;
        char op;
        //从字符数组中解析字符
        sscanf(input, "%d%c%d=%c", &a, &op, &b, &c);
        if (op == '+'){
            if (c != '?' && c == (a + b))
                correct_answers++;
        }
        else if (op == '-'){
            if (c != '?' && c == (a - b))
                correct_answers++;
        }
    }
    printf("%d", correct_answers);
    return 0;
}

3.8 堵车问题


答案:

c 复制代码
#include <stdio.h>
int main(){
    int n, count = 0;
    scanf("%d", &n);
    for (int small = 0; small <= n; small++)
        for (int middle = 0; middle <= n/2; middle++)
            for (int large = 0; large <= n/3; large++)
                if (small + 2*middle + 3*large == n)
                    count++;
    printf("%d",count);
    return 0;
}
相关推荐
Scott9999HH4 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
码智社5 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
AI云海5 小时前
python 列表、元组、集合和字典
开发语言·python
萧瑟余晖6 小时前
JDK 26 新特性详解
java·开发语言
马优晨6 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
人邮异步社区8 小时前
怎么把C语言学到精通?
c语言·开发语言
心平气和量大福大9 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
ttwuai9 小时前
Cursor 生成 CRUD 后,Go 后台接口别只测 200:JWT、RBAC 和 tenant_id 怎么验
开发语言·后端·golang
এ慕ོ冬℘゜9 小时前
前端基础:什么是时间戳?JS获取时间戳三种方法与实战用途
开发语言·前端·javascript
执明wa9 小时前
LayoutInflater详解: XML是如何变成View的?
android·xml·开发语言·android studio