C++的顺序结构练习题记录【代码】

转载至:https://zhuanlan.zhihu.com/p/609399111

一、顺序结构

一、和、积、商和余数

#include <iostream>
using namespace std;
int main ()
{
   int a,b;
    cin>>a>>b;
    //输入你的代码
    cout <<a<<"+"<<b<<"="<<a+b<<endl;
    cout <<a<<"*"<<b<<"="<<a*b<<endl;
    cout <<a<<"/"<<b<<"="<<a/b<<endl;
    cout <<a<<"%"<<b<<"="<<a%b;
    return 0;
}

二、4个整型数的平均值

//#include <iostream>
#include <iomanip>
using  namespace std;
int main()
{
    //输入你的代码
    int x1,x2,x3,x4;
    double result,sum;
    cin >> x1 >> x2 >> x3 >> x4;
    sum = x1 + x2 + x3 + x4;
    result = sum/4;
    cout << fixed << setprecision(2) << result <<endl;      //这里对应 <iomanip> 库
	return 0;
} 

三、数字分解

#include <iostream>
using  namespace std;
int main()
{
    //输入你的代码
    int num,a,b,c;
    cin >> num;
    a = num/100;         //百位数
    b = num/10%10;       //十位数
    c = num%10;          //个位数
    cout << a << " " << b << " " << c <<endl;
    return 0;
}

四、数字加密

#include <iostream>
using  namespace std;
int main()
{
    //输入你的代码
    int num;
    int x1,x2,x3,x4;
    cin >> num;
    x1 = num /1000 + 13;       //处理千位数
    x2 = num /100 %10 + 13;    //处理百位数
    x3 = num %100 /10 + 13;    //处理十位数
    x4 = num %10 + 13;         //处理个位数

    char y1,y2,y3,y4;
    y1 = 'A' + x1 -1;
    y2 = 'A' + x2 -1;
    y3 = 'A' + x3 -1;
    y4 = 'A' + x4 -1;

    cout << y1 << y2 << y3 << y4 <<endl;
    
    return 0;
}

五、两点之间的距离

#include <iostream>
#include <math.h>
#include <iomanip>
using  namespace std;
int main()
{
    //输入你的代码
    int x1,y1,x2,y2;
    cin >> x1 >> y1 >> x2 >>y2;
    double distence,m,n;
    m = x1-x2;
    n = y1-y2;
    distence = sqrt( m*m + n*n );
    cout << fixed <<setprecision(2) << distence << endl;
    return 0;
}

六、时间

#include <iostream>
using  namespace std;
int main()
{
    //输入你的代码
    int hour,minute,second,time;
    cin >> time;
    second = time %60;
    minute = time /60 %60;
    hour = time /60 /60;
    cout << hour << "小时" << minute << "分" << second << "秒" << endl;
    return 0;
}

七、球体表面积和体积

#include <iostream>
#include <iomanip>
using  namespace std;
int main()
{
    //输入你的代码
    int R;
    cin >> R;
    double pi=3.14;
    double V,S;
    S = 4 * pi * R * R;
    V = 4 * pi * R * R * R /3;
    cout << fixed << setprecision(1) << "半径为" << R << "的球体积为" << V << endl;
    cout << fixed << setprecision(1) << "半径为" << R << "的球体表面积为" << S << endl;
    return 0;
}

八、数字加密及解密

#include <iostream>
using  namespace std;
int main()
{
    //输入你的代码
    int n,m;
    cin >> n >> m;
    int n_code,m_decode;

    //对n加密
    int x1,x2,x3,x4;
    x1 = ( n /1000 + 7 ) %10;
    x2 = ( n /100 %10 + 7 ) %10;
    x3 = ( n %100 /10 + 7 ) %10;
    x4 = ( n %10 + 7 ) %10;

    n_code = x3 * 1000 + x4 * 100 + x1 * 10 + x2;
    
    //对m解密
    x1 = ( m /1000 + 10 - 7 ) %10;
    x2 = ( m /100 %10 + 10 - 7 ) %10;
    x3 = ( m %100 /10 +10 - 7 ) %10;
    x4 = ( m %10 +10 - 7 ) %10;

    m_decode = x3 * 1000 + x4 * 100 + x1 * 10 + x2;

    cout << n_code << " " << m_decode << endl;

    return 0;
}

九、电费(考虑整型数溢出)

#include <iostream>
using  namespace std;
int main()
{
    //输入你的代码
   long long int num,w;
   cin >> num;
   w = num*6;                     //将浮点数转换为整型数,此时个位数为角,十位数及其更高位数为元
   long long int yuan,jiao;
   yuan = w/10;
   jiao = w%10;
   cout << yuan << " " << jiao;
   return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值