小学期C++实践
链表
1、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
struct node{
int val;
struct node *next;
} ;
struct node *head = NULL;
int x;
struct node *reverseList(struct node *head){
struct node *head1 = NULL;
struct node *now = NULL;
struct node* p = head;
vector <int> G;
while(p != NULL){
G.push_back(p->val);
p = p->next;
}
for(int i = G.size() - 1; i >= 0; i--){
struct node *tmp = new node;
tmp->val = G[i];
tmp->next = NULL;
if(head1 == NULL){
head1 = tmp;
now = tmp;
}
else{
now->next = tmp;
now = now->next;
}
}
return head1;
}
int main(){
// freopen("hh.txt", "r", stdin);
struct node *now = head;
while(scanf("%d", &x) != EOF){
struct node *tmp = new node;
tmp->val = x;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
}
struct node *head1 = reverseList(head);
struct node *p = head1;
while(p != NULL){
cout << p->val << " ";
p = p->next;
}
return 0;
}
2、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
struct node{
int val;
struct node *next;
} ;
struct node *head = NULL;
int x;
void insert(struct node * &head, struct node * &now, int val){
struct node *tmp = new node;
tmp->val = val;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
return ;
}
struct node *deleteDuplicates(struct node *head){
struct node *head1 = NULL;
struct node *now = NULL;
struct node* p = head;
int num = head->val;
insert(head1, now, num);
p = p->next;
while(p != NULL){
if(p->val == num){
p = p->next;
continue;
}
num = p->val;
insert(head1, now, num);
p = p->next;
}
return head1;
}
int main(){
// freopen("hh.txt", "r", stdin);
struct node *now = head;
while(cin >> x && x != -1){
struct node *tmp = new node;
tmp->val = x;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
}
struct node *head1 = deleteDuplicates(head);
struct node *p = head1;
while(p != NULL){
cout << p->val << " ";
p = p->next;
}
return 0;
}
3、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
struct node{
int val;
struct node *next;
} ;
struct node *head = NULL;
int x;
void insert(struct node * &head, struct node * &now, int val){
struct node *tmp = new node;
tmp->val = val;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
return ;
}
struct node *deleteDuplicates(struct node *head){
struct node* p = head;
map<int, int> g;
while(p != NULL){
g[p->val]++;
p = p->next;
}
struct node *now = NULL;
p = head;
head = NULL;
while(p != NULL){
if(g[p->val] == 1){
insert(head, now, p->val);
}
p = p->next;
}
return head;
}
int main(){
// freopen("hh.txt", "r", stdin);
struct node *now = head;
while(cin >> x && x != -1){
struct node *tmp = new node;
tmp->val = x;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
}
struct node *head1 = deleteDuplicates(head);
struct node *p = head1;
while(p != NULL){
cout << p->val << " ";
p = p->next;
}
return 0;
}
4、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
struct node{
int val;
struct node *next;
} ;
struct node *head1 = NULL;
struct node *head2 = NULL;
int x;
void insert(struct node * &head, struct node * &now, int val){
struct node *tmp = new node;
tmp->val = val;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
return ;
}
struct node *addTwoNumbers(struct node *head1, struct node *head2){
struct node *head = NULL;
struct node *now = NULL;
struct node *p1 = head1, *p2 = head2;
int last = 0;
while(p1 != NULL && p2 != NULL){
int x = p1->val + p2->val + last;
last = 0;
if(x >= 10){
x -= 10;
last += 1;
}
insert(head, now, x);
p1 = p1->next;
p2 = p2->next;
}
while(p1 != NULL){
int x = p1->val + last;
last = 0;
if(x >= 10){
last = 1;
x -= 10;
}
insert(head, now, x);
p1 = p1->next;
}
while(p2 != NULL){
int x = p2->val + last;
last = 0;
if(x >= 10){
last = 1;
x -= 10;
}
insert(head, now, x);
p2 = p2->next;
}
if(last){
insert(head, now, 1);
}
return head;
}
int main(){
// freopen("hh.txt", "r", stdin);
struct node *now = head1;
while(cin >> x && x != -1){
struct node *tmp = new node;
tmp->val = x;
tmp->next = NULL;
if(head1 == NULL){
head1 = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
}
now = head2;
while(cin >> x && x != -1){
insert(head2, now, x);
}
struct node *head = addTwoNumbers(head1, head2);
struct node *p = head;
while(p != NULL){
cout << p->val << " ";
p = p->next;
}
return 0;
}
5、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
struct node{
int val;
struct node *next;
} ;
struct node *head = NULL;
int x;
void insert(struct node * &head, struct node * &now, int val){
struct node *tmp = new node;
tmp->val = val;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
return ;
}
struct node *oddEvenList(struct node *head){
struct node *head1 = NULL;
struct node *now = NULL;
struct node* p = head;
int cnt = 0;
while(p != NULL){
cnt++;
if(cnt % 2 == 1){
insert(head1, now, p->val);
}
p = p->next;
}
cnt = 0;
p = head;
while(p != NULL){
cnt++;
if(cnt % 2 == 0){
insert(head1, now, p->val);
}
p = p->next;
}
return head1;
}
int main(){
// freopen("hh.txt", "r", stdin);
struct node *now = head;
while(cin >> x && x != -1){
struct node *tmp = new node;
tmp->val = x;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
}
struct node *head1 = oddEvenList(head);
struct node *p = head1;
while(p != NULL){
cout << p->val << " ";
p = p->next;
}
return 0;
}
6、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
struct node{
int val;
struct node *next;
} ;
struct node *head = NULL;
int x;
void insert(struct node * &head, struct node * &now, int val){
struct node *tmp = new node;
tmp->val = val;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
return ;
}
struct node *partition(struct node *head, int k){
struct node *head1 = NULL;
struct node *now = NULL;
struct node* p = head;
while(p != NULL){
if(p->val < k){
insert(head1, now, p->val);
}
p = p->next;
}
p = head;
while (p != NULL){
if(p->val >= k) insert(head1, now, p->val);
p = p->next;
}
return head1;
}
int main(){
// freopen("hh.txt", "r", stdin);
struct node *now = head;
while(cin >> x && x != -1){
struct node *tmp = new node;
tmp->val = x;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
}
cin >> x;
struct node *head1 = partition(head, x);
struct node *p = head1;
while(p != NULL){
cout << p->val << " ";
p = p->next;
}
return 0;
}
7、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
struct node{
int val;
int sum; // 求前缀和,前缀和相等证明中间部分为 0
struct node *next;
struct node *pre;
} ;
struct node *head = NULL;
int x;
void insert(struct node * &head, struct node * &now, int val){
struct node *tmp = new node;
tmp->val = val;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
return ;
}
struct node *DeleteZero(struct node *head){
struct node *head1 = NULL;
struct node *now = NULL;
struct node* p = head;
struct node *q = NULL;
head->sum = head->val;
while(233){
p = head;
while(p != NULL){
if(p->next != NULL){
p->next->sum = p->next->val + p->sum;
}
p = p->next;
}
bool flag = 0;
p = head, q = head->next;
while(p != NULL){
q = p->next;
while(q != NULL){
if(p->sum == q->sum || q->sum == 0){
flag = 1;
break;
}
q = q->next;
}
if(flag) break;
p = p->next;
}
if(!flag) break;
p = p->next;
if(p == head || q->sum == 0) head = q->next;
else p->pre->next = q->next;
}
return head;
}
int main(){
// freopen("hh.txt", "r", stdin);
struct node *now = head;
while(cin >> x && x != -1){
struct node *tmp = new node;
tmp->val = x;
tmp->next = NULL;
tmp->pre = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
tmp->pre = now;
now = tmp;
}
}
struct node *head1 = DeleteZero(head);
struct node *p = head1;
while(p != NULL){
cout << p->val << " ";
p = p->next;
}
return 0;
}
8、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
struct node{
int val;
struct node *next;
} ;
struct node *head1 = NULL;
struct node *head2 = NULL;
int x;
void insert(struct node * &head, struct node * &now, int val){
struct node *tmp = new node;
tmp->val = val;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
return ;
}
struct node *mergeTwoLists(struct node *head1, struct node *head2){
struct node *head = NULL;
struct node *now = NULL;
struct node* p1 = head1, *p2 = head2;
while(p1 != NULL && p2 != NULL){
if(p1->val < p2->val){
insert(head, now, p1->val);
p1 = p1->next;
}
else{
insert(head, now, p2->val);
p2 = p2->next;
}
}
while(p1 != NULL){
insert(head, now, p1->val);
p1 = p1->next;
}
while(p2 != NULL){
insert(head, now, p2->val);
p2 = p2->next;
}
return head;
}
int main(){
// freopen("hh.txt", "r", stdin);
struct node *now = head1;
while(cin >> x && x != -1){
struct node *tmp = new node;
tmp->val = x;
tmp->next = NULL;
if(head1 == NULL){
head1 = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
}
while(cin >> x && x != -1){
struct node *tmp = new node;
tmp->val = x;
tmp->next = NULL;
if(head2 == NULL){
head2 = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
}
struct node *head = mergeTwoLists(head1, head2);
struct node *p = head;
while(p != NULL){
cout << p->val << " ";
p = p->next;
}
return 0;
}
## 9、
```cpp
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
struct node{
int val;
struct node *next;
struct node *pre;
} ;
struct node *head = NULL;
int x;
void insert(struct node * &head, struct node * &now, int val){
struct node *tmp = new node;
tmp->val = val;
tmp->next = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
now = tmp;
}
return ;
}
bool isPalindrome(struct node* head){
struct node *p = head;
struct node *q = NULL;
while(p != NULL){
if(p->next == NULL) q = p;
p = p->next;
}
if(q == head) return 1;
p = head;
while(p != q && p->pre != q){
// printf("val: %d %d\n", p->val, q->val);
if(p->val != q->val) return 0;
p = p->next;
q = q->pre;
}
return 1;
}
int main(){
// freopen("hh.txt", "r", stdin);
struct node *now = head;
while(cin >> x && x != -1){
struct node *tmp = new node;
tmp->val = x;
tmp->next = NULL;
tmp->pre = NULL;
if(head == NULL){
head = tmp;
now = tmp;
}
else{
now->next = tmp;
tmp->pre = now;
now = tmp;
}
}
if(isPalindrome(head)) cout << "True" << endl;
else cout << "False" << endl;
return 0;
}
位运算
1、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
int main(){
// freopen("hh.txt", "r", stdin);
int x = -23;
cout << bitset<64>(x) << endl;
x >>= 1;
cout << bitset<64>(x); // 对于补码,Windows采取高位补1.
cout << x << endl;
return 0;
}
2、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
int main(){
// freopen("hh.txt", "r", stdin);
int a = 9;
int b = -2;
cout << (int)(a & b) << endl;
cout << bitset<65>(a) << endl;
cout << bitset<64> (b) << endl; // 对负数高位补1,转成补码后 & 运算未消去原数字的高位
return 0;
}
3、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
string s;
int a;
string Int32toSixteen(ll a){
if(a < 0){
a = abs(a);
for(int i = 0; i < 32; i++)
a ^= (1ll << i);
a += 1; // 取补码
}
string t = "";
while(a){
int tmp = (a >> 4) << 4;
int x = a ^ tmp;
// int num = 1 | (1 << 1) | (1 << 2) | (1 << 3);
// x ^= num;
if(x < 10) t.push_back(x + '0');
else t.push_back(x + 'A' - 10);
a >>= 4;
}
reverse(t.begin(), t.end());
return t;
}
int main(){
// freopen("hh.txt", "r", stdin);
cin >> a;
s = Int32toSixteen(a);
cout << s;
return 0;
}
4、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
string s;
int a;
string Int32toTwo(ll a){
if(a < 0){
a = abs(a);
for(int i = 0; i < 32; i++)
a ^= (1ll << i);
a += 1; // 取补码
}
string t = "";
while(a){
int tmp = (a >> 1) << 1;
int x = tmp ^ a;
t.push_back(x + '0');
a >>= 1;
}
reverse(t.begin(), t.end());
return t;
}
int main(){
// freopen("hh.txt", "r", stdin);
cin >> a;
s = Int32toTwo(a);
cout << s;
return 0;
}
5、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
string s;
int x = 0;
int main(){
// freopen("hh.txt", "r", stdin);
cin >> s;
if(s[0] == '0'){
for(int i = 0; i < s.length(); i++){
x = (x << 1) + (s[i] - '0');
}
printf("%x\n%d", x, x);
}
else{
for(int i = 1; i < s.length(); i++){
x = (x << 1) + (s[i] - '0');
}
x -= 1;
for(int i = 0; i < 15; i++)
x ^= (1 << i);
x *= -1;
printf("%x\n%d", x, x);
}
return 0;
}
6、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
struct node{
int a:3;
int b:5;
int c:6;
int d:9;
} t;
int n;
string Int32toSixteen(ll a){
if(a < 0){
a = abs(a);
for(int i = 0; i < 32; i++)
a ^= (1ll << i);
a += 1; // 取补码
}
string t = "";
while(a){
int tmp = (a >> 4) << 4;
int x = a ^ tmp;
// int num = 1 | (1 << 1) | (1 << 2) | (1 << 3);
// x ^= num;
if(x < 10) t.push_back(x + '0');
else t.push_back(x + 'A' - 10);
a >>= 4;
}
reverse(t.begin(), t.end());
return t;
}
string Int32toTwo(ll a){
if(a < 0){
a = abs(a);
for(int i = 0; i < 32; i++)
a ^= (1ll << i);
a += 1; // 取补码
}
string t = "";
while(a){
int tmp = (a >> 1) << 1;
int x = tmp ^ a;
t.push_back(x + '0');
a >>= 1;
}
reverse(t.begin(), t.end());
return t;
}
int main(){
// freopen("hh.txt", "r", stdin);
cin >> n;
t.a = n, t.b = n, t.c = n, t.d = n;
string s1 = Int32toSixteen(t.a);
string s2 = Int32toSixteen(t.b);
string s3 = Int32toSixteen(t.c);
string s4 = Int32toSixteen(t.d);
cout << s1 << " " << s2 << " " << s3 << " " << s4 << endl;
s1 = Int32toTwo(t.a);
s2 = Int32toTwo(t.b);
s3 = Int32toTwo(t.c);
s4 = Int32toTwo(t.d);
cout << s1 << " " << s2 << " " << s3 << " " << s4;
return 0;
}
7、
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010
int main(){
// freopen("hh.txt", "r", stdin);
string s;
cin >> s;
double flag = 1;
if(s[0] == '1') flag = -1;
int e = 0;
for(int i = 1; i <= 8; i++){
e = e * 2 + s[i] - '0';
}
e -= 127;
deque <int> G;
G.push_back(1);
for(int i = 9; i < s.length(); i++){
G.push_back(s[i] - '0');
}
string s1, s2;
if(e < 0){
int num = abs(e);
for(int i = 1; i < num; i++)
G.push_front(0);
while(!G.empty()){
s2.push_back(G.front());
G.pop_front();
}
}
else{
for(int i = 0; i <= e; i++){
if(G.empty()){
s1.push_back(0);
continue;
}
s1.push_back(G.front() + '0');
G.pop_front();
}
while(!G.empty()){
s2.push_back(G.front() + '0');
G.pop_front();
}
}
double x1 = 0;
double x2 = 0;
for(int i = 0; i < s1.length(); i++)
x1 = x1 * 2 + s1[i] - '0';
for(int i = 0; i < s2.length(); i++)
x2 += (s2[i] - '0') * (pow(0.5, i + 1));
cout << flag * (x1 + x2) << endl;
return 0;
}
函数递归
1
#include <bits/stdc++.h>
using namespace std;
int n;
int f(int n){
if(n == 1 || n == 2) return 1;
return f(n - 2) + f(n - 1);
}
int main(){
cin >> n;
cout << f(n) << endl;
return 0;
}
2
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define ll long long
void convert(int n){
if(n == 0) return ;
convert(n / 10);
cout << (char)(n % 10 + '0');
return ;
}
int main(){
long n;
cin >> n;
if(n < 0){
putchar('-');
n = abs(n);
}
convert(n);
return 0;
}
3
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define ll long long
char s[6];
void dfs(int now){
if(now == 6) return ;
dfs(now + 1);
printf("%c", s[now]);
return ;
}
int main(){
cin >> s + 1;
dfs(1);
return 0;
}
4
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define ll long long
string s;
bool vis[N];
string t;
set <string> G;
void dfs(int now){
if(now > s.length()){
G.insert(t);
return ;
}
for(int i = 0; i < s.length(); i++){
if(!vis[i]){
vis[i] = 1;
t.push_back(s[i]);
dfs(now + 1);
t.pop_back();
vis[i] = 0;
}
}
return ;
}
int main(){
cin >> s;
dfs(1);
for(auto it : G){
cout << it << endl;
}
return 0;
}
文件操作
1
#include <bits/stdc++.h>
using namespace std;
int main(){
ofstream zbkq;
zbkq.open("zbkq.txt");
if(zbkq == NULL){
printf("Error!");
return 0;
}
string s;
while(getline(cin, s)){
zbkq << s << endl;
}
ifstream zbkq1;
zbkq1.open("zbkq.txt");
if(zbkq1 == NULL){
printf("Error!");
return 0;
}
while(zbkq1 >> s){
cout << s << endl;
}
zbkq.close();
zbkq1.close();
return 0;
}
2
#include <bits/stdc++.h>
using namespace std;
#define N 100010
#define ll long long
int main(){
ofstream f1;
f1.open("1.txt", ios::app);
ifstream f2("2.txt");
if(f1 == NULL || f2 == NULL){
cout << "Error!" << endl;
return 0;
}
string s;
while(getline(f2, s)){
f1 << s << endl;
}
f1.close();
f2.close();
return 0;
}
3
#include <bits/stdc++.h>
using namespace std;
#define N 100010
#define ll long long
int n;
int main(){
ofstream F("data.txt", ios::binary);
srand(time(0));
cin >> n;
for(int i = 1; i <= n; i++){
int x = rand() % 200;
F << x << endl;
}
F << n << endl;
F.close();
return 0;
}
4
#include <bits/stdc++.h>
using namespace std;
#define N 100010
#define ll long long
int main(){
ifstream F("data.txt", ios::binary);
if(F == NULL){
cout << "Error!" << endl;
return 0;
}
int x;
vector <int> G;
while(F >> x){
G.push_back(x);
}
G.pop_back();
sort(G.begin(), G.end());
ofstream F2("sort.txt", ios::binary);
for(auto it : G){
F2 << it << endl;
}
F2.close();
F.close();
return 0;
}
堆栈
1
#include <bits/stdc++.h>
using namespace std;
#define N 100010
#define ll long long
stack <int> stac;
int n;
int main(){
cin >> n;
while(n){
stac.push(n % 2);
n >>= 1;
}
while(!stac.empty()){
cout << stac.top();
stac.pop();
}
return 0;
}
2
#include <bits/stdc++.h>
using namespace std;
#define N 100010
#define ll long long
stack <char> stac;
string s;
int main(){
cin >> s;
// s = "abcba";
for(int i = 0; i < s.length(); i++){
stac.push(s[i]);
}
int p = 0;
bool flag = 1;
while(!stac.empty()){
if(stac.top() != s[p]){
flag = 0;
break;
}
stac.pop();
p++;
}
if(flag) puts("Yes");
else puts("No");
return 0;
}
3
#include <bits/stdc++.h>
using namespace std;
#define N 100010
#define ll long long
stack <char> stac;
int main(){
string s;
cin >> s;
// s = "(){{}}[()]";
bool flag = 1;
for(int i = 0; i < s.length(); i++){
if(s[i] == '(' || s[i] == '{' || s[i] == '[')
stac.push(s[i]);
else{
if(stac.empty()){
flag = 0;
break;
}
else{
if(s[i] == ')' && stac.top() != '(')flag = 0;
if(s[i] == '}' && stac.top() != '{') flag = 0;
if(s[i] == '[' && stac.top() != ']') flag = 0;
stac.pop();
}
}
if(!flag) break;
}
if(!stac.empty()) flag = 0;
if(!flag) puts("No");
else puts("Yes");
return 0;
}
4
#include<bits/stdc++.h>
#define MAXSIZE 100
using namespace std;
int Push(stack <char> &S, char m) {
S.push(m);
return 1;
}
int Pop(stack <char> &S, char &m) {
if(S.empty()) return 0;
m = S.top();
S.pop();
return 1;
}
char GetTop(stack <char> &S) {
if(S.empty()) return 0;
return S.top();
}
double operate(char ch, double x, double y){
double z;
switch (ch)
{
case '+': z = x + y; break;
case '-': z = x - y; break;
case '*': z = x * y; break;
case '/': z = x / y; break;
}
return((char)z);
}
int precede(char p1, char p2){ // 求值
int flag = -2;
switch (p1){
case '+':
if (p2 == '*' || p2 == '/' || p2 == '(') flag = -1;
else flag = 1;
break;
case '-':
if (p2 == '*' || p2 == '/' || p2 == '(') flag = -1;
else flag = 1;
break;
case '*':
if (p2 == '(') flag = -1;
else flag = 1;
break;
case '/':
if (p2 == '(') flag = -1;
else flag = 1;
break;
case '(': if (p2 == ')') flag = 0;
else flag = -1;
break;
case ')':
flag = 1;
break;
case '#':
if (p2 == '#')
flag = 0;
else
flag = -1;
break;
}
return(flag);
}
double EvaluateExpression(char a[]) { // 求后缀表达式的值
stack <char> S1, S2;
double x, y, z;
char x1, x2;
char r, ch;
int I = 0;
Push(S1, '#');
r = a[I];
while (r != '#' || GetTop(S1) != '#'){
if (r <= '9' && r >= '0'){
x = 0;
while (r <= '9' && r >= '0'){
x = x * 10 + r - '0';
r = a[++I];
}
Push(S2, x);
}
else
switch (precede(GetTop(S1), r)){
case -1:
Push(S1, r);
r = a[++I];
break;
case 0:
Pop(S1, ch);
r = a[++I];
break;
case 1:
Pop(S1, ch); Pop(S2, x1); Pop(S2, x2);
Push(S2, operate(ch, x2, x1));
r = a[I];
break;
}
}
return S2.top();
}
int main(){
int length;
int t = 0;
char tmp[1000] = "(1+2)*3/(1+2)#"; // 以 # 号作为结尾标识符
do {
length = strlen(tmp);
for (int i = 0; i < length; i++) {
if (isdigit(tmp[i]) || tmp[i] == '+'
|| tmp[i] == '-' || tmp[i] == '*' || tmp[i] == '/' || tmp[i] == '#')
t = 0;
else if (tmp[length - 1] != '#') {
t = 1; continue;
}
else t = 1; continue;
}
} while (t == 1);
if (t == 0) {
double rst = EvaluateExpression(tmp);
cout << rst << endl;
}
return 0;
}
5
#include<bits/stdc++.h>
using namespace std;
#define N 110
struct node{
int x, y;
} ;
stack <node> stac;
int a[N][N]; // 0 代表障碍,1代表通路
int sx, sy; // 起点
int tx, ty; // 终点
int n, m; // 迷宫长,宽
int fx[5] = {0, 1, -1, 0, 0};
int fy[5] = {0, 0, 0, 1, -1};
bool vis[N][N]; // 不走回头路
bool judge(int x, int y){
if(x < 1 || y < 1 || x > n || y > m || vis[x][y] || a[x][y] == 0) return 0;
return 1;
}
void dfs(int x, int y){
if(x == tx && y == ty){
while(!stac.empty()){
cout << stac.top().x << ", " << stac.top().y << endl;
stac.pop();
}
exit(0);
}
vis[x][y] = 1;
for(int i = 1; i <= 4; i++){
int sx = x + fx[i];
int sy = y + fy[i];
if(judge(sx, sy)){
stac.push((node){sx, sy});
dfs(sx, sy);
stac.pop();
}
}
return ;
}
int main(){
cin >> n >> m;
cin >> sx >> sy >> tx >> ty;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
}
}
stac.push((node){sx, sy});
dfs(sx, sy);
return 0;
}
文章来源: https://www.cnblogs.com/wondering-world/p/17532127.html
All rights reserved ©雪之下,树之旁
__EOF__

本文作者:雪之下,树之旁
本文链接:https://www.cnblogs.com/wondering-world/p/17532127.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/wondering-world/p/17532127.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异