Introduction to Algorithm Competition Classic (Second Edition)_1 Getting Started

Introduction to Algorithm Competition Classic (Second Edition)_1_1_Arithmetic Expression

Study notes, plus shallow ideas

A. Arithmetic expression

1. Integer values ​​are output with %d, and real numbers are output with %f. The output format can also be modified, for example: %.1f% means to keep one decimal place.

2. Integer/integer=integer, floating-point number/floating-point number=floating-point number. Integer-floating point number = floating point number, where the integer is first "changed" into a floating point number, and then floating point number-floating point number = floating point number.


B. Variables and their inputs

1. Here is a way to get a π value:

const double pi = acos(-1);//反余弦函数,头文件为<amath>或<math.h>,声明为常数,所求的pi精度较高。

That is, the value of the arc cosine function of -1 is π, and I want to know its precision, and then try to output it and find that the longest is 16 digits after the decimal point.

2. Algorithm competitions should only do three things: read in data, calculate results, and print out.


C. Sequence structure program design

1. Three-digit inversion_1

#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
printf("%d%d%d\n",n%10,n/10%10,n/100);
return 0;
}

The author's thinking is particularly rigorous, and also takes into account the special situation of output 025, then how to solve it?

Three-digit inversion_2

#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
m = (n%10)*100 + (n/10%10)*10 + (n/100);
printf("%03d",m);
return 0;
}

%03d is a bit novel, so I did a little bit of Baidu and expanded my knowledge in this area (quoted from Baidu):

%3d--The width can be specified, and the left side of insufficient space can be filled
%-3d--Left-justified
%03d---a constant-width format with zeros on the left, such as the number 12, %03d comes out as: 012

C. Branch structure programming

1. The logical operators in C language are all short-circuit operators. Once the value of the entire expression can be determined, no more operations are performed.

2. The goal of the algorithm competition is to program to get correct results for any input, not just sample data.

D. General practice questions

1. Leap year: There are 29 days in February in a leap year, and only 28 days in February in a normal year

2. A leap year can be divisible by 4. If it is a hundred years (century year), one more condition is needed --- can be divisible by 400

Guess you like

Origin blog.csdn.net/qq_41914687/article/details/88412046