栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > C/C++/C#

【C语言、C++基础编程题】【基础类题集】【平顶山学院ACM算法攻关部】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【C语言、C++基础编程题】【基础类题集】【平顶山学院ACM算法攻关部】

题源:平顶山学院ACM算法攻关部

目录

前言

  • 1000:A+B Problem
  • 1001:编写一个程序,输出指定信息
  • 1002:求三个数最大值
  • 1003:字符串加密
  • 1004:计算圆柱体的面积体积等
  • 1005:C语言程序设计教程(第三版)课后习题4.9
  • 1006:C语言程序设计教程(第三版)课后习题5.4
  • 1007: C语言程序设计教程(第三版)课后习题5.5
  • 1008: 级数求和

有空继续更新,欢迎评论区留言讨论~


前言

        边温习C语言基础边练习题库,把C语言基础打扎实。长期更新,建议收藏~

        本篇是全文合集,有难度的题会再单独发文。欢迎评论区提问交流~


1000:A+B Problem

问题描述:Calculate a+b

输入:Two integer a,b

输出:Output a+b

样例输入:1 2

样例输出:3

提示:由于原题太难所以一个小天使改过了这道题目~ ~OvO~

代码:

#include
int main()
{
    int a = 0, b = 0, sum = 0;

    scanf("%d %d", &a, &b);
    sum = a + b;
    printf("%d", sum);

    return 0;
}

1001:编写一个程序,输出指定信息

问题描述:

请参照本章例题,编写一个程序,输出以下信息:
**************************
         Very    Good!
**************************
数*号可看出,Very前面9空格,Good前面……
*也是输出的一部分,别光打印Very Good!

输入:无需输入

输出:

**************************
         Very    Good!
**************************

样例输出:同上

提示:最后一行没有换行符 C语言程序设计教程(第三版)课后习题1.5

代码:

#include

int main()
{
    printf("**************************n");
    printf("         Very    Good!n");
    printf("**************************");
    
    return 0;
}
#include

int main()
{
    printf("**************************n         Very    Good!n**************************");
   
    return 0;
} 

1002:求三个数最大值

问题描述:

编写一个程序,输入a、b、c三个值,输出其中最大值。

输入:一行数组,分别为a b c

输出:a b c其中最大的数

样例输入:10 20 30

样例输出:30

提示:C语言程序设计教程(第三版)课后习题1.6

代码:

方法一:假定a为最大值,引入第四个变量,分别与b,c进行比较

#include
int main()
{
    int a, b, c;
    int max = 0;
    scanf("%d %d %d", &a, &b, &c);
    max = a;
    if (max < b)
    { 
        max = b;
    }
    else if (max < c)
    {
        max = c;
    }
    printf("%d", max);
}

方法二:省去第四个变量 直接将变量a作为最大值。

#include
int main()
{
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);

    if (a < b) 
    {
        a = b;
    }
    if (a < c) 
    {
        a = c;
    }
    printf("%d", a);

    return 0;
}

方法三:直接比较,if else循环嵌套,谁最大输出谁。

#include 

int main()
{
    int a,b,c;
    scanf("%d %d %d", &a, &b, &c);

    if (a > b)
    {
        if (a > c)
        {
            printf("%d", a);
        }
        else
        {
            printf("%d", c);
        }
    }
    else if (b > c)
        {
            printf("%d", b);
        }
        else
        {
            printf("%d", c);
        }

    return 0;
}

方法四:使用&&避免嵌套 与方法三同理。

#include
int main()
{
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if (a >= b && a >= c) {
        printf("%d" ,a);
    }
    else if (b >= a && b >= c) {
        printf("%d", b);
    }
    else {
        printf("%d", c);
    }

    return 0;
}

方法五:三目运算符(无第四变量)

#include
int main()
{
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    printf("%d", a > b ? (a > c ? a : c) : (b > c ? b : a));
}

方法六:三目运算符(有第四变量)

#include
int main()
{
    int a, b, c;
    int max = 0;
    scanf("%d %d %d", &a, &b, &c);

    max = (a > b) ? a : b;
    max = (max > c) ? max : c;

    printf("%d", max);
}

方法七:三木运算符(a作为第四变量)

#include
int main()
{
    int a, b, c;
    int max = 0;
    scanf("%d %d %d", &a, &b, &c);
    a = (a > b) ? a : b;
    a = (a > c) ? a : c;
    printf("%d", a);
}

1003:字符串加密

问题描述:要将"China"译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母"A"后面第4个字母是"E"."E"代替"A"。因此,"China"应译为"Glmre"。请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。

输入:China

输出:加密后的China

样例输入:China

样例输出:Glmre

提示:只实现China的输入和加密即可 so easy

C语言程序设计教程(第三版)课后习题3.7

代码:

方法一:直接使用char字符的储存性质进行加密。

