随学笔记:
小计:
<1>: ACM比赛中不能使用#include<conio.h> 中包含的getch(),clrscr()等函数,不能使用getche(),gotoxy()等函数。
<2>: 算法竞赛中如发现题目有异议应向相关人员询问,不应主观臆断。
例如: 例题1-2中所讲三位数翻转末尾0是否输出。
<3>: 尽量使用const关键字申明常数。
例如:const double Pi =acos(-1.0);
<4>: 逻辑运算符:
&&和&
& 无论左边结果是什么,右边还是继续运算;
&&当左边为假,右边不再进行运算。
但是两者的结果是一样的
||和|
| 无论左边结果是什么,右边还是继续运算;
||当左边为真,右边不再进行运算。
但是两者的结果是一样的
无特殊要求情况下,相比之下&&和||更高效。
<5>: 平时练习和写模板时应适当添加注释让别人更容易理解,让自己保持思路清晰。
//内容 OR
/* 内容 */
例题1-1:
<1>:浮点型输入输出时:float输入输出都是%f,double输入时为%lf,输出时为%f。
1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 double t1; 8 scanf("%lf",&t1); 9 printf("%f\n",t1); 10 11 float t2; 12 scanf("%f",&t2); 13 printf("%f\n",t2); 14 15 return 0; 16 }
<2>:圆周率π用表达式 acos(-1.0)表示。
这其中acos为math.h里的函数,double acos(double x);//x∈[-1,1]。//应用函数时记得申明头文件#include<cmath>
1 #include <cstdio> 2 #include <cmath> 3 #include <iostream> 4 using namespace std; 5 const double Pi =acos(-1.0); 6 int main() 7 { 8 9 printf("%f\n",Pi); 10 11 return 0; 12 }
相关习题及代码:
1-4: 正弦和余弦
输入integer n,输入n的正弦和余弦值。
1 #include <cstdio> 2 #include <cmath> 3 #include <iostream> 4 using namespace std; 5 const double Pi=acos(-1.0); 6 7 int main() 8 { 9 int n; 10 while(~scanf("%d",&n)){ 11 double x=n*1.0/180*Pi; 12 printf("%.2f\n%.2f\n",sin(x),cos(x)); 13 } 14 return 0; 15 }
课后问题:
<1>: int 型数值的最大表示范围:
1 #include <cstdio> 2 #include <cmath> 3 #include <iostream> 4 using namespace std; 5 6 int main() 7 { 8 int n1=1,cnt1=1; 9 while(++n1>0) 10 cnt1++; 11 printf("%d\n",cnt1); 12 13 int n2=-1,cnt2=-1; 14 while(--n2<0) 15 cnt2--; 16 printf("%d\n",cnt2); 17 return 0; 18 }
<2>: else总是与他前面离他最近的未配对的if配对。
1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 int a=1,b=2; 8 if(a) 9 if(b) 10 a++; 11 else 12 b++; 13 printf("%d\n%d\n",a,b); 14 15 return 0; 16 }
/*可替换代码为
if(a)
b ? a++:b++;
*/