7-130 Balancing Symbols
Balancing symbols: Check if parenthesis (), brackets[], and braces{} are balanced.
输入格式:
1 line
An expression includes variables and symbols. The max length is 50.
输出格式:
1 line
If all the () [] {} are balanced, then output 0.
If () are not balanced, output 1; If [] not balanced, output 2; If {} not balance, output 3.
If more than one symbol are not balanced, output corresponding numbers, sort them in ascending order and divided them by comma.
输入样例:
在这里给出一组输入。例如:
{a*(b-c)-x/2]
输出样例:
在这里给出相应的输出。例如:
2,3,
#include<stdio.h>
#include<stdlib.h>
#define max 100
typedef struct node{
char data;
struct node* next;
}*Stack;
int isEmpty(Stack s);
void pop(Stack s);
void push(Stack s,char x);
char getTop(Stack s);
main()
{
char str[max],tmp;
int i=0,re[3]={0},flag=0;
Stack s=NULL;
s=(Stack)malloc(sizeof(struct node));
s->next=NULL;
scanf("%s",str);
while(str[i]!='\0')
{
switch(str[i])
{
case '(':
case '[':
case '{':
push(s,str[i]);
i++;
break;
case ')':
//不匹配
if(isEmpty(s))
{
re[0]=1;
}
else
{
tmp=getTop(s);
if(tmp!='(')
{
re[0]=1;
}
//匹配
else
{
pop(s);
}
}
i++;
break;
case ']':
//不匹配
if(isEmpty(s))
{
re[1]=1;
}
else
{
tmp=getTop(s);
if(tmp!='[')
{
re[1]=1;
}
//匹配
else
{
pop(s);
}
}
i++;
break;
case '}':
//不匹配
if(isEmpty(s))
{
re[2]=1;
}
else
{
tmp=getTop(s);
if(tmp!='{')
{
re[2]=1;
}
//匹配
else
{
pop(s);
}
}
i++;
break;
default:
i++;
break;
}
}
while(!isEmpty(s))
{
tmp=getTop(s);
switch(tmp)
{
case '(':
re[0]=1;
break;
case '[':
re[1]=1;
break;
case '{':
re[2]=1;
break;
default:
break;
}
pop(s);
}
for(i=0;i<3;i++)
{
if(re[i])
{
printf("%d,",i+1);
flag++;
}
}
if(flag==0)
{
printf("0");
}
while(s->next!=NULL)
{
pop(s);
}
free(s);
}
int isEmpty(Stack s)
{
if(s->next==NULL)
{
return 1;
}
else
{
return 0;
}
}
void pop(Stack s)
{
if(s->next==NULL)
{
printf("\nempty stack!\n");
}
else
{
Stack tmp=s->next;
s->next=s->next->next;
free(tmp);
}
}
void push(Stack s,char x)
{
Stack c=(Stack)malloc(sizeof(struct node));
c->data=x;
c->next=s->next;
s->next=c;
c=NULL;
free(c);
}
char getTop(Stack s)
{
if(s->next==NULL)
{
printf("\nempty stack\n");
}
else
{
return s->next->data;
}
}
7-131 Infix to Postfix Conversion
Convert the infix expression to postfix expression.
输入格式:
1 line.
A correct expression include +,-,*,/,(,) and integer. Each integer is less than 10 and more than 0.
输出格式:
2 line.
The first line gives the result of expression. Keep 2 digits after the decimal point.
The second line gives the postfix expression. Use a blank space to divide every number and symbol. There is a blank space at the end of the line.
输入样例:
在这里给出一组输入。例如:
2*(3+4)/5
输出样例:
在这里给出相应的输出。例如:
2.80
2 3 4 + * 5 /
#include<stdio.h>
#include<stdlib.h>
struct node{
char element;
struct node* next;
};
typedef struct node *PtrToNode;
typedef PtrToNode Stack;
struct Node{
float element;
struct Node* next;
};
typedef struct Node* nstack;
int isEmpty(Stack s);
int isEmptyn(nstack ns);
void makeEmpty(Stack s);
void makeEmptyn(nstack ns);
Stack createStack();
nstack createStackn();
char pop (Stack s);
float popn (nstack ns);
void push (char x, Stack s);
void pushn (float x, nstack ns);
char top(Stack s);
main(){
// Stack s=(Stack)malloc(sizeof(struct node));
// s->next=NULL;
Stack s=createStack();
char str[100],re[100],tmp;
int i=0,j=0,len=0;
scanf("%s",str);//输入
//中缀表达式转后缀表达式
while(str[i]!='\0'){
if(str[i]>'0' && str[i]<='9'){
//数字直接存入结果数组中
re[j++]=str[i];
}
else if(str[i]=='('){
//(直接压栈
push(str[i],s);
}
else if(str[i]==')'){
//)弹栈直至遇到(
tmp=pop(s);
while(tmp!='('){
re[j++]=tmp;
tmp=pop(s);
}
}
else if(str[i]=='+' || str[i]=='-'){
//+-优先级较低,弹栈至遇到(或空栈,(还要压栈压回去
if(!isEmpty(s)){
//如果不是空栈
tmp=pop(s);
while(tmp!='('){
re[j++]=tmp;
if(!isEmpty(s)){
tmp=pop(s);
}else{
break;
}
}
if(tmp=='('){
push(tmp,s);
}
}
push(str[i],s);
}
else if(str[i]=='*' || str[i]=='/'){
//优先级高,只有乘除才弹栈
if(!isEmpty(s)){
//不是空栈
tmp=pop(s);
while(tmp=='*' || tmp=='/'){
re[j++]=tmp;
if(!isEmpty(s)){
tmp=pop(s);
}else{
break;
}
}
if(tmp=='+' || tmp=='-' || tmp=='('){
push(tmp,s);
}
}
push(str[i],s);
}
else{
printf("\nWrong format\n");
}
i++;
}
while(!isEmpty(s)){
tmp=pop(s);
re[j++]=tmp;
}
len=j;
//计算结果
float right=0,left=0,n=0;
nstack ns=(nstack)malloc(sizeof(struct Node));
ns->next=NULL;
for(j=0;j<len;j++){
switch(re[j]){
case '+':
right=popn(ns);
left=popn(ns);
pushn(right+left,ns);
break;
case '-':
right=popn(ns);
left=popn(ns);
pushn(left-right,ns);
break;
case '*':
right=popn(ns);
left=popn(ns);
pushn(left*right,ns);
break;
case '/':
right=popn(ns);
left=popn(ns);
pushn(left/right,ns);
break;
default:
n=re[j]-'0';
pushn(n,ns);
break;
}
}
//输出结果
n=popn(ns);
printf("%.2lf\n",n);
for(j=0;j<len;j++){
printf("%c ",re[j]);
}
}
int isEmpty(Stack s){
return s->next == NULL;
}
int isEmptyn(nstack ns){
return ns->next==NULL;
}
void makeEmpty(Stack s) {
if (s == NULL){
printf("\nMust use CreateStack first\n");
}else{
while (!isEmpty(s)){
pop(s);
}
}
}
void makeEmptyn(nstack ns){
if(ns==NULL){
printf("\nMust use CreateStack first\n");
}else{
while(!isEmptyn(ns)){
popn(ns);
}
}
}
Stack createStack(){
Stack s;
s = (Stack)malloc (sizeof (struct node));
if (s==NULL) {
printf("\nOut of space!!\n");
}
s->next=NULL;
makeEmpty(s);
return s;
}
nstack createStackn(){
nstack ns;
ns=(nstack)malloc(sizeof(struct Node));
if(ns==NULL){
printf("\nOut of space!!\n");
}
ns->next=NULL;
makeEmptyn(ns);
return ns;
}
char pop (Stack s) {
char tmp;
PtrToNode first;
if (isEmpty(s)) {
printf("\nempty stack\n");
} else {
first = s->next;
s->next = s->next->next;
tmp=first->element;
free(first);
}
return tmp;
}
float popn (nstack ns) {
float tmp;
nstack first;
if (isEmptyn(ns)) {
printf("\nempty stack\n");
} else {
first = ns->next;
ns->next = ns->next->next;
tmp=first->element;
free(first);
}
return tmp;
}
void push (char x, Stack s) {
PtrToNode temp;
temp = (PtrToNode)malloc(sizeof(struct node));
if (temp == NULL) {
printf("\nout of space!!\n");
} else {
temp->element = x;
temp ->next = s->next;
s->next = temp;
}
temp=NULL;
free(temp);
}
void pushn (float x, nstack ns) {
nstack temp;
temp = (nstack)malloc(sizeof(struct Node));
if (temp == NULL) {
printf("\nout of space!!\n");
} else {
temp->element = x;
temp ->next = ns->next;
ns->next = temp;
}
temp=NULL;
free(temp);
}
char top(Stack s) {
if (!isEmpty(s)) {
return s->next->element;
}
printf("\nEmpty stack\n");
return 0;
}
7-132 central list
If a list is central symmetry, it is called a central list. for example, 1->3->5->7->9->7->5->3->1 is a central list.
Give you a sequence of numbers, form a link list and then judge whether it is a central list.
输入格式:
A sequence of numbers seperated by comma
输出格式:
If it is a central list output Yes, otherwise No
输入样例:
1,3,5,7,9,7,5,3,1,
输出样例:
Yes
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>num;
int main()
{
int x;
while(~scanf("%d,",&x)){
num.push_back(x);
}
bool flag = 0;
int len = num.size();
for(int i = 0, j = len - 1;i <= j ; i++,j--){
if(num[i] != num[j]){
flag = 1;
break;
}
}
if(flag) cout << "No";
else cout << "Yes";
return 0;
}
7-133 英雄出场王
英雄联盟总决赛正在若火如荼的展开,盲僧、刀妹、酒桶、青钢影等各路英雄悉数登场,当一个英雄被选出场时系统自动登记其序号,出场次数最多的英雄成为出场王。给定英雄序号的出场集合T,例如,T={2,4,4,4,6,7}。其出场王是4号英雄,出场次数为3。对于给定的由n个序号组成的出场集T,计算出场王序号及其出场次数。如果出现多个出场王,请输出序号最小的那个。
输入格式:
输入数据的第1行是英雄出场集T中序号个数n(n<1000);第二行输入n个出场英雄序号(不超过5位数字的自然数)。
输出格式:
输出数据的第1行给出出场王序号,第2行是出场次数。
输入样例:
在这里给出一组输入。例如:
6
2 4 4 4 6 7
输出样例:
在这里给出相应的输出。例如:
4
3
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int id,maxx,cnt[1000000];
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n ; i++){
int x;
cin >> x;
cnt[x]++;
if(cnt[x] > maxx){
maxx = cnt[x];
id = x;
}
else if(cnt[x] == maxx && id > x)
id = x;
}
cout << id << "\n" << maxx;
return 0;
}