#include
int main()
{
    char a,b,c,d,e;
    scanf("%c %c %c %c %c", &a, &b, &c, &d, &e);
    printf("%c%c%c%c%c", a + 4, b + 4, c + 4, d + 4, e + 4);
    return 0;
}

方法二: 使用strlen函数求数组长度和数组的性质进行加密

#include
#include
int main()
{
    char str[99];
    scanf("%s", str);
    int count = strlen(str);

    for (int i = 0; i < count; i++) 
    {
        printf("%c", str[i] + 4);
    }

    printf("n");
    return 0;
}

1004:计算圆柱体的面积体积等

问题描述:设圆半径r,圆柱高h 求圆周长C1、圆面积Sa、圆球表面积Sb、圆球体积Va、圆柱体积Vb。输入数据半径和高,输出计算结果,输出时要求文字说明,取小数点后两位数字。请编程序。 PI=3.14

输入:两个浮点数,r和h

输出:圆周长C1、圆面积Sa、圆球表面积Sb、圆球体积Va、圆柱体积Vb。 保留两位小数,每个结果后换行。

样例输入:

2 3

样例输出:

C1=12.56
Sa=12.56
Sb=50.24
Va=33.49
Vb=37.68

提示:提高精度,注意精度

代码:

#include 
#define PI 3.14
int main()
{

    int r, h;        //设圆半径为r   圆柱高为h   两个浮点数  
    scanf("%d %d", &r, &h);
     
    //求圆周长C1  圆面积Sa  圆球表面积Sb  圆球体积Va  圆柱体积Vb  保留两位小数
    float C1 = 2 * PI * r;
    float Sa = PI * r * r;
    float Sb = 4 * PI * r * r;
    float Va = 4.0 / 3 * PI * r * r * r;
    float Vb = PI * r * r * h;
 
    printf("C1=%.2fn", C1);
    printf("Sa=%.2fn", Sa);
    printf("Sb=%.2fn", Sb);
    printf("Va=%.2fn", Va);
    printf("Vb=%.2fn", Vb);
     
    return 0;
}

1005:C语言程序设计教程(第三版)课后习题4.9

问题描述:输入一个华氏温度,要求输出摄氏温度。公式为 c=5(F-32)/9 输出要求有文字说明,取位2小数。

输入:一个华氏温度,浮点数

输出:摄氏温度,浮点两位小数

样例输入:

-40

样例输出:

c=-40.00

提示:零下40度,可以不问是?氏

代码:

#include 
int main()
{
    float c,f;
    scanf("%f",&c);
    f=5*(c-32)/9;
    printf("c=%.2f",f);
    return 0;
}

解析:注意.f即可


1006:C语言程序设计教程(第三版)课后习题5.4

问题描述:有三个整数a b c,由键盘输入,输出其中的最大的数。

输入:一行数组,分别为a b c

输出:a b c其中最大的数

样例输入:10 20 30

样例输出:30

提示:跟1002一模一样,上去翻一翻。


1007: C语言程序设计教程(第三版)课后习题5.5

问题描述:有一个函数
y={ x      x<1
    2x-1   1<=x<10
    3x-11  x>=10

写一段程序,输入x,输出y

输入:x

输出:y

样例输入:14

样例输出:31

提示:使用函数

代码:

#include
int fun(int x)
{
    int y;
    if (x < 1)
        y = x;
    else if (x >= 1 && x < 10)
        y = 2 * x - 1;
    else
        y = 3 * x - 11;
    return y;
}

int main()
{
    int x, y;
    scanf("%d", &x);
    y = fun(x);
    printf("%d", y);
    return 0;
}

解析:调用函数即可,怎么写都行,不写函数也可以。


1008: 级数求和

问题描述:已知:Sn= 1+1/2+1/3+…+1/n。显然对于任意一个整数K,当n足够大的时候,Sn大于K。现给出一个整数K(1<=k<=15),要求计算出一个最小的n;使得Sn>K。

输入:键盘输入 k

输出:屏幕输出 n

样例输入:1

样例输出:2

提示:注意精度问题,应使用double

代码:

方法一:for循环,break跳出

#include
int main()
{
    double Sn=0,i;
    int k,n;
    scanf("%d",&k);
    for(i=1;;i++)
    {
        Sn+=1/i;
    if(Sn>k)
    {
        n=i;
        break;
    }
    }
    printf("%d",n);
}

方法二:while循环,条件跳出

#include 
int main()
{
    int k, n = 1;
    double Sn = 0;
    scanf("%d", &k);

    while(Sn <= k)
    {
        Sn = Sn + 1.0/n;
        n++;
    }
    printf("%dn", n-1);
     
    return 0;
}

解析:注意输出n的大小,是否减一


有空继续更新,欢迎评论区留言讨论~

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1038835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号