1. Word Solitaire
RAM has just started learning English words and is interested in sorting words.
If you give RAM a set of words, he can quickly determine whether these words can be arranged in a list so that the first letter of any word in the list is the same as the letter of the previous word.
Can you write a program to help RAM to judge?
Input Description:
The input contains multiple sets of test data.
For each set of test data, the first behavior is a positive integer n, which represents n words.
Then there are n strings, which represent n words.
Guarantee:
2<=N<=200, each word is longer than 1 and less than or equal to 10, and all words are made up of lowercase letters.
Output Description:
Output "Yes" or "No" for each set of data
Input Example:
3
Abc
Cdefg
Ghijkl
4
Abc
Cdef
Fghijk
Xyz
Output Example:
Yes
No
The code is as follows:
#include <iostream> #include <vector> #include <string>using namespace std; int main () { int n; while (CIN >> N) { bool flag=true; Vector<string> a (n); for (int i = 0; i<n; i++) { cin >> a[i]; } for (int i=0;i<n-1;i++) { int len=a[i].length (); String Str1=a[i]; String str2=a[i+1]; if (str1[len-1]! = Str2[0]) { flag=false; break; } } if (flag) cout<< "Yes" <<endl; else cout<< "No" <<endl; } return 0;}
2. Page scheduling algorithm
In the computer, one of the difficulties in the realization of the page-type virtual memory is the design of the page scheduling (permutation) algorithm. One of the implementations is a FIFO algorithm.
FIFO algorithm according to the page into the memory of the time successively select the elimination of the page, first into the memory of the page first eliminated, after entering the memory after the elimination.
Assuming that the cache size is 2 and there are 5 page requests, 2 1 2 3 1, the state of the cache is converted to: (2), (2,1), (1,3), (1,3), 1th, 2, 4 pages with a total of 3 pages.
Now give the cache size n and M page requests, please calculate the number of pages.
Input Description:
The input contains multiple sets of test data.
For each set of test data, the first row of two integers n,m.
Then there are m integers that represent the request page number.
Guarantee:
2<=n<=20,1<=m<=100,1<= page number <=200.
Output Description:
For each set of data, output an integer that represents the number of pages
Input Example:
2 5
2 1 2) 3 1
Output Example:
3
The code is as follows:
3. Process scheduling algorithm
Short job priority (SJF, shortest job first), also known as the "short process first" SPN (shortest process Next), is an improvement to the FCFS algorithm with the goal of reducing the average turnaround time.
The short job priority scheduling algorithm is based on the idea that:
Priority scheduling with short running times;
The process that initiates the request is dispatched if the run time is the same.
Wait time: The interval from which a process initiates a request to the start of execution.
There are now n processes requesting the CPU, each of which is represented by a two-tuple: (P,Q), p represents the time the process initiated the request, and P represents the time it takes to consume the CPU.
Please calculate the average wait time for n processes.
Input Description:
The input contains multiple sets of test data.
For each set of test data, the first behavior is an integer n.
Then there are n rows, two integers per line, representing the above two tuples (p,q).
Guarantee:
2<=n<=2000,1<=p<=300,1<=q<=100.
Output Description:
For each set of data, output a floating-point number that represents the average wait time, keeping 4 digits valid
Input Example:
4
1 4
1 3
1 5
2 1
Output Example:
5.2500
The code is as follows:
Baidu 2017 Summer Internship Program problem