mooc
remind
1. The scope of the test must be ensured. For example, it is far from enough to verify 11, 12, 13 and 14, but 0, 1, 10, 15, 100 and 1000 can be verified. Check the output format carefully, such as how many Spaces are specified. 3. Do not worry about large input lengths, such as fafakjNFsjkf, which can be string or char[100] 4. Initialization Initialization initialization
Oj part
Part mooc
chap.3
1. Two ping-pong teams are playing a match with three players each. Team A consists of A,B and C, and team B consists of X,Y and Z. The list of players has been drawn. The players were asked for the match list. A says he doesn’t compete with X, C says he doesn’t compete with X and Z. Program to find out what happens between three pairs of players.
My mistake: The setting of output criteria was too crude and poorly considered. The implication of this problem is that the relationship between team A and team B is mapped one by one. The matched group of opponents should not continue to participate in the allocation, but my practice will assign multiple opponents to one person.
string a[3]="ABC",b[3]="XYZ",c[6],d; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(! (i==0&&j==0)||(i==2&&j==1)) { c[2*i]=a[i]; c[2*i+1]=b[j]; }}}Copy the code
Correct Practice (transfer) :
#include <iostream> using namespace std; int main() { char a, b, c, str; for (str = 'x'; str <= 'z'; STR ++) {//x to z if (STR! = 'x' && str ! {//c = x, z = c; } } for (str = 'x'; str <= 'z'; str++) { if (str ! = 'x' && str ! //a is not equal to x, and is not equal to c; } } for (str = 'x'; str <= 'z'; str++) { if (str ! = a && str ! = c) {// a is not the same as a, b = STR; }} cout < < "is a opponent:" < < a < < "\ nb opponent is:" < < < < b \ nc opponent is: "< < c; }Copy the code
2. Monkeys eat peaches. The monkey picked a number of peaches on the first day, immediately ate half, do not solve the greedy, and eat one more; The second day, eat the remaining half of the peach, but also not satisfied, and eat one more; After that, I ate one more than half of what was left on the previous day every day. On the NTH day, WHEN I wanted to eat again, there was only one peach left. How many peaches did you pick on the first day?
#include <iostream> using namespace STD; Int func(int n,int day){if(day==n) return 1; if(day==n) return 1; Else return (func(n,day+1)+1)*2; } int main(){int n; cin>>n; cout<<func(n,1); }Copy the code
4. Enter an integer num of int type, output its digits backwards, and find the sum of its digits.
#include<iostream> #include<cmath> using namespace std; int main () { int n; cin>>n; int sum=0; int bit; for(bit=1; n>0; bit++) { sum+=(n%10); cout<<n%10; n/=10; } cout<<endl; cout<<sum<<endl; cout<<--bit<<endl; return 0; }Copy the code
chap.4
1. Design a function toOcr(int n) to convert an input decimal number into an octal number
#include <iostream> using namespace STD; int toOcr(int n); int main() { int n; cin>>n; toOcr(n); return 0; } int toOcr(int n) { int a; if (n==0) return 0 ; // Distinguish between "== "and "="!! else { a=n%8; n=n/8; toOcr(n); cout<<a; Int fun(int n) {if(n==1)return 1; if(n==1)return 1; else { cout<<f(n)<<endl; return fun(n-1)+1; } } int main() { int n; cin>>n; fun(n); } * /Copy the code
chap.6
1. Enter a string from the keyboard and insert the string ab after the first occurrence of the largest element in the string.
#include<string> using namespace std; int main() { string aa,bb; cin>>aa; int temp=0; for(int i=0; i<aa.size()-1; i++){ if(aa[i+1]>aa[temp])temp=i+1; } aa=aa+"00"; bb=aa; for(int i=temp+1; i<=aa.size(); i++)aa[i+2]=bb[i]; aa[temp+1]='a'; aa[temp+2]='b'; cout<<aa; }Copy the code
Harvest:
1. The string length cannot be expanded by assigning to elements in an undefined space, so it passesaa=aa+"00";
Expand space ahead of time to assign values to elements. Such as:string a,aa="abd"; for(int i=0; i<=temp; i++)a[i]=aa[i];
The result shows that a is empty because string length cannot be expanded by assigning to elements of undefined space
2. ![(%U~K4$L%]NGNZFKZ4~TSU.png
Note that we cannot simply say aa[I +2]==aa[I] for character progression in a string, because previous progression affects subsequent character progression, so I define a new variable bb to prevent previous loops from affecting subsequent loops
3. For string inserts, you can call the function directly, for exampleB = STR. Insert (4, 5, 'x');
Insert string 5 characters x at position 4
2. Enter a string containing numeric and non-numeric characters, for example, a123x456 17935? 098tab: stores consecutive numbers as an integer in array A, counts the number of integers, and prints these numbers.
#include <iostream> #include <string> using namespace std; int main() { string str; int k = 0,flag = 0; int a[50] = { 0 }; // Array must be initialized!! getline(cin,str); for (int i = 0; i <str.length(); i++) { if(str[i] >= '0' && str[i] <= '9') { a[k] = a[k] * 10+ int(str[i] - '0'); // the character *int is automatically converted to int flag = 1; } else if (flag==1) { ++k; flag = 0; } } cout << k+1 << endl; for (int i = 0; i <= k; i++) cout << a[i] << endl; return 0; }Copy the code
1. Getline (cin, inputLine); Cin is the input stream being read, and inputLine is the name of the string variable that receives the input string. Int I =0; int I =0; When defined in the body of for, the scope is limited to for
chap.11/12
1.Topic content:
Define a base class Person with three private data members: name(char *), sex(char *), age(int); A constructor is used to initialize the data member; There is a member function show() that prints information about data members.
Create a public derived class Employee from Person and add two private data members: basicSalary (int) and leaveDays (int); It defines a constructor that initializes member information, and a member function show() that displays data member information.
Input format:
A total of 5 data, respectively representing name, gender, age, basic salary, leave days.
Output format:
As shown in the sample data, there are five lines representing name, age, gender, basic salary, and days of leave.
zhangsan m 30 4000 2
Example output:
name:zhangsan
age:30
sex:m
basicSalary:4000
leavedays:2
/* I can't write code on the Internet. */ #include<iostream> #include<string.h> using namespace STD; class Person { private: char name[20]; // If you run out of space, the remaining space system will use \0 instead of char sex; int age; public: Person(char*n,char s,int a) { strcpy(name,n); // Call <string.h> to assign n to name sex=s; age=a; } void show() { cout<<"name:"<<name<<'\n'<<"age:"<<age<<"\n"<<"sex:"<<sex<<"\n"; }}; class Employee:public Person { private: int basicSalary,leaveDays; public: Employee(char*n,char s,int a,int b,int l):Person(n,s,a) { basicSalary=b; leaveDays=l; } void show() { Person::show(); cout<<"basicSalary:"<<basicSalary<<'\n'<<"leaveDays:"<<leaveDays; }}; int main() { char name[20],sex; int age,basicSalary,leaveDays; cin>>name>>sex>>age>>basicSalary>>leaveDays; Employee e(name,sex,age,basicSalary,leaveDays); e.show(); return 0; }Copy the code
‘\0’ = ‘0’ =’ 0′ = ‘0’ =’ 0′ = ‘0’
Cout <<"-- integer array --"<<endl; Int a [10] = {1, 2, 3, 4}; Cout <<" UNDEFINED space ASCII code is: "<<int(a[9])<<endl; Cout <<" Print an integer array: "; for(int i=0; i<10; i++) {cout<<a[i]<<' '; } cout<<'\n'<<"-- character array --"<<endl; char b[10]={"china"}; Cout <<" UNDEFINED space ASCII code is: "<<int(b[9])<<endl; Cout <<" Print character array: "; for(int i=0; i<10; i++) {cout<<b[i]<<' '; }Copy the code
Print result:
! [D0Y6UY6IC) 919 h} S26M] ~ DA. PNG] (p9-juejin.byteimg.com/tos-cn-i-k3… ?).
Important expression
- The character size can be compared directly without quotation marks
Logical error
int a,b,c,d;
complex m(a,b),n(c,d);
cin>>a>>b>>c>>d;
Copy the code
The variable is defined without an initial value, defaults to 0, and is passed in line 2. Line 3 represents a change to the value, but is not passed
Common error
Character constant too long for its type character constant too long for its type redeclaration of ‘int m’ 3. Lvalue required as left operand of assignment ==