(1)
cpp
#include <stdlib.h>
int test(int *a) {
int* t;
*a = 99;
printf("%p---\n", a);
t = a;
return t;
}
int main()
{
int x, *y;
printf("%p=%d\n", &x,x);
y = test(&x);
printf("%p", y);
return(0);
}
(2)
cpp
#include <iostream>
class CA {
public:
CA(){};
CA(int val){};
};
int main()
{
CA a;
CA a1();
CA a2(100);
CA a3 = 100;
return 0;
}
(3)
cpp
#include <iostream>
using namespace std;
int main(){
int x,y;
while(1) {
scanf("%d, %d", &x, &y);
cout << x << " " << y << endl;
}
return 0;
}
只有输入11, 12 时,才会输出11 12
必须有逗号,空格可以多个或者没有,如果输入12 34
则输出
12 xxxx
34 xxxx
int main(){
int x,y;
while(1) {
scanf("%d %d", &x, &y);
cout << x << " " << y << endl;
}
return 0;
}
必须得输入空格,空格可以有多个
(4)
cpp
x*=y+5; 等价于 x = x*(y+5);
(5)派生类向基类传递参数_当c++派生类作为参数传递给基类参数函数-CSDN博客
(6)
cpp
int j;
std::cout << (j=3,j++) << " " << std::endl;
输出3
(7)输入链表,和一个数字n,输入链表中倒数第n个数字,如果链表有环,则输出0,如果链表没环,但是n超过了链表长度,则输出-1,否则输出对应的数字。
输入示例
cpp
1 -> 2 -> 3 -> 4 -> 5
4
输出2
1 -> 2 -> 3 -> 4 -> 5
10
输出-1
1 -> 2 -> 3 -> 4 -> 5 -> 2
5
输出0
cpp
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
int main() {
string str;
while (getline(cin, str)) { // 注意 while 处理多个 case
int n;
cin >> n;
// 接受cin中残留的回车,这个很重要
getchar();
map<int, int> mp;
vector<int> nums;
int len = str.size();
int t = 0;
bool flag = false;
bool hasLoop = false;
for (int i = 0; i < len; i++) {
if (isdigit(str[i])) {
t = t*10 + (str[i]-'0');
flag = true;
} else {
if (flag) {
if (mp[t]) {
// 有环
cout << 0 << endl;
hasLoop = true;
break;
}
nums.push_back(t);
mp[t] = 1;
t = 0;
flag = false;
}
}
}
if (flag) {
if (mp[t]) {
// 有环
cout << 0 << endl;
hasLoop = true;
}
nums.push_back(t);
mp[t] = 1;
}
if (!hasLoop) {
int numSize = nums.size();
if (n > numSize) {
cout << -1 << endl;
} else {
cout << nums[numSize-n] << endl;
}
}
}
}
(8)输入多个数组区间,合并之后,找出缺失的区间,如果没有输出-1
输入:2,6;8,10;1,3;15,18
输入:[6, 8]
[10, 15]
cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
static bool cmp (const vector<int> &a, const vector<int>& b) {
return a[0] < b[0];
}
int main () {
string str;
while (getline(cin, str)) {
int len = str.size();
int t = 0;
vector<vector<int>> nums;
bool hasNum = false;
int start;
for (int i = 0; i < len; i++) {
if (isdigit(str[i])) {
t = t*10 + (str[i]-'0');
hasNum = true;
} else {
if (hasNum) {
if (str[i] == ',') {
start = t;
} else if (str[i] == ';') {
nums.push_back({start, t});
}
t = 0;
hasNum = false;
}
}
}
// 2,6;8,10;1,3;15,18
if (hasNum) {
nums.push_back({start, t});
}
sort(nums.begin(), nums.end(), cmp);
int lastStart = nums[0][0];
int lastEnd = nums[0][1];
vector<vector<int>> newNums;
for (int i = 1; i < nums.size(); i++) {
if (nums[i][0] <= lastEnd) {
lastEnd = max(lastEnd, nums[i][1]);
} else {
newNums.push_back({lastStart, lastEnd});
lastStart = nums[i][0];
lastEnd = nums[i][1];
}
}
newNums.push_back({lastStart, lastEnd});
if (newNums.size() == 1) {
cout << -1 << endl;
} else {
for (int i = 0; i < newNums.size()-1; i++) {
cout << '[' << newNums[i][1] << ',' << ' ' << newNums[i+1][0] << ']' << endl;
}
}
}
return 0;
}