算术运算符与算术表达式的常见的几道题:
1、输入一个三位数,输出其个、十、百位数,用空格隔开。
#include<stdio.h>
int main()
{
int a,b,c,t;
printf("一个三位正整数:");
scanf("%d",&t);
c=t%10;
b=(t/10)%10;
a=t/100;
printf("%d %d %d\n",a,b,c);
return 0;
}
运行结果:
2、从键盘上输入一个浮点数(小数点后有三位数),然后分别输出该数的整数部分和小数部分。
#include<stdio.h>
int main()
{
float x;
int m,n;
printf("输入一个带小数点后三位的小数:");
scanf("%f",&x);
m=x;
n=(int)(x*1000)%1000;
printf("整数部分%d\n小数部分%d",m,n);
return 0;
}
运行结果:
3、小慧过生日,小明(小明真忙)要买鲜花送她。每枝红玫瑰5元,满5支送1枝,满20枝送5枝。小明一共有n(n>10)元钱,最多能买到多少?
#include<stdio.h>
int main()
{
int a,x,y,z;
printf("小明带的钱数:");
scanf("%d",&a);
x=a/5; //原本能买多少枝;
y=x/20*5; //能送多少个5枝;
z=(x-(x/20)*20)/5; //能送多少个1枝;
printf("小明能买多少枝:%d\n",x+y+z);
return 0;
}
运行结果:
4、输入3个双精度实数,分别求出它们的和、平均值、平方和以及平方和的开方,并输出所求出各个值。
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,w,x,y,z;
scanf("%f %f %f",&a,&b,&c);
w=a+b+c;
x=(a+b+c)/3;
y=a*a+b*b+c*c;
z=sqrt(y*2);
printf("和:%f\n",w);
printf("平均值:%f\n",x);
printf("平方和:%f\n",y);
printf("平方和的开方:%f\n",z);
return 0;
}
运行结果:
5、写一个程序把极坐标(r,θ) (θ之单位为度)转换为直角坐标( X,Y)。转换公式是:
x=r.cosθ
y=r.sinθ
#include<stdio.h>
#include<math.h>
#define PI 3.1415926
int main()
{
float r,theta,x,y;
scanf("%f %f",&r,&theta);
x=r*cos(theta/180*PI);
y=r*sin(theta/180*PI);
printf("%f %f",x,y);
return 0;
}
运行结果:
总结:有两个参考了老师的代码,把实际问题写成代码对于现在的我来说还有点吃力,望以后可以熟练运用。