【EduCoder答案】C/C++实训答案

简介

其他各类实训答案的目录见这里
答案获取的方法简介见这里

并不是所有的关卡都有答案,有些只有部分关卡有

在电脑网页界面的右侧,有个文章目录,可以在那里查一下要找的实训标题

C/C++相关实训答案

C&C++基本输入输出 >>>链接

第1关: 重要的事情说三遍

解题代码

// 包含标准输入输出函数库
#include <stdio.h>

// 定义main函数
int main()
{
   
    // 请在下面编写将字符输出三遍的程序代码
    /********** Begin *********/
    char x = getchar();
    putchar(x);
    putchar(x);
    putchar(x);
    putchar('!');
    /********** End **********/
    return 0;
}

第2关: 整数四则运算表达式的输出格式控制

解题代码

//包含标准输入输出函数库
#include <stdio.h>

int main()
{
   
    //声明两个整型变量,用于存储输入的两个整数
    int x,y;
    //请在Begin-End之间添加你的代码,按要求格式输出四则运算式子
    /********** Begin *********/
	scanf("%d%d",&x,&y);
    printf("%5d + %-5d = %10d\n", x, y, x + y);
    printf("%5d - %-5d = %10d\n", x, y, x - y);
    printf("%5d * %-5d = %10d\n", x, y, x * y);
    printf("%5d / %-5d = %10d", x, y, x / y);
    /********** End **********/
    return 0;
}

第3关: 你好,生日

解题代码

// 包含I/O流库iostream
#include <iostream>

// 加载名字空间std
using namespace std; 

int main()
{
   
    // 声明三个变量,分别用来存储年、月、日
    int y, m, d;
    // 请在Begin-End之间添加你的代码,输入你的生日,并按指定格式输出信息。	
    /********** Begin *********/
	cin >> y >> m >> d;
    cout << "Hello! " << m << " " << d << " " << y;
    /********** End **********/
    return 0;
}

第4关: 不同精度的PI

解题代码

#include <iostream>

// 包含流操作算子库
#include <iomanip>
using namespace std;

// 定义常量PI,后面可以直接用PI代替后面的数值
#define PI 3.14159265358979323846

int main()
{
   
    int n;
    // 请在Begin-End之间添加你的代码,输入n,按不同的精度输出 PI。
    /********** Begin *********/
	// 输入n
    cin >> n;
    // 输出PI
    cout << setiosflags(ios::fixed) << setprecision(n) << PI << endl;
    cout << setprecision(n + 1) << PI << endl;
    cout << setprecision(n + 2) << PI << endl;
    cout << setprecision(n + 3) << PI << endl;
    cout << setprecision(n + 4) << PI;
    /********** End **********/
    return 0;
}

C&C++控制结构实训 >>>链接

第1关: 分支结构:是闰年吗

解题代码

// 包含两种I/O库,可以使用任一种输入输出方式
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
   
    int year;
    // 请在此添加代码,判断输入的年份是否位闰年,是则输出"Yes",否则输出"No"
    /********** Begin *********/
	cin >> year;
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        cout << "Yes";
    else
        cout << "No";
    /********** End **********/
    return 0;
}
第3关: 分支结构:重排最大数

解题代码

// 包含两种I/O库,可以使用任一种输入输出方式
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
   
    // n-输入的数,m-重排后的数
    int n, m;
    // 请在此添加代码,输入一个小于1000的正整数,重排出最大的数存入m中
    /********** Begin *********/
	cin >> n;
    int a = n % 10, b, c, t;
    n = n / 10;
    b = n % 10;
    c = n / 10;
    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;
    }
    m = a * 100 + b * 10 + c;
    /********** End **********/
    // 输出重排后的数
    cout << m << endl;
    return 0;
}
第4关: 循环结构:黑洞陷阱

解题代码

// 包含两种I/O库,可以使用任一种输入输出方式
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
   
    int n;
    // 请在此添加代码,输出整数进入黑洞过程
    /********** Begin *********/
	cin >> n;
    int k = 1;
    while(1)
    {
   
        int a = n % 10, b, c, t;
        n = n / 10;
        b = n % 10;
        c = n / 10;
        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;
        }
        t = a * 100 + c - c * 100 - a;
        if(t != 495)
          cout << k << ":" << a * 100 + b * 10 + c <<"-" << a + b * 10 + c * 100 << "=" << t << endl;
        if( t == 495)
          cout << k << ":" << a * 100 + b * 10 + c <<"-" << a + b * 10 + c * 100 << "=" << t << endl;
        k++;
        n = t;
        if(t == 495)
            break;
	}
    /********** End **********/
    return 0;
}
第5关: 循环结构:是素数吗

解题代码
本关任务对应参考代码实现如下:

// 包含两种I/O库,可以使用任一种输入输出方式
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
   
    int n;

    // 请在此添加代码,输入正整数n,如果n是素数则输出“Yes”,否则输出“No”
    /********** Begin *********/
    cin >> n;
    if(n == 1)
    {
   
        cout << "No";
        return 0;
    }
    int flag = 1;
    for(int i = 2; i * i <= n; i++)
        if(n % i == 0)
        {
   
            flag = 0;
            break;
        }
    if(flag)
        cout << "Yes";
    else
        cout << "No";
    /********** End **********/

    return 0;
}
第6关: 循环结构:素数和

