Note that the following examples are just a sample, and there are many other ways to implement
Basic Syntax:
input and output:
Normal article-input question:As a freshman, more of you may first contact programming. So the input and output as a way of interaction, naturally is very important. So the first way we learn to input and output is to cin,cout these two functions. First of all, we have the first pose:
int A;
cin>>a;
First define the variables, and then enter. This variable can make double,float,int,char,bool and so on. In our computer problem, if the input tells you how many groups of case to enter, how many input to each box, then the use of this method is naturally no problem, because we can use a loop to solve. As the first practice of C (title number, 2076 the Drunk jailer),
Sample Input
2
5
100
Sample Output
2
10
We can use the following code to solve the input problem.
int n,u;
cin>>n;
for (int i = 0;i < n; i++) {
cin>>u;
}
But in other cases, tell the input to enter the end of the number, such as Chinese exercises C (4144 simple String transformation)
Input Sample
3
ABC
4
aabc
0
Output Sample
def
AAEF
We can use the following code to solve
int n;
while (cin>>n) {
if (n = 0) break
;
...//other code
}
This keeps typing the numbers until the input is 0 and jumps out of the loop.
However, all of the above is determined by the input to determine whether the end, if you encounter the input of EOF (end of file) is finished before the end of input how to do it.
For example first practice a (2015 Keep on Truckin ')
Sample Input:
180 160 170
Sample Output:
CRASH 160
So our solution is as follows:
int a,b,c;
while (cin>>a>>b>>c) {
...//other code
}
The problem solved above is that a variable corresponds to an input, and it is good for a row to be a sentence (character array) and a space.
For example, the first practice D (1153 Word reversal)
Method 1: We use the string array to save the input
string A;
int n;
cin>>n;
GetChar (); Because the first integer input followed by a newline character ' \ n ', this character we want to read in order to read the next line while
(n--)
{
getline (cin,a);
...//other code
}
It's okay to have this. In fact, we have a lot of other ways.
We can use the char array (as if we haven't learned it yet, but we'll learn it in advance and tell you beforehand)
Method 2:
Char a[100];
int n;
cin>>n;
GetChar (); For the reasons above you need to use the Cstdio this header file #include <cstdio> while
(n--) {
gets (a);
...//other code
}
If you want to get the length of the array A, the code looks like
int length = strlen (a)
So, it's good to feel like an array here, but is it really over?
How to embody a higher level, show a show of black technology (haha, also can only with a freshman said it is black technology)
Method 3: Read characters with GetChar ()
Char a[100];
int n;
cin>>n;
GetChar (); Reason ditto while
(n--) {
int length = 0;
while (true) {
a[length] = GetChar (); The GetChar () function is to get a character input
if (a[length] = = ' \ n ') //' \ n ' indicates a newline break
;
length++;
}
A[length] = ' the '; ' I ' represents the terminating character of a character array
//cout<<a<<endl;
...//other code
}
So to get here, feel a little bit of a gradient rise?
In the three kinds of methods, the extra knowledge is that you need to know some coding knowledge, here specifically ASCII, ' the ' and ', ' corresponds to the null character, is also the symbol that indicates the end of the character array, ' \ n ' is a newline character
References: Http://baike.baidu.com/link?url=tSj0V1ALtbxUCo27_IozDqqT2MNrUk7zqxAbIlTrr3yt_ m-eezp24qskmkqfvhqokswewobc2elpglzfxodn4q
If you understand this, then any input will not baffle you.
The next issue is the output:
Normal article-output problem: The output format There are many, we use acm.hdu.edu.cn on a few simple questions as an example, interested can do to do 1.1089 a+b for Input-output Practice (I) Sample Input
1 5 10 20
Sample Output
6 30 It's the simplest, one-line input, line output
2.1096a+b for Input-output Practice (VIII)
Sample Input
3 4 1 2 3 4 5 1 2 3 4 5 3 1-2 3
Sample Output
10 15 6 This output requires a newline between two outputs, so what do we do? ------------------here to tell you a very loaded B.
talk is cheap, show me the Code)
int n;
cin>>n;
int flag = 0; This is a very common tagging method while
(n--) {
if (flag = 1)
cout<<endl;
flag = 1;
...//other code
}
Here we can see a new variable with a flag tag defined, if flag==1, then output line, because at this time, we know that there must be a line of output before entering this cycle, will produce a new output, then we have reason to determine, this time need to output a blank line.
ps:1089-1096 is a simple a+b problem, is the link between input and output control, very recommended to do one.
3.5918 sequence I Don't do this, just to tell you that there are other demands.Sample Input
2 6 3 1 1 2 3 1 2 3 1 2 3 6 3 2 1 3 2 2 3 1 1 2-3
Sample Output
Case #1:2 case #2:1
Usually another format is output case # Data group number: answer or change a position: Sample Input
2 3 2 1 2 3 1 3 2 3 3 2 3 3 3 1 1 2 2
Sample Output
Case #1:3 1 case #2:2 3
No matter which format, the actual requirements can be, but the steps of trouble. The input output ends here.
Loop:For D (1153 Word reversal), we can use a looping pose with the following several
Sample Input
2
5
100
Sample Output
2
10
The first type:
int n;
cin>>n;
for (int i = 0;i < n; i++) {
...//other code} The
second type:
int n;
cin>>n;
while (n--) {
...//other code} The
third type:
int n;
cin>>n;
do{
}while (n--);
In particular:
For example, Chinese exercises C (4144 simple String transformation)
Input Sample
3
ABC
4
aabc
0
Output Sample
def
AAEF
We can still have a fourth pose for writing loops.
int n;
while (cin>>n,n!=0) {
...//other code
}
is not very magical, the judgment condition is written in the while bracket. So all four ways you can try it.
With the above knowledge, do exercise is no problem, the rest is thinking. But is that enough?
No~no~no~ we know that college studies are different from high school, we need to master a basic skill is how to self-study, how to find information, how to learn on the road gradually away ~ if you want to become an ACM great God, how to go beyond the high school elder sister, how to surpass the competition of the same grade students, Can you achieve the pace of one year's copper, two-year silver, three-year gold, or are you satisfied? If you want to become a hacker, computer network, composition principle, cryptography, architecture, database, operating system, Linux instructions ... A mountain of books. If you want to be a researcher, machine learning, data mining, graphic images, neural networks ... (Forgive me here for the little I know) ..... Well, all the above is scaring you. What we're going to talk about here is looking at the data.
Baidu Search:First of all, Baidu is everyone will, then we have to use more Baidu search. There is a saying: when you know more and more, you will feel more and more ignorant. You do a lot of problems, you will find that you know too little, so you have to find the problem on the Internet (
Here I strongly do not suggest to find the answers to the exercises on the Internet, such a simple topic, you must think independently, but you will find that the problem on the toj is not so easy. So I suggest you want to brush the problem with the HDU (acm.hdu.edu.cn), Peking University, POJ (poj.org), CF (http://codeforces.com/gyms/)
If English is a big problem (love ACM, you have to face sooner or later), then back to the second, see the Chinese topic, Fuzhou University Fzu (http://acm.fzu.edu.cn/) sjtu (http://acm.sjtu.edu.cn/OnlineJudge/ Problems) In fact, you have a lot of platforms can go, but note that the choice of a platform to keep the brush on the line, do not often change the platform, because a lot of simple questions, but they do not let you progress, you have to try to challenge yourself. HDU,POJ,CF has a lot of problem-solving reports on the web, and when you don't, just look at what other people do. You can put in some examples (sorry, advertising yourself again). Look at a few different blogs and you'll know more.
or query the use of strlen functions.
Second: C + + manual queryhttp://www.cplusplus.com/reference/(this site is still very slow)
Well, let's see what we can find in the brochure. You're not very familiar with the string class. Enter a string in the search box to search
So we'll get some information on string this header file, pull down and we can see more introductions
There are a lot of methods and operations, but actually we need to use very little. We choose length into view
We can look at more instructions and see more examples.
End of query with strlen function
The second chapter ends here ~~~~~~~
Of course, the best way to grow up is to find someone who is better than you, and your elder sister is a very good choice.
Loneliness is a person's carnival, Carnival is a group of people's loneliness. I wish you in the study of the road of no return, more and more deep.