"Algorithm notes" learning record 002
First, today SLP
p14~27
Remarks:
- I ++ ++ i and distinction of: i ++ is then used to i + 1; ++ i i + 1 is the first re-use i.
- Infinite number of INF said:
const int INF = (1<<30)-1;
const int INF = 0x3fffffff;
- Left to the right:
Specify A = 60 (0011 1100):
(1) bitwise left shift (<<) all bits leftward operand specified number of digits.
The figure below shows the results 11111111 << 1 (11111111 left one) is. Bit digital representation is moved blue, gray is discarded bits 0 vacancies filled with orange.
(A << 2) result 240, binary 1111 0000(2) the right bit (<<) to move to the right all the bits of the operand specified number of digits.
The figure below shows the results (right one 11111111) 11111111 >> 1. Bit digital representation is moved blue, gray is discarded bits 0 vacancies filled with orange.
A >> 2 result 15, is binary 0000 1111
- It is equivalent to the following two lines of code, but preferably parentheses. (Avoid mistakes unskilled)
n/= m+1 ;
n/=(m+1);
- It does not require additional address operator & array name before. In scanf , except where char array (string) without & entire input, the other variable types need to add & .
- The double scanf is% lf; printf is% f.
- How% output and \:
printf("%%");
printf("\\");
- printf的三种实用输出格式:%md %0md %.mf(m是常数)
- “四舍六入五成双”
“四舍六入五成双”,也即“4舍6入5凑偶”,这里“四”是指≤4 时舍去,"六"是指≥6时进上。"五"指的是根据5后面的数字来定,当5后有数时,舍5入1;当5后无有效数字时,需要分两种情况来讲:5前为奇数,舍5入1;5前为偶数,舍5不进(0是偶数)。
一、具体计算规则:
(1)被修约的数字小于5时,该数字舍去;
(2)被修约的数字大于5时,则进位;
(3)被修约的数字等于5时,要看5前面的数字,若是奇数则进位,若是偶数则将5舍掉,即修约后末尾数字都成为偶数;若5的后面还有不为“0”的任何数,则此时无论5的前面是奇数还是偶数,均应进位。
二、来由:
这一方式的另一个常见名称为“银行家舍入”,是IEEE754标准的推荐舍入标准。这一方式跟通常的四舍五入相比,平均数方面更能保持原有数据的特性。
三、举例:
9.8249=9.82, 9.82671=9.83
9.8350=9.84, 9.8351 =9.84
9.8250=9.82, 9.82501=9.83
- getchar可以识别换行符,输出的时候就会换一行。
- 常用math函数:使用时程序前面要加头文件
#include <math.h>
1.fabs(double x) //绝对值函数
2.floor(double x)和ceil(double x) //向下/向上取整
3.pow(double r,double p) //返回r^p
4.sqrt(double x) //返回算术平方根
5.log(double x) //返回自然对数,其他对数用换底公式
6.sin(double x)、cos(double x)和tan(double x) //参数要求是弧度制pi*45/180
7.asin(double x)、acos(double x)和atan(double x)
8.round(double x) //四舍五入,返回类型是double型,强制转换取整
- 角度制与弧度制
角度制与弧度制
- 角度制(Degree Measure)
把一个圆周平均分成360份,其中的每一份都是1o的角。这种以“度”作为单位来度量角度单位制叫做角度制。下图是我们常见的180o角度尺Protractor。
Radians (Radian Measure)
length of long radius arc, the central angle of the arc is 1 (Radian), indicated by symbol rad.
Is a positive angle in radians positive and negative angle of curvature is a negative number, the number zero angle in radians. Long radius arc of a circle the center of the angle α r is l, then the absolute value of [alpha] is the angle in radians | [alpha]
| = L / R & lt.The angle in radians and converted
common control angle in radians FIG.
- Exact value of pi π = acos (-1.0)
- % F default retention six decimal.
Second, today's code analysis
2.02
#include <stdio.h>
int main(){
char c1,c2,c3;
c1=getchar();
getchar();
c2=getchar();
c3=getchar();
putchar(c1);
putchar(c2);
putchar(c3);
return 0;
}
b where to go?
- declare three variables, output three variables. getchar is connected character ah, a friend, a received, not assignment out, not to suddenly disappear. c1 receiving a; getchar () receives b, thus saving not suddenly disappear; C3 received c; c4 receiving d.
Third, practice 2.2
2.03A
#include <stdio.h>
int main(){
printf("This is my first c program!");
return 0;
}
2.04B
#include <stdio.h>
int main(){
printf("********************\nVery Good!\n********************");
return 0;
}
2.05C
#include <stdio.h>
int main(){
int a=123;
int b=456;
int sum=a+b;
printf("sum=%d",sum);
return 0;
}
2.06D
#include <stdio.h>
int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%d",a+b);
return 0;
}
2k07a
#include <stdio.h>
#include <math.h>
int main(){
double a,b,c,r1,r2;
scanf("%lf%lf%lf",&a,&b,&c);
if(a=0||b*b-4*a*c<=0){
printf("a=0或b^2-4ac不大于0,请重新输入参数!");
}
else{
r1 = ((-b) + sqrt(b*b - 4*a*c))/(2*a);
r2 = ((-b) - sqrt(b*b - 4*a*c))/(2*a);
printf("r1=%.2f\n",r1);
printf("r2=%.2f",r2);
}
return 0;
}
#include <stdio.h>
#include <math.h>
int main(){
double a,b,c,r1,r2;
scanf("%lf%lf%lf",&a,&b,&c);
if(b*b-4*a*c<=0){
printf("a=0或b^2-4ac不大于0,请重新输入参数!");
}
else{
r1 = ((-b) + sqrt(b*b - 4*a*c))/(2*a);
r2 = ((-b) - sqrt(b*b - 4*a*c))/(2*a);
printf("r1=%.2f\n",r1);
printf("r2=%.2f",r2);
}
return 0;
}
He has changed a bit, although they are still trying to find fault.
#include <stdio.h>
#include <math.h>
int main(){
double a,b,c,r1,r2;
scanf("%lf%lf%lf",&a,&b,&c);
if(a!=0||b*b-4*a*c>0){
r1 = ((-b) + sqrt(b*b - 4*a*c))/(2*a);
r2 = ((-b) - sqrt(b*b - 4*a*c))/(2*a);
printf("r1=%.2f\n",r1);
printf("r2=%.2f",r2);
}
else
{
printf("a=0或b^2-4ac不大于0,请重新输入参数!");
}
return 0;
}
2.08F
#include <stdio.h>
#include <math.h>
int main(){
char str[10];
scanf("%s",str);
printf("%s\n",str);
return 0;
}