解题代码
本关任务对应参考代码实现如下:

// 包含两种I/O库,可以使用任一种输入输出方式
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
   
    int n, k;
    // 请在此添加代码,输入n和k,并输出n以内k个素数以及它们的和
    /********** Begin *********/
    cin >> n >> k;
    int t, m = 0, sum = 0;
    for(t = n; t >= 2; t--)
    {
   
        int flag = 1;
        for(int i = 2; i * i <= t; i++)
            if(t % i == 0)
            {
   
                flag = 0;
                break;
            }
        if(flag)
        {
   
            cout << t << " ";
            m++;
            sum += t;
            if(m == k)
                break;
        }
    }
    cout << sum;
    /********** End **********/

    return 0;
}

C&C++函数实训 >>>链接

第1关: 登月纸桥

解题代码

#include <iostream>
using namespace std;

// foldTimes-计算建纸桥的折叠次数
// 参数:dis-星际距离(千米),thick-纸的厚度(毫米)
// 返回值:建桥需要折叠的次数
int foldTimes(double dis, double thick);

int main()
{
   
    double dis, thick;
    cin >> dis >> thick;
    cout << "需要折叠" << foldTimes(dis,thick) << "次" << endl;
    return 0;
}

int foldTimes(double dis, double thick)
{
   
    // 请在这里补充代码,实现函数foldTimes
    /********** Begin *********/
    thick = thick / 1000.0;      // 调整单位为米
    dis = dis * 1000.0;     // 调整单位为米
    int t = 0;     // 折叠次数
    while(thick < dis)
    {
   
        t++;
        thick *= 2;
    }
    return t;
    /********** End **********/
}
第2关: 几点几分了?

解题代码

#include <iostream>
using namespace std;

void whatTime(int secs, int &h, int &m, int &s)
{
   
    // 请在这里补充代码,设计并实现函数whatTime,使main函数中的函数调用正确
    /********** Begin *********/
	h = secs / 3600;
    secs = secs % 3600;
    m = secs / 60;
    s = secs % 60;
    /********** End **********/
}

int main()
{
   
    int secs;     // secs秒表上的秒数   
    int h, m, s;     // 当前时间:h-小时,m-分,s-秒
    cin >> secs;     // 输入秒表上的秒数
    whatTime(secs,h,m,s);     // 计算当前时间
    cout << h << ":" << m << ":" << s << endl;     // 输出当前时间
    return 0;
}
第3关: 这天星期几?

解题代码

#include <iostream>
using namespace std;

// 函数leapYear
int leapYear(int y)
{
   
    if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
        return 1;
    return 0;
}

// 函数whatDay:计算某年某月某日是星期几
// 参数:year-年,month-月
// 返回值:--7分别表示星期一到星期日
int whatDay(int year, int month)
{
   
    // 请在这里补充代码,实现函数whatDay
    /********** Begin *********/
	// 1年月日是星期一
    int w = 1;
    int i;
    // 1到year-1都是全年
    for(i = 1; i < year; i++)
    {
   
        if(leapYear(i))
            w += 366;
        else
            w += 365;
    }
    switch(month)
    {
   
    case 12:
        w += 30;
    case 11:
        w += 31;
    case 10:
        w += 30;
    case 9:
        w += 31;
    case 8:
        w += 31;
    case 7:
        w += 30;
    case 6:
        w += 31;
    case 5:
        w += 30;
    case 4:
        w += 31;
    case 3:
        if(leapYear(year))
            w += 29;
        else
            w += 28;
    case 2:
        w += 31;
    case 1:     // 1月不加了
        ;
    }
    w = w % 7;     // 得到-6,其中为星期天
    // 调整星期天
    if(w == 0)
        w = 7;
    return w;

    /********** End **********/
}

int main()
{
   
    int y, m, xq;     // 年、月、星期几
    cin >> y >> m;     // 输入年月
    xq = whatDay(y,m);     // 计算星期几
    cout << y << "年" << m << "月1日是星期";     // 输出星期
    if(xq == 7)
        cout << "日" << endl;
    else
        cout << xq << endl;
    return 0;
}
第4关: 打印日历

解题代码

// 包含两种I/O库,可以使用任一种输入输出方式
#include <stdio.h>
#include <iostream>
using namespace std;

// 函数printMonth:按要求的格式打印某年某月的日历
// 参数:year-年,month-月
// 返回值:无
void printMonth(int year, int month);

// leapYear:判断闰年
// 参数:y-年
// 返回值:1-是闰年,0-不是闰年
int leapYear(int y)
{
   
    if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
        return 1;
    return 0;
}

// 函数whatDay:计算某年某月的1号是星期几
// 参数:year-年,month-月
// 返回值:1到7--星期1到星期日
int whatDay(int year, int month)
{
   
    // 1年月日是星期一
    int w = 1;
    int i;

    // 1到year-1都是全年
    for(i = 1; i < year
  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值