The test machine _06 USTC

2. given four [year, month], determines how many days of this month. Pseudocode given topic leap determination method.

#include<iostream>

using namespace std;

int months[2][13]={
    {0,31,28,31,30,31,30,31,31,30,31,30,31},//平年
    {0,31,29,31,30,31,30,31,31,30,31,30,31}//闰年
};

bool isrein(int year){
    return (year%400==0)||(year%4==0&&year%100!=0);
}

int main(){
    int year,month;
    while(cin>>year>>month){
        if(isrein(year)) cout<<months[1][month];
        else cout<<months[0][month];
    }
}

3. give some identifier, determines how many legal identifier. (C language identifier is consistent with the definition.)

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;

bool first(char c){
    return (c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c=='_');
}

bool others(char c){
    return (c>='a'&&c<='z')||(c>='A'&&c<='Z')||isdigit(c)||(c=='_');
}

int main(){
    int n;
    while(cin>>n){
        vector<string> v;
        int Count=0;
        for(int i=0;i<n;i++){
            string s;
            cin>>s;
            v.push_back(s);
        }
        for(int i=0;i<v.size();i++){
            string s1=v[i];
            if(first(s1[0])){
                bool flag=true;
                for(int j=1;j<s1.size();j++){
                    if(!others(s1[j])) flag=false;
                }
                if(flag) Count++;
            }
            else continue;
        }
        cout<<Count<<endl;
    }
}

4. Fourth channels, given the matrix of FIG connectionless, seeking each connected component

#include<iostream>
#include<vector>
#include<cstdio>
const int MAXN=1000;

int father[MAXN];
int height[MAXN];//以该节点为根的树的高度
using namespace std;

void Initial(){
    for(int i=0;i<MAXN;i++){
        father[i]=i;
        height[i]=0;
        //inDegree[i]=0;
        //visit[i]=false;
    }
}

int FindRoot(int x){
    if(father[x]!=x) father[x]=FindRoot(father[x]);//注意写法
    return father[x];
}

void Union(int x,int y){
    x=FindRoot(x);
    y=FindRoot(y);
    if(x!=y){
        if(height[x]<height[y]) father[x]=y;
        else if(height[x]>height[y]) father[y]=x;//前面两种情况树高并没有变
        else{
            father[y]=x;
            height[x]++;
        }
    }
    return ;
}

int main(){
    FILE *fp1,*fp2;//1.定义指针
    fp1=fopen("in.txt","r");//2.指向文件
    fp2=fopen("out.txt","w");
    int n;
    fscanf(fp1,"%d",&n);//3.进行读写
    Initial();
    while(!feof(fp1)){
        int a,b;
        fscanf(fp1,"%d",&a);
        fscanf(fp1,"%d",&b);
        Union(a,b);
    }
    vector<int> roots;
    for(int i=1;i<=n;i++){//因为序号是从1开始
        if(FindRoot(i)==i){
            roots.push_back(i);
        }
    }
}

5. decomposed into an integer is given as many consecutive integers and

#include<iostream>

using namespace std;

int main(){
    int n;
    while(cin>>n){
        int maxi=0,maxj=0;
        bool flag=false;
        for(int i=1;i<=n/2;i++){
            int sum=0;
            for(int j=2;j<n;j++){//j表示从i开始往后连续的长度
                sum=(i+i+j-1)*j/2;
                if(sum>n) break;
                else if(sum==n){
                    flag=true;
                    if(j>maxj){
                        maxi=i;
                        maxj=j;
                    }
                }
            }
        }
        if(flag){
            for(int i=maxi;maxj>0;maxj--,i++){
                cout<<i<<" ";
            }
            cout<<endl;
        }
        else cout<<"分解失败"<<endl;
    }
}

6. given parenthetical expression four operations, required to give reverse Polish

#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<stack>

using namespace std;

stack<char> Stack;
char output[500];
int outLen;
int Priority(char c){//判断运算符优先级
    if(c=='*'||c=='/') return 2;
    else if(c=='+'||c=='-') return 1;
    else return 0;//'('
}

bool isOperator(char op)				//判断是不是操作符
{
	return (op=='+' || op=='-' || op=='*' || op=='/');
}

void rePolish(string s,int len){//输出逆波兰式
	memset(output,'\0',sizeof(output));
	outLen=0;
	for(int i=0;i<len;++i){
	    //操作数
		if(isdigit(s[i])){
            cout<<"shu:"<<s[i]<<endl;
			output[outLen++] = s[i];
			while (i+1<len&&isdigit(s[i+1])){
				output[outLen++] = s[i+1];
				++i;
			}
			output[outLen++] = ' ';		//空格隔开
		}
		//运算符
		else if (isOperator(s[i])){
            cout<<"fu:"<<s[i]<<endl;
            if(!Stack.empty()&&Priority(Stack.top())>=Priority(s[i])){
                while(Priority(Stack.top())>=Priority(s[i])){
                    output[outLen++]=Stack.top();
                    output[outLen++] = ' ';
                    Stack.pop();
                }
			}
			else Stack.push(s[i]);//栈为空,或者栈顶优先级更小
		}
		//'('
		else if (s[i]=='('){
            cout<<"(:"<<s[i]<<endl;
            Stack.push(s[i]);
		}
		//')'
		else if (s[i]==')'){
		    cout<<"):"<<s[i]<<endl;
			while (Stack.top()!='('){
				output[outLen++] = Stack.top();
				output[outLen++] = ' ';
				Stack.pop();
			}
			Stack.pop();// 此时Stack.top()='(',跳过)
		}
		else continue;
	}
	while (!Stack.empty()){//输入完毕,栈中剩余的所有操作符出栈
		output[outLen++] = Stack.top();
		output[outLen++] = ' ';
		Stack.pop();
	}
}

int main(){
	string s;
	while (getline(cin,s)){
        cout<<s<<" "<<s.size();
        Stack.push('#');
		rePolish(s,s.size());
		output[outLen-1] = '\0';
		printf("%s\n",output);
	}
	return 0;
}

Published 23 original articles · won praise 21 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_38440882/article/details/104380468