7-1 进制转换
输入十进制整数N和待转换的进制x(2、8、16),分别代表十进制N转换成二进制、八进制和十六进制,输出对应的结果。十六进制中A~F用大写字母表示。
输入两个整数N(十进制整数N)和x(x进制),中间用空格隔开。
输出对应的结果。
在这里给出一组输入。例如:
123 2
在这里给出相应的输出。例如:
1111011
在这里给出一组输入。例如:
123 16
在这里给出相应的输出。例如:
7B
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
- #include
- using namespace std;
-
- typedef long long LL;
- const int eps = 1e-8;// 一个极小值
- const int N = 1e4 + 100;
- const int M = 3e3 + 100;
- const int INF = 0x3f3f3f3f;
- const LL LINF = 0x3f3f3f3f3f3f3f3f;
- #define PI acos(-1.0)
- stack<char> s;// stl的容器
- int main()
- {
- int n, r;
- cin >> n >> r;
- if(n == 0) cout << "0" << endl;
- if(r == 16){
- while(n){
- int x = n % r;
- if(x < 10) s.push(x + '0');// 将int型转换为char型,使其能够存入char类型的栈中
- else s.push(x - 10 + 'A');
- n /= r;
- }
- }
- else{
- while(n){
- int x = n % r;
- s.push(x + '0');// 栈的特点:先进后出
- n /= r;
- }
- }
- while(!s.empty()){
- cout << s.top();// 栈顶是最后进栈的数
- s.pop();
- }
- return 0;
- }
**为了用stl容器偷懒我真的努力了23333
7-2 中缀表达式转换为后缀表达式
所谓中缀表达式,指的是运算符处于操作数的中间(例:3 * ( 4 + 2 )),中缀表达式是人们常用的算术表示方法,但中缀表达式不容易被计算机解析,因为既要考虑运算符的优先级,还要考虑括号的处理。但中缀表达式仍被许多程序语言使用,因为它符合人们的普遍用法。后缀表达式,指的是不包含括号,运算符放在两个操作数的后面,所有的计算按运算符出现的顺序,严格从左向右进行(不再考虑运算符的优先规则,也不需要考虑括号)。
给出一个中缀表达式,请将其转换为后缀表达式并输出。
只有一行,是一个长度不超过1000的字符串,表示一个中缀表达式。表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。运算符、操作数之间用一个空格分隔,数据保证输入的操作数中不会出现负数,保证除数不会为0。
输出对应的后缀表达式。运算符、操作数之间用一个空格分隔,但行尾无多余空格。
3 * ( 4 + 2 )
3 4 2 + *
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
- #include
- using namespace std;
-
- int main()
- {
- string a;// 在c++里不能用gets了
- char s[20];
- getline(cin, a);
- int len = a.size(), f = 0, cnt = -1;
- for(int i = 0; i < len; i++){
- if(a[i] >= '0' && a[i] <= '9'){
- if(f) printf(" ");
- f = 1;
- while(a[i] >= '0' && a[i] <= '9'){
- printf("%c", a[i++]);// 遇到数字就输出
- }
- i--;// 返回去输出空格
- }
- else{
- if(a[i] == '(') s[++cnt] = a[i];// 遇左括号就“入栈”
- else if(a[i] == '*' || a[i] == '/'){
- while(s[cnt] == '*' || s[cnt] == '/' && cnt != -1){
- printf(" %c", s[cnt--]);
- }
- s[++cnt] = a[i];
- }
- else if(a[i] == ')'){
- while(s[cnt] != '('){
- printf(" %c", s[cnt--]);
- }// 不是左括号(可能是加or减)就输出
- cnt--;// 无论是否有输出都得出个栈~最后左括号会出栈
- }
- else if(a[i] == '+' || a[i] == '-'){
- while(s[cnt] != '(' && cnt != -1){
- printf(" %c", s[cnt--]);
- }
- s[++cnt] = a[i];
- }
- }
- }
- while(cnt != -1){
- printf(" %c", s[cnt--]);
- }
- return 0;
- }
**这个 只限一位数字。。
7-3 后缀式求值
我们人类习惯于书写“中缀式”,如 3 + 5 * 2 ,其值为13。 (p.s. 为什么人类习惯中缀式呢?是因为中缀式比后缀式好用么?)
而计算机更加习惯“后缀式”(也叫“逆波兰式”,Reverse Polish Notation)。上述中缀式对应的后缀式是: 3 5 2 * +
现在,请对输入的后缀式进行求值。
在一行中输入一个后缀式,运算数和运算符之间用空格分隔,运算数长度不超过6位,运算符仅有+ - * / 四种。
在一行中输出后缀式的值,保留一位小数。
3 5.4 2.2 * +
14.9
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
- #include
- using namespace std;
- int main()
- {
- char str[10];
- double st[100];
- int top = 0;
- while(~scanf("%s", str)){// 这是一个字符一个字符输入的~
- if(str[1] == '\0' && (str[0] == '+' || str[0] == '-' || str[0] == '*' || str[0] == '/')){
- double num1 = st[--top];
- double num2 = st[--top];
- double res;
- switch(str[0]){
- case '+':
- res = num2 + num1;
- st[top++] = res;
- break;
- case '-':
- res = num2 - num1;
- st[top++] = res;
- break;
- case '*':
- res = num2 * num1;
- st[top++] = res;
- break;
- case '/':
- res = num2 / num1;
- st[top++] = res;
- break;
- }
- }
- else{
- double num;
- sscanf(str, "%lf", &num);// 从str中取出double类型的元素
- st[top++] = num;
- }
- }
- printf("%.1lf", st[0]);
- return 0;
- }
**sscanf有好多用法
7-4 括号匹配
给定一串字符,不超过100个字符,可能包括括号、数字、字母、标点符号、空格,编程检查这一串字符中的( ) ,[ ],{ }是否匹配。
输入在一行中给出一行字符串,不超过100个字符,可能包括括号、数字、字母、标点符号、空格。
如果括号配对,输出yes,否则输出no。
sin(10+20)
yes
{[}]
no
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
- #include
- using namespace std;
- const int N = 1e3 + 100;
- int main()
- {
- string s;
- int top = 0;
- char str[N];
- getline(cin, s);
- int i = 0;
- int len = s.size();
- for(i = 0; i < len; i++){
- if(s[i] == '(' || s[i] == '[' || s[i] == '{'){
- str[++top] = s[i];
- }
- if(s[i] == ')' || s[i] == ']' || s[i] == '}'){
- if((str[top] == '(' && s[i] == ')') || (str[top] == '[' && s[i] == ']') || (str[top] == '{' && s[i] == '}')){
- top--;
- }
- else break;
- }
- }
- if(top == 0 && i == len) cout << "yes" << endl;
- else cout << "no" << endl;
- return 0;
- }
7-5 出栈序列的合法性
给定一个最大容量为 M 的堆栈,将 N 个数字按 1, 2, 3, ..., N 的顺序入栈,允许按任何顺序出栈,则哪些数字序列是不可能得到的?例如给定 M=5、N=7,则我们有可能得到{ 1, 2, 3, 4, 5, 6, 7 },但不可能得到{ 3, 2, 1, 7, 5, 6, 4 }。
输入第一行给出 3 个不超过 1000 的正整数:M(堆栈最大容量)、N(入栈元素个数)、K(待检查的出栈序列个数)。最后 K 行,每行给出 N 个数字的出栈序列。所有同行数字以空格间隔。
对每一行出栈序列,如果其的确是有可能得到的合法序列,就在一行中输出YES,否则输出NO。
- 5 7 5
- 1 2 3 4 5 6 7
- 3 2 1 7 5 6 4
- 7 6 5 4 3 2 1
- 5 6 4 3 7 2 1
- 1 7 6 5 4 3 2
- YES
- NO
- NO
- YES
- NO
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
- #include
- using namespace std;
- const int N = 1e3 + 100;
-
- int main()
- {
- int m, n, k;
- int stackk[N];
- int top = 0;
- int inx1 = 1, inx2 = 1;
- int b[N];
- cin >> m >> n >> k;
- while(k--){
- int f = 1;
- inx1 = 1, inx2 = 1;
- top = 0;
- for(int i = 1; i <= n; i++){
- cin >> b[i];
- }
- while(1){// 这个的存在大概就是为了让后面的break起作用
- if(inx1 == b[inx2]){// 元素进栈后立马出栈的情况
- inx1++;
- inx2++;
- }
- else if(top != 0 && stackk[top - 1] == b[inx2]){
- top--;
- inx2++;
- }// 判断如果栈中有元素,然后栈顶元素也与此时的出栈序列元素相同,那么就继续出栈来判断
- else{
- if(inx1 > n) break;
- stackk[top] = inx1;
- top++;
- inx1++;
- if(top >= m){
- f = 0;
- break;
- }
- }
- }
- if(!f || top != 0) cout << "NO" << endl;
- else cout << "YES" << endl;
- }
- return 0;
- }
7-6 行编辑器
一个简单的行编辑程序的功能是:接受用户从终端输入的程序或数据,并存入用户的数据区。
由于用户在终端上进行输入时,不能保证不出差错,因此,若在编辑程序中,“每接受一个字符即存入用户数据区”的做法显然不是最恰当的。较好的做法是,设立一个输入缓冲区,用以接受用户输入的一行字符,然后逐行存入用户数据区。允许用户输入出差错,并在发现有误时可以及时更正。例如,当用户发现刚刚键入的一个字符是错的时,可补进一个退格符"#",以表示前一个字符无效;
如果发现当前键入的行内差错较多或难以补救,则可以键入一个退行符"@",以表示当前行中的字符均无效。
如果已经在行首继续输入'#'符号无效。
输入一个多行的字符序列。但行字符总数(包含退格符和退行符)不大于250。
按照上述说明得到的输出。
在这里给出一组输入。例如:
whli##ilr#e(s#*s)
在这里给出相应的输出。例如:
while(*s)
在这里给出一组输入。例如:
outcha@putchar(*s=#++);
在这里给出相应的输出。例如:
putchar(*s++);
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
- #include
- using namespace std;
- const int N = 1e2 + 100;
- int main()
- {
- int cnt = 0;
- char str[N];
- string s;
- while(getline(cin, s)){
- int i = 0;
- for(i = 0; i < s.size(); i++){
- if(s[i] == '#' && cnt == 0) continue;
- else if(s[i] == '#') cnt--;
- else if(s[i] == '@') cnt = 0;
- else str[cnt++] = s[i];
- }
- for(int j = 0; j < cnt; j++){
- cout << str[j];
- }
- }
- return 0;
- }
7-7 银行业务队列简单模拟
设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出。
输入为一行正整数,其中第1个数字N(≤1000)为顾客总数,后面跟着N位顾客的编号。编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口。数字间以空格分隔。
按业务处理完成的顺序输出顾客的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。
8 2 1 3 9 4 11 13 15
1 3 2 9 11 4 13 15
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
- #include
- using namespace std;
-
- queue<int>a, b;
- int main()
- {
- int n, x;
- cin >> n;
- for(int i = 0; i < n; i++){
- cin >> x;
- if(x % 2 != 0) a.push(x);
- else b.push(x);
- }
- int cnt = 0;
- while(!a.empty() || !b.empty()){
- if(!a.empty()){
- if(cnt > 0) cout << " ";
- cout << a.front();
- a.pop();
- cnt++;
- }
- if(!a.empty()){
- if(cnt > 0) cout << " ";
- cout << a.front();
- a.pop();
- cnt++;
- }
- // 因为A = 2B
- if(!b.empty()){
- if(cnt > 0) cout << " ";
- cout << b.front();
- b.pop();
- cnt++;
- }
- }
- return 0;
- }
7-8 堆栈模拟队列
设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q。
所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操作函数:
int IsFull(Stack S):判断堆栈S是否已满,返回1或0;int IsEmpty (Stack S ):判断堆栈S是否为空,返回1或0;void Push(Stack S, ElementType item ):将元素item压入堆栈S;ElementType Pop(Stack S ):删除并返回S的栈顶元素。实现队列的操作,即入队void AddQ(ElementType item)和出队ElementType DeleteQ()。
输入首先给出两个正整数N1和N2,表示堆栈S1和S2的最大容量。随后给出一系列的队列操作:A item表示将item入列(这里假设item为整型数字);D表示出队操作;T表示输入结束。
对输入中的每个D操作,输出相应出队的数字,或者错误信息ERROR:Empty。如果入队操作无法执行,也需要输出ERROR:Full。每个输出占1行。
- 3 2
- A 1 A 2 A 3 A 4 A 5 D A 6 D A 7 D A 8 D D D D T
- ERROR:Full
- 1
- ERROR:Full
- 2
- 3
- 4
- 7
- 8
- ERROR:Empty
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
- #include
- using namespace std;
-
- int main()
- {
- int n1, n2;
- char ch;
- cin >> n1 >> n2;
- if(n1 > n2) swap(n1, n2);
- stack<int> s1, s2;
- while(cin >> ch && ch != 'T'){
- if(ch == 'A'){
- int x;
- cin >> x;
- if(s1.size() < n1) s1.push(x);
- else if(s2.empty()){
- while(!s1.empty()){
- s2.push(s1.top());
- s1.pop();
- }
- s1.push(x);
- }
- else cout << "ERROR:Full\n";
- }
- else {
- if(!s2.empty()){
- cout << s2.top() << endl;
- s2.pop();
- }
- else if(!s1.empty()){
- while(!s1.empty()){
- s2.push(s1.top());
- s1.pop();
- }
- cout << s2.top() << endl;
- s2.pop();
- while(!s2.empty()){
- s1.push(s2.top());
- s2.pop();
- }
- }
- else cout << "ERROR:Empty" << endl;
- }
- }
- return 0;
- }
/*
栈的特点:后进先出
队列的特点:先进先出
在两个栈之间来回倒腾,以模拟队列
*/
7-9 选数
已知n个整数x1,x2,x3...xi,以及1个整数k(k
3+7+19=29,
7+12+19=38,
3+12+19=34,
现在,要求你计算出和为素数共有多少种。
例如上例,只有一种的和为素数:3+7+19=29
第一行两个空格隔开的整数 n,k(1≤n≤20,k
输出一个整数,表示种类数。
在这里给出一组输入。例如:
- 4 3
- 3 7 12 19
在这里给出相应的输出。例如:
1
代码长度限制
16 KB
时间限制
1000 ms
内存限制
128 MB
- #include
- using namespace std;
-
- const int N = 25;
- int a[N], n, k, cnt;
- bool isPrime(int n)
- {
- for(int i = 2; i * i <= n; i++){
- if(n % i == 0) return 0;
- }
- return 1;
- }
- void dfs(int m, int sum, int x)// x表示现在已经选取了几个数
- {
- if(m == k){
- if(isPrime(sum))
- cnt++;
- return ;
- }
- for(int i = x; i < n; i++){
- dfs(m + 1, sum + a[i], i + 1);
- }
- return ;
- }
- int main()
- {
- cin >> n >> k;
- for(int i = 0; i < n; i++){
- cin >> a[i];
- }
- dfs(0, 0, 0);
- cout << cnt;
- return 0;
- }
7-10 全排列
Lc今天上课学会了数的全排列并且Lc觉得数的全排列很简单,但是直到Lc的同桌YooQ向他提出了一个问题,该问题的描述如下:我们知道n的全排列总共有n!个序列,例如2的全排列有两个序列{1,2}和{2,1},现在你要解决的问题是n的全排列的n!个序列中第m个序列是什么?(注意:n的全排列的n!个序列是按字典序由小到大排序的)
第一行为样例组数t(t≤1e5),接下来t行每行有一个整数n和m(1<=n<=20,1<=m<=n!)
输出t行,每行输出n的全排列的n!个序列中第m个序列,两相邻的数间有一空格,行末不得有多余空格。
在这里给出一组输入。例如:
- 2
- 1 1
- 3 6
在这里给出相应的输出。例如:
- 1
- 3 2 1
代码长度限制
16 KB
时间限制
1000 ms
内存限制
- #include
- using namespace std;
-
- const int N = 25;
- int a[N];
- int main()
- {
- int t, n, m;
- cin >> t;
- while(t--){
- cin >> n >> m;
- for(int i = 0; i < n; i++){
- a[i] = i + 1;// !!!
- }
- if(m == 1){
- for(int i = 0; i < n - 1; i++){
- cout << a[i] << " ";
- }
- cout << a[n - 1] << endl;
- }
- else{
- int cnt = 2;
- while(next_permutation(a, a + n)){
- if(cnt == m){
- for(int i = 0; i < n - 1; i++){
- cout << a[i] << " ";
- }
- cout << a[n - 1] << endl;
- break;
- }
- cnt++;
- }
- }
- }
- return 0;
- }
/*
将1~n存到一个数组里,聪明!!
*/
7-11 输出全排列
请编写程序输出前n个正整数的全排列(n<10),并通过9个测试用例(即n从1到9)观察n逐步增大时程序的运行时间。
输入给出正整数n(<10)。
输出1到n的全排列。每种排列占一行,数字间无空格。排列的输出顺序为字典序,即序列a1,a2,⋯,an排在序列b1,b2,⋯,bn之前,如果存在k使得a1=b1,⋯,ak=bk 并且 ak+1 代码长度限制 16 KB 时间限制 3500 ms 内存限制 64 MB输入样例:
3
输出样例: