为什么这个会RE呀
#include
using namespace std;
int main()
{
stack<int>stk;
long long x;
cin>>x;
int t=x;
int y,c;
while(t)
{
y=t%2;
stk.push(y);
t=t/2;
}
for(int i=0;;i++)
{
cout<top();
stk.pop();
if(stk.empty())break;
}
t=x;
while(t)
{
c=t%7;
stk.push(c);
t=t/7;
}
cout<for(int i=0;;i++)
{
cout<top();
stk.pop();
if(stk.empty())break;
}
return 0;
}
程序的第二个循环中,程序没有将栈 stk 清空,从而导致在第二个循环开始时,栈中可能会包含上一个循环遗留下来的数据。这可能会导致不可预测的结果,例如访问空栈或栈溢出等运行时错误。解决方法是在第二个循环之前清空栈。可以在第一个循环后添加 while(!stk.empty()) stk.pop(); 这行代码来清空栈。
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long x;
cin >> x;
stack<int> stk2; // 用于存储七进制的余数
do {
stk2.push(x % 7);
x /= 7;
} while (x);
while (!stk2.empty()) {
cout << stk2.top();
stk2.pop();
}
cout << endl;
stack<int> stk1; // 用于存储二进制的余数
do {
stk1.push(x % 2);
x /= 2;
} while (x);
while (!stk1.empty()) {
cout << stk1.top();
stk1.pop();
}
return 0;
}
优化和修改的代码
import re
当正则表达式中含有转义字符时,使用原生字符串。
使用Re库时,最好先将字符串编译成正则表达式对象:regex=re.compile(pattern,flags=0)
再利用regex对象调用Re库的函数。