转载至: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;
}