Recently, in the use of spare time and Coursera, we have attended a special course of Programming and Algorithms in Peking University, a total of seven courses, which is the first course of Introduction to Computing and C Language Foundation.
Programming Question #1: Number of letters
Source: POJ (Coursera Statement: Exercises completed on POJ will not be included in Coursera's final score. )Note: Total time limit: 1000ms memory limit: 65536kB
describe
Find out the number of occurrences of vowel letters a,e,i,o,u in a string.
input
Enter a line of strings (there may be spaces in the string, please use cin.getline(s,counts) method to input a line of strings into the character array s, where counts is the maximum length of s, which can be directly written in 80.) The string length is less than 80 characters.
output
Output a line, output a,e,i,o,u in turn in the number of times in the input string, integers are separated by spaces.
//Tenth weeks //Programming Question #1: Number of letters #include <iostream> using namespace std; int main() { char s[80] = { 0 }; int num_a, num_e, num_i, num_o, num_u; num_a=num_e= num_i= num_o= num_u=0; cin.getline(s, 80); for (int i = 0; i < 80; i++) { if (s[i] == 'a') num_a++; if (s[i] == 'e') num_e++; if (s[i] == 'i') num_i++; if (s[i] == 'o') num_o++; if (s[i] == 'u') num_u++; } cout << num_a << ' ' << num_e << ' '<<num_i << ' '<<num_o << ' '<<num_u << endl; return 0; }
Programming Question #2: Ignoring case comparison strings size
Source: POJ (Coursera Statement: Exercises completed on POJ will not be included in Coursera's final score. )
Note: Total time limit: 1000ms memory limit: 65536kB
describe
Generally, we use strcmp to compare the size of two strings. The method of comparison is to compare two strings one by one from the beginning to the end (according to the size of ASCII code), until different characters or encounter'\ 0'. If all characters are the same, they are considered the same; if there are different characters, the comparison results of the first different character shall prevail. But sometimes we compare the size of strings and want to ignore the size of letters, such as "Hello" and "hello" when ignoring the case of letters. Write a program to compare the case of two strings without letters.
input
The input is two lines, one string per line, two strings in total. (Enter each line of string with cin.getline(s,80) (each string length is less than 80)
output
If the first string is smaller than the second string, output a character "<"
If the first string is larger than the second string, output a character ">"
If two strings are equal, output a character "="
//Programming Question #2: Ignoring case comparison strings size //The meaning of the title is to ignore all capitals, i.e. to compare by identifying them in lowercase or in capitalization. #include <iostream> using namespace std; int main() { char s1[80] = { 0 }; char s2[80] = { 0 }; cin.getline(s1,80); cin.getline(s2, 80); char result = 0; //0 means equal, - 1 means s1 is less than s2, 1 means s1 is greater than S2 for (int i = 0;i<80;i++) { //First change the same capitalization into lowercase if (s1[i] >= 'A' && s1[i] <= 'Z') s1[i] += ('a' - 'A'); if (s2[i] >= 'A' && s2[i] <= 'Z') s2[i] += ('a' - 'A'); if (s1[i] == s2[i]) { result = '='; continue; } else if (s1[i]>s2[i]) { result = '>'; break; } else { result = '<'; break; } } cout << result << endl; return 0; }
Source: POJ (Coursera Statement: Exercises completed on POJ will not be included in Coursera's final score. )
Note: Total time limit: 1000ms memory limit: 65536kB
describe
A simple English sentence ending with'.', separated by spaces between words, without abbreviations or other special forms.
input
A simple English sentence ending with'.'(no longer than 500 in length), separated by spaces between words, without abbreviations or other special forms.
output
The longest word in the sentence. If more than one, output the first
I didn't come up with this question myself. I just came up with the online answer for reference. Take note of it.
//Programming Question #3: Longest Word 2 #include <iostream> using namespace std; int main() { char s[500] = { 0 }; cin.getline(s, 500); int count = 0; //count is used to record the length of words int count_max = 0; //count_max is used to record the length of the longest word int end = 0; int i = 0;//End is used to record the end position of the longest word while (i<500) { if (s[i] != ' '&& s[i] != '.' && s[i] != '\0') { count++; if (count > count_max) { count_max = count; end = i; } i++; } if (s[i] == ' ') { count = 0; i++; } if (s[i] == '.' || s[i] == '\0') { break; } } for (int i = end-count_max+1; i < end+1; i++) { cout << s[i]; } cout << endl; }
Programming Question #4: Matrix Exchange Lines
Source: POJ (Coursera Statement: Exercises completed on POJ will not be included in Coursera's final score. )
Note: Total time limit: 1000ms memory limit: 65536kB
describe
In main function, a 5*5 matrix is generated, the matrix data is input, and the values of N and m are input. Determine whether n, m are in the range of the array, if not, output error; if in the range, swap n rows and m rows, output the new matrix after swapping n, M.
input
The data of the 5*5 matrix and the values of n and m.
output
If not interchangeable, output error
If commutative, a new matrix is output
Tips
The output error format is as follows:
cout<< "error" << endl;
The output matrix format is as follows:
cout<< setw(4)<< num;
Court << endl should be output after one line of output matrix.
setw is the format control operator defined in the iomanip library, and it needs to include this header file in # include < iomanip >.
The following points should be noted: 1. Writing of two-dimensional arrays as function parameters; 2. Formatting output of cout.
//Programming Question #4: Matrix Exchange Lines #include <iostream> using namespace std; #include <iomanip> bool change(int a[5][5], int n, int m) { bool flag = true; if ((n >= 0 && n <= 4) && (m >= 0 && m <= 4)) { int line_change[5] = { 0 }; for (int i = 0; i < 5;i++) line_change[i] = a[n][i]; for (int i = 0; i < 5; i++) a[n][i] = a[m][i]; for (int i = 0; i < 5; i++) a[m][i] = line_change[i]; } else { flag= false; } return flag; } int main() { int matrix[5][5] = { 0 }; int m, n; m = n = 0; for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) { cin >> matrix[i][j]; } cin >> n >> m; bool flag = change(matrix, n, m); if (flag) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cout << setw(4) << matrix[i][j]; } cout << endl; } } else { cout << "error" << endl; } return 0; }