/*【项目1-分离各位数】
写一个程序,输入x(三位数),输出其个、十、百位数,用空格隔开
样例输入:768
样例输出:8 6 7*/
#include <stdio.h>
int main()
{
int one, ten, hundred, digit;
printf("Please enter the digit: ");
scanf("%d", &digit);
one = digit % 10;
ten = digit / 10 % 10; // * / % 结合方向由左向右
hundred = digit / 100;
printf("%d %d %d\n", one, ten, hundred);
return 0;
}
/*
Please enter the digit: 768
8 6 7
Process returned 0 (0x0) execution time : 11.091 s
Press any key to continue.
*/
</pre><pre code_snippet_id="1626029" snippet_file_name="blog_20160328_1_3464867" name="code" class="cpp">______________________________________________________________________________________________________________________________________________
<pre name="code" class="cpp">/*【项目2-分离整数和小数部分】
编写一个程序,其功能为:从键盘上输入一个浮点数(小数点后有三位数),
然后分别输出该数的整数部分和小数部分。
样例输入:123.456
样例输出:123 456 */
#include <stdio.h>
int main()
{
float number;
int integer, decimal;
printf("Please enter number: ");
scanf("%f", &number);
integer = number; //自动类型转换,取整数部分
decimal = (int)(number * 1000) % 1000;//HELP:写到这里想不出来了?参考了解答. //乘1000后对1000取余,得到3位小数点后数字
printf("%d %d\n", integer, decimal);
return 0;
}
/*
Please enter number: 125.789
125 789
Process returned 0 (0x0) execution time : 6.835 s
Press any key to continue.
*/
//
/* 项目答案:
#include <stdio.h>
int main()
{
float x;
int a, b;
scanf("%f",&x);
a=x; //自动类型转换,取整数部分
b=(int)(x*1000)%1000; //乘1000后对1000取余,得到3位小数点后数字
printf("%d %d\n", a, b);
return 0;
*/
————————————————————————————————————————————————————————————————————————
<pre name="code" class="cpp">/*【项目3-如何买玫瑰?】
小慧过生日,小明(小明真忙)要买鲜花送她。每枝红玫瑰5元,
满5支送1枝,满20枝送5枝。小明一共有n(n>10)元钱,最多能买到多少?
样例输入:135
样例输出:33 */
#include <stdio.h>
#define PRICE 5
int main()
{
int n, sum;
printf("Please enter the money: ");
scanf("%d", &n);
if (n > 10)
sum = n / PRICE + n / 20;
else
sum = n / PRICE + n / 5;
printf("Rose = %d", sum);
return 0;
}
/*
Please enter the money: 135
Rose = 33
Process returned 0 (0x0) execution time : 4.776 s
Press any key to continue.
*/
/*参考解答:
思路:样例中的33枝是怎么来的?5元一支,135元能买27支。
他这样买:先买20支,这时赠5支,再买7支,还能再送1支,
这样就能达到33支了。
int main( )
{
int money, n, z1, z2;
scanf(135"%d", &money);
n=money/5; //实际能买的玫瑰
z1=n/20*5; //买够20就能送5支
z2=(n-(n/20)*20)/5; //不够买20赠5的,享受买5赠1的
printf("%d\n", n+z1+z2);
return 0;
}
*/
---------------------------------------------------------------------------------------------------------------------------
/*【项目4-玩数字】
输入3个双精度实数,分别求出它们的和、平均值、
平方和以及平方和的开方,并输出所求出各个值。*/
#include <stdio.h>
#include <math.h>
int main()
{
double x, y, z;
double sum=0, ave=0, sum_squares=0, root=0; //初始化并赋值
printf("Please enter x, y, z: ");
scanf("%lf %lf %lf", &x, &y, &z);
sum = x + y + z;
ave = (x + y + z) / 3;
sum_squares = pow(x,2)+pow(y,2)+pow(z,2);//平方pow(a, 2); //error:刚刚开始写成pow((x+y+z), 2);
root =sqrt(sum_squares);//开方sqrt(a); //error:这也是写成sqrt(x+y+z);
printf("sum = %.2lf, average = %.2lf, sum_square = %.2lf, root = %.2lf\n",
sum, ave, sum_squares, root);
return 0;
}
/*
Please enter x, y, z: 10 20 30
sum = 60.00, average = 20.00, sum_square = 1400.00, root = 37.42
Process returned 0 (0x0) execution time : 2.865 s
Press any key to continue.
*/
/
/*参考解答:
#include <stdio.h>
#include <math.h> //sqrt需要math.h
int main( )
{
float x, y, z;
float sum, avg, sq_sum, root;
scanf("%f %f %f", &x, &y, &z);
sum = x + y + z;
avg = sum / 3;
sq_sum = x*x + y*y + z*z;
root = sqrt(sq_sum);
printf("和:%f\n", sum);
printf("平均:%f\n", avg);
printf("平方和:%f\n", sq_sum);
printf("平方和开方:%f\n", root);
return 0;
}*/
笔记:
平方和公式:
1::
(各数的平方之和)
2:a²+b²=(a+b)²-2ab =(a-b)²+2ab(完全平方公式的变形)
----------------------------------------------------------------------------------
<span style="font-size: 14px;">/*【项目5-坐标转换】
写一个程序把极坐标(r,θ) (θ之单位为度)转换为直角坐
标( X,Y)。转换公式是:
x=r.cosθ
y=r.sinθ
样例输入1:10 45(代表r=10 θ=45°)
样例输出1:7.071068 7.071068
样例输入2:20 90 (代表r=20 θ=90°)
样例输出2:0 20(可以接近似的结果) */
#include <stdio.h>
#include <math.h>
#define PI 3.14
int main()
{
float r, r1, x, y;
printf("Please enter r, r1: ");
scanf("%f %f", &r, &r1);
x = r * cos(r1 / 180 * PI);//公式不会?.参考了解答。
y = r * sin(r1 / 180 * PI);
printf("%.0f %.0f\n", x, y);
return 0;
}
/*
Please enter r, r1: 20 90
0 20
Process returned 0 (0x0) execution time : 3.327 s
Press any key to continue.
*/
//
/*
[参考解答]
[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
#include <stdio.h>
#include <math.h> //三角函数需要math.h
#define PI 3.1415926 //用符号常量处理
int main( )
{
float r, theta, x, y;
scanf("%f %f", &r, &theta);
x = r * cos(theta/180*PI);//注意cos需要弧度作为参数
y = r * sin(theta/180*PI);
printf("%f %f", x, y);
return 0;
}
*/
/* </span><span style="font-size:24px;">笔记:</span><span style="font-size: 14px;">
三角函数公式http://baike.so.com/doc/5351306-5586764.html
正弦:sine(简写sin)[sain]
余弦:cosine(简写cos)[kəusain]
正切:tangent(简写tan)['tændʒənt]
余切:cotangent(简写cot)['kəu'tændʒənt]
正割:secant(简写sec)['si:kənt]
余割:cosecant(简写csc)['kau'si:kənt]
正矢:versine(简写versin)['və:sain]
余矢:versed cosine(简写vercos)['və:sə:d][kəusain]
*/
</span>