Weekly summary of algorithm practice (10.31-11.6)
Learning objectives:
One week elementary practice of algorithmLearning content:
1. Question: call fee calculation
2. Problem: time conversion
3. Problem: Abbreviated summation
4. Problem: word encryption
5. Problem: monkeys eat peaches
Study time:
1. 7 p.m. - 9 p.m. from Monday to Friday
2. Saturday 9 a.m. - 11 a.m
3. 3 p.m. - 6 p.m. Sunday
Learning output:
1. CSDN technology blog 1
2. 1 plog for learning
Summary of contents:
Question 1: call fee calculation (c + +)
Title Description
The global general user's monthly rental fee is 50 yuan, and the call fee is 0.4 yuan per minute. Input the user's call minutes in the current month, output the user's call fee in the current month, and leave one digit after the decimal point.
Input: call minutes of the current month
Output: use floating-point numbers to output the current month's call charges
Sample input: 80
Sample output: 82.0
#include<iostream> #include<iomanip> using namespace std; int main() { double bills = 0;//Define call charges double calltime = 0;//Monthly call time cin >> calltime; bills = 50 + calltime * 0.4; cout << setiosflags(ios::fixed) << setprecision(1) << bills;//Keep one decimal place after the decimal point }
Summary of knowledge points involved in question 1:
Retention of digits after decimal point
First, the header file "iomanip" should be indicated
(1) setprecision -- keep the number of integer bits plus decimal places
double a=3.1415; cout<<setprecision(3)<<a<<endl; //The output is 3.14
(2) fixed + setprecision ¬ -- keep the number of digits after the decimal point
double a=3.1415; cout<<setiosflags(ios::fixed)<<setprecision(3)<<a<<endl; //The output result is 3.142 -- automatic carry //Or cout < < fixed < < setprecision (3) < < a < < endl
(3) scientific + setprecision - Science reserves the number of digits after the decimal point
double bills = 3.14159; cout << scientific << setprecision(3) << bills<<endl; cout << scientific << setprecision(5) << bills<<endl; //3.142e+00 //3.14159e+00
Question 2: time conversion (c + +)
Title Description
Given a time t in seconds, it is required to represent the time in the format of "< H >: < m >: < s >"< H> Represents time, < m > represents minutes, and < s > represents seconds. They are all integers and have no leading "0". For example, if t=0, the output should be "0:0:0"; If t=3661, output "1:1:1".
Input: the input has only one line, which is an integer t (0 < = T < = 86399).
Output: the output has only one line, which is the time expressed in the format of "< H >: < m >: < s >", excluding quotation marks.
Sample input: 5436
Sample output: 1:30:36
#include<iostream> using namespace std; int main() { int hr, mi, se; int t; cin >> t; hr = t / 3600; mi = (t % 3600) / 60; se = t % 60; cout << hr << ":" << mi << ":" << se << endl; return 0; }
Summary of knowledge points involved in question 2:
%Represents the remainder operator after a/b
eg: a=31 b=10;
a/b=3 a%b=1
Problem 3: contraction sum (c + +)
Title Description
Before the popularization of electronic computers, people often used a rough method to check whether the four operations were correct. For example: 248 * 15 = 3720
Sum the multiplier and the multiplicand bit by bit respectively. If it is a multi digit number, sum it bit by bit until it is 1 digit, and then get
2 + 4 + 8 = 14 ==> 1 + 4 = 5;
1 + 5 = 6
5 * 6
The bitwise sum of the results is 3
The result of 5 * 6 is consistent with 3, indicating that it is very likely to be correct!! (errors cannot be excluded)
Please write a computer program to sum the given string bit by bit:
Input: the input is a string composed of numbers, representing n digits (n < 1000);
Output: the output is a single digit, indicating the result of repeated bit by bit summation.
Sample input: 35379
Sample output: 9
#include<iostream> #include<string> using namespace std; string change(string num) { if (num.length() == 1) { //One bit left, return directly return num; } int sum=0; for (int i = 0; i < num.length(); ++i) { sum += stoi(num.substr(i, 1)); //The string truncated from the first bit is converted to integer type and summed } return (change(to_string(sum))); //Convert numeric values to strings } int main() { //Since the number entered may be large, select a string to receive string num; cin >> num; cout << change(num) << endl; return 0; }
Summary of knowledge points involved in question 3:
1.stoi() function
The character processing function converts the numeric string into int output
2.substr() function (commonly known as character interception function)
Resolution:
Format 1: substr (string, string, int a, int b);
1,a Start position of intercepted string 2,b The length of the string to intercept
Format 2: substr (string, int a);
1,string String to intercept 2,a Can be understood as from the second a Start with characters and intercept all subsequent strings.
3.to_string() function
Convert numeric values to strings
Question 4: word encryption (c + +)
Title Description
To encrypt "China", the encryption law is: replace the original letter with the fourth letter after the original letter. For example, the fourth letter after the letter "A" is "E" and "E" replaces "A". Therefore, "China" should be translated as "Glmre".
Please compile a program to make the values of cl, c2, c3, c4 and c5 respectively 'C', 'h', 'i', 'n' and 'a' by assigning initial values. After operation, c1, c2, c3, c4 and c5 become 'G', 'l','m ',' r 'and' e 'respectively and output.
Input: original text, for example: China. Ends when a space is encountered
Output: encrypted ciphertext
Sample input: China
Sample output: Glmre
#include<iostream> using namespace std; int main() { char a[10]; gets_s(a); for (int i = 0; a[i]!='\0'; ++i) { if (a[i] >= 'a' && a[i] <= 'z') { a[i] = (a[i] + 4 - 'a') % 26 + 'a'; } if (a[i] >= 'A' && a[i] <= 'Z') { a[i] = (a[i] + 4 - 'A') % 26 + 'A'; } } puts(a); return 0; }
Question 5: monkeys eat peaches (c + +)
Title Description
Monkeys eat peaches. On the first day, the monkey picked several peaches and ate half of them immediately. It was not fun, so he ate another one. The next morning, he ate half of the remaining peaches and another one. After that, I ate the remaining half and one every morning. When I wanted to eat again in the morning of the nth day, I saw that there was only one peach left. Ask how many peaches you pick on the first day.
Input: N
Output: total peaches
Sample input: 10
Sample output: 1534
#include<iostream> using namespace std; int main() { int last=1, sum=0, day; cin >> day; while(day>1) { sum = (last + 1) * 2; last = sum; day--; } cout << last; }