代码题自用
1.已知摄氏温度与华氏温度的换算公式 C=5/9x(F-32)
编写一个程序求华氏温度为30°F,70°F,100°F,150°F时的摄氏温度。
#include<iostream>
using namespace std;
int main()
{
int w;
double t;
for(int i = 0;i < 4;i++){
scanf("%d", &w);
t = 5*(w-32.0)/9;
printf("%5.2f\n",t);
}
return 0;
}
2.求地球表面积
#include<iostream>
using namespace std;
const double pi = 3.1415926;
int main()
{
float r = 6371.0;
double s;
s = 4/3 * pi * r * r;
printf("%5.2f",s);
return 0;
}
3.已知三角形的三条边长为a,b,c,求三角形的面积公式为: F = [s(s-a)*(s-b)*(s-c)]^1/2
s = (a+b+c)/2
编写一个程序读入a,b,c的值,计算出面积F,并输出三条边及面积的值。
#include<iostream>
#include <cmath>
using namespace std;
int main(){
float a,b,c,s;
double F;
cin >>a >>b >>c;
s = (a + b + c)/2.0;
F = sqrt(s*(s-a)*(s-b)*(s-c));
printf("三条边长分别为 a = %5.2f,b = %5.2f,c = %5.2f\n",a,b,c);
printf("面积为%5.2f",F);
return 0;
}
4.设圆半径r = 1.5,圆柱高h = 3,求圆周长,圆面积,圆球表面积,圆柱体积。编写程序,用scanf输入数据,输出结果,输出要求有文字说明,取小数点后两位数字。
#include<iostream>
using namespace std;
const double pi = 3.14;
int main()
{
double r;
int h;
cin >> r >>h;
printf("圆周长为: %5.2f\n",r * pi *2);
printf("圆面积为:%5.2f\n",pi * r * r);
printf("圆球表面积为: %5.2f\n",4 * pi * r * r);
printf("圆柱体积为: %5.2f\n",pi * r * r *h);
return 0;
}
5.有三个数据a,b,c它们由键盘输入,编写程序将他们从小到大排序。
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cin >> a >> b >> c;
if(a > b&&a > c){
if(b>c) cout << c <<" "<< b <<" " << a <<endl;
else cout << b << " " << c <<" " << a <<endl;
}
if(b > a&&b > c){
if(a>c) cout << c <<" "<< a <<" " << b <<endl;
else cout << a << " " << c <<" " << b <<endl;
}
if(c > b&&c > a){
if(a>b) cout << b <<" "<< a <<" " << c <<endl;
else cout << a << " " << b <<" " << c <<endl;
}
return 0;
}
6.对一批货物征收税金,价格在10000元以上的货物征收5%的税金,在5000元以上,10000元以下的货物征收3%的税金,在1000元以上,5000元以下的货物征收2%的税金,1000元以下的货物免税。编写程序,读入货物的价格,计算并输出税金。
#include<iostream>
using namespace std;
int main()
{
float v;
double f = 0;
cin >> v;
if(v>=10000) f = v * 0.005;
else if(v >= 5000 && v < 10000) f = v* 0.03;
else if(v >= 1000 && v < 5000) f = v * 0.02;
printf("价格为%5.2f的货物,应交税金为%5.2f",v,f);
return 0;
}
7.给出一个不多于五位数的正整数,要求:1.求出它是几位数;2.分别输出每一位数字;3.按逆序输出每一位数字。例如:原数是321,输出为123。
#include<iostream>
# include<cstring>
#include<vector>
using namespace std;
int main()
{
string str;
vector<int> num;
int len;
cin >> str;
len = str.size();
for(int i = 0;i < len;i++) num.push_back(str[i]-'0');
printf("它是%d位数\n",len);
for(int i = 0;i < len;i++) cout << num[i] << " " ;
puts(" ");
for(int j = len-1;j>=0;j--) cout << num[j] << " " ;
return 0;
}
8.编写程序,从0~180°每隔5°输出该角度的正弦和余弦函数值。
#include<iostream>
# include<cmath>
using namespace std;
const double PI = 3.1415926;
int main()
{
double x,y;//x为正弦,y为余弦
int i;
double t,s;//i为角度值,t为弧度制
for(int i = 0;i < 36;i++){
s = 5 * (i+1);
t = s * PI/180;
x = sin(t);
y = cos(t);
printf("当x = %0.2f度时,sin(x) = %5.4f,cos(x) = %5.4f\n",s,x,y);
}
return 0;
}
9.编写程序,将20个数读入计算机,并计算其中正数,负数和零的个数。
#include<iostream>
using namespace std;
int main()
{
int num;
int a = 0,b = 0,c = 0;
while(cin >>num)
{
if(num > 0) a ++;
else if (num == 0) b++;
else c++;
}
printf("大于0的数有%d个\n等于0的数有%d个\n小于0的数有%d个",a,b,c);
return 0;
}
10.打印矩阵
#include<iostream>
using namespace std;
int main()
{
int t;
for(int i = 0;i < 5;i ++){
t = 65 + i;
for(int j = 0;j < 5;j ++){
printf("%c ",t);
if(t % 5 == 4) t = 65;
else t++;
}
printf("\n");
}
return 0;
}
11.编写程序计算n!的值
#include<iostream>
using namespace std;
typedef long long LL;
int main()
{
LL num = 1;
int a;
cin >> a;
while(a != 0){
num *= a;
a--;
}
printf("阶乘为%ld",num);
return 0;
}
12.编写程序,找出1到999之间能被3整除其至少有一位数字是5的所有整数。
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
for(int i = 999;i > 0;i--)
{
if(i % 3 == 0){
a = i / 100;
b = (i - a*100)/10;
c = i-a*100-b*10;
if(a == 5||b == 5||c == 5)
printf("%5d ",i);
}
}
return 0;
}