• PAT第二次上机题目


    5-1

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    template <class T> T maxn(T x[], int len)
    {
    	int i;
    	T store = x[0];
    	
    	int k = 0;
    	for (i = 0; i < len; i++)
    	{
    		if(x[i] > x[k]) k = i;
    	}
    	
    	store = x[k];
    	
    	return store;
    }
    
    class time0
    {
    	private:
    		int hh,mm,ss;
    	public:
    		time0(int a = 0, int b = 0, int c = 0):hh(a),mm(b),ss(c){};
    		bool operator > (time0 &t);
    		void display();
    };
    
    void time0::display()
    {
    	cout << hh << " " << mm << " " << ss << endl;
    }
    
    bool time0::operator > (time0 &t)
    {
    	int pre = 0, beh = 0;
    	pre = hh * 3600 + mm * 60 + ss;
    	beh = t.hh * 3600 + t.mm * 60 + ss;
    	if(pre > beh)return true;
    	else return false;
    }
    
    class date
    {
    	private:
    		int year,mouth,day;
    	public:
    		date(int a = 0, int b = 0, int c = 0):year(a),mouth(b),day(c){};
    		bool operator > (date &d);
    		void display();
    };
    
    void date::display()
    {
    	cout << year << " " << mouth << " " << day << endl;
    }
    
    bool date::operator > (date &d)
    {
    	int pre = 0, beh = 0;
    	pre = year * 365 + mouth * 30 + day;
    	beh = d.year * 365 + d.mouth * 30 + day;
    	if(pre > beh)return true;
    	else return false;
    }
    
    int intArray[100];
    
    double douArray[100];
    
    time0 timeArray[100];
    
    date dateArray[100];
    
    int main()
    {
    	int ope;
    	int i,j;
    	while(cin >> ope)
    	{
    		if(ope == -1)break;
    		
    		if(ope == 1)
    		{
    			int tot = 0;
    			while(1)
    			{
    				int w;
    				cin >> w;
    				if(w == 0)break;
    				
    				intArray[tot++] = w;
    			}
    			
    			double result = 0;
    			result = maxn<int> (intArray,tot);
    			
    			cout << result << endl;
    		}
    		else if(ope == 2)
    		{
    			int tot = 0;
    			while(1)
    			{
    				double w;
    				cin >> w;
    				if(w == 0)break;
    				
    				douArray[tot++] = w;
    			}
    			
    			double result = 0;
    			result = maxn<double> (douArray,tot);
    			
    			cout << result << endl;
    		}
    		else if(ope == 3)
    		{
    			int hh1,mm1,ss1;
    			int tot = 0;
    			while(1)
    			{
    				cin >> hh1;
    				if(hh1 == 0)break;
    				
    				cin >> mm1 >> ss1;
    				
    				time0 t(hh1,mm1,ss1);
    				timeArray[tot++] = t;
    			}
    			
    		    time0 result;
    		    result = maxn<time0> (timeArray, tot);
    		    
    		    result.display();
    		}
    		else if(ope == 4)
    		{
    			int hh1,mm1,ss1;
    			int tot = 0;
    			while(1)
    			{
    				cin >> hh1;
    				if(hh1 == 0)break;
    				
    				cin >> mm1 >> ss1;
    				
    				date t(hh1,mm1,ss1);
    				dateArray[tot++] = t;
    			}
    			
    		    date result;
    		    result = maxn<date> (dateArray, tot);
    		    
    		    result.display();
    		}
    	}
    	return 0;
    }
    

    5-2

    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    
    template <class T> int addSet(T * myset, T elem,int len)
    {
    	if(len > 100)
    	{
    		cout << "Full Set." << endl;
    		return len;
    	}
    	
    	int i, j;
    	for(i = 0; i < len; i++)
    	{
    		if(*(myset + i) == elem)
    		{
    			cout << elem << " is already exist!" << endl;
    			return len;
    		}
    	}
    	
    	*(myset + len) = elem;
    	
    	cout << len-1 << endl;
    	
    	len ++;
    	
    	return len;
    }
    
    template <class T> int deleSet(T * myset, T elem, int len)
    {
    	int i, j;
    	
    	bool flag = false;
    	int beg = 1;
    	for(i = 0; i < len; i++)
    	{
    		if(*(myset + i) == elem)
    		{
    			flag = true;
    			beg = i;
    			break;
    		}
    	}
    	
    	if(!flag)
    	{
    		cout << elem << " is not exist!" << endl;
    		return len;
    	}
    	
    	cout << beg-1 << endl;
    	
    	for(i = beg, j = beg + 1; j < len; i++, j++)
    	{
    		*(myset + i) = *(myset + j);
    	}
    	
    	len --;
    	
    	return len;
    }
    
    template <class T> int findElem(T * myset, T elem, int len)
    {
    	int i, j;
    	
    	for(i = 0; i < len; i++)
    	{
    		if(*(myset + i) == elem)
    		{
    			return i;
    		}
    	}
    	
    	return -1;
    }
    
    int intSet[100];
    
    double douSet[100];
    
    string StrSet[100];
    
    int main()
    {
        int intLen = 1, douLen = 1, strLen = 1;
    
    	int i;
    	
    	int ope;
    	
    	while(cin >> ope)
    	{
    		if(ope == 0)break;
    		
    		if(ope == 1)
    		{
    			int nope, inum;
    			cin >> nope >> inum;
    			
    			if(nope == 1)
    			{
    				intLen = addSet<int> (intSet, inum, intLen);
    			}
    			else if(nope == 2)
    			{
    				intLen = deleSet<int> (intSet, inum, intLen);
    			}
    			else if(nope == 3)
    			{
    				int judge = findElem<int> (intSet, inum, intLen);
    				if(judge == -1)
    				{
    	                cout << inum << " is not exist!" << endl;
    				}
    				else
    				{
    					cout << judge-1 << endl;
    				}
    			}
    		}
    		else if(ope == 2)
    		{
    			int nope;
    			double inum;
    			cin >> nope >> inum;
    			
    			if(nope == 1)
    			{
    				douLen = addSet<double> (douSet, inum, douLen);
    			}
    			else if(nope == 2)
    			{
    				douLen = deleSet<double> (douSet, inum, douLen);
    			}
    			else if(nope == 3)
    			{
    				int judge = findElem<double> (douSet, inum, douLen);
    				if(judge == -1)
    				{
    	                cout << inum << " is not exist!" << endl;
    				}
    				else
    				{
    					cout << judge-1 << endl;
    				}
    			}
    		}
    		else if(ope == 3)
    		{
    			int nope;
    			string inum;
    			cin >> nope >> inum;
    			
    			if(nope == 1)
    			{
    				strLen = addSet<string> (StrSet, inum, strLen);
    			}
    			else if(nope == 2)
    			{
    				strLen = deleSet<string> (StrSet, inum, strLen);
    			}
    			else if(nope == 3)
    			{
    				int judge = findElem<string> (StrSet, inum, strLen);
    				if(judge == -1)
    				{
    	                cout << inum << " is not exist!" << endl;
    				}
    				else
    				{
    					cout << judge-1 << endl;
    				}
    			}
    		}
    	}
    	return 0;
    }
    

    5-3

    (听说用5-2的代码也能过噢

    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    template <class T> class MySet
    {
    private:
        T data[100];
        int count;
        
    public:
    	MySet()
    	{
    		count = 0; 
    	};
    	
        int addSet(T elem)
        {
    	    if(count > 100)
    	    {
    		    cout << "Full Set." << endl;
    		    return count;
    	    }
    	
    	    int i, j;
    	    for(i = 0; i < count; i++)
    	    {
    		    if(data[i] == elem)
    		    {
    			    cout << elem << " is already exist!" << endl;
    			    return count;
    		    }
    	    }
    	
    	    data[count] = elem;
    	
    	    cout << count << endl;
    	
    	    count ++;
    	
    	    return count;
        };
        
        int deleSet(T elem)
        {
    	    int i, j;
    	
    	    bool flag = false;
    	    int beg = 1;
    	    for(i = 0; i < count; i++)
    	    {
    		    if(data[i] == elem)
    		    {
    			    flag = true;
    			    beg = i;
    			    break;
    		    }
     	    }
    	
    	    if(!flag)
    	    {
    		    cout << elem << " is not exist!" << endl;
    		    return count;
    	    }
    	
    	    cout << beg << endl;
    	
    	    for(i = beg, j = beg + 1; j < count; i++, j++)
    	    {
    		    data[i] = data[j];
    	    }
    	
    	    count --;
    	
    	    return count;
        };
        
        int findElem(T elem)
        {
    	    int i, j;
    	
    	    for(i = 0; i < count; i++)
    	    {
    		    if(data[i] == elem)
    		    {
    			    return i;
    		    }
    	    }
    	
    	    return -1;
        };
    
    };
    
    //int MySet::addSet(T elem)
    
    
    //int MySet::deleSet(T elem)
    
    
    //int MySet::findElem(T elem)
    
    int main()
    {
    	MySet<int> intSet;
        MySet<double> douSet;
        MySet<string> strSet;
    	
        int intLen = 1, douLen = 1, strLen = 1;
    
    	int i;
    	
    	int ope;
    	
    	while(cin >> ope)
    	{
    		if(ope == 0)break;
    		
    		if(ope == 1)
    		{
    			int nope, inum;
    			cin >> nope >> inum;
    			
    			if(nope == 1)
    			{
    				intLen = intSet.addSet(inum);
    			}
    			else if(nope == 2)
    			{
    				intLen = intSet.deleSet(inum);
    			}
    			else if(nope == 3)
    			{
    				int judge = intSet.findElem(inum);
    				if(judge == -1)
    				{
    	                cout << inum << " is not exist!" << endl;
    				}
    				else
    				{
    					cout << judge << endl;
    				}
    			}
    		}
    		else if(ope == 2)
    		{
    			int nope;
    			double inum;
    			cin >> nope >> inum;
    			
    			if(nope == 1)
    			{
    				douLen = douSet.addSet(inum);
    			}
    			else if(nope == 2)
    			{
    				douLen = douSet.deleSet(inum);
    			}
    			else if(nope == 3)
    			{
    				int judge = douSet.findElem(inum);
    				if(judge == -1)
    				{
    	                cout << inum << " is not exist!" << endl;
    				}
    				else
    				{
    					cout << judge << endl;
    				}
    			}
    		}
    		else if(ope == 3)
    		{
    			int nope;
    			string inum;
    			cin >> nope >> inum;
    			
    			if(nope == 1)
    			{
    				strLen = strSet.addSet(inum);
    			}
    			else if(nope == 2)
    			{
    				strLen = strSet.deleSet(inum);
    			}
    			else if(nope == 3)
    			{
    				int judge = strSet.findElem(inum);
    				if(judge == -1)
    				{
    	                cout << inum << " is not exist!" << endl;
    				}
    				else
    				{
    					cout << judge << endl;
    				}
    			}
    		}
    	}
    	return 0;
    }
    

    5-4

    注意:(备注说明:分数为0时,表示成0z1m,如果结果为负数,那么分子取负数,分母为正数)

    #include <iostream>
    #include <cstdlib>
    #include <sstream>
    #include <cmath>
    using namespace std;
    
    int small(int a, int b)
    {
    	if(a == 0 || b == 0)return 0;
    	
    	int t;
    	if(a < b)
    	{
    		t = a; a = b; b = t;
    	}
    	int c = a % b;
    	while(c != 0)
    	{
    		a = b;
    		b = c;
    		c = a % b;
    	}
    	
    	return b;
    }
    
    class FS
    {
    	private:
    		int fz,fm;
    	public:
    		FS(int a = 0, int b = 0):fz(a),fm(b){};
    		FS operator + (const FS &f);
    		void display();
    };
    
    void FS::display()
    {
    	bool flag = false;
    	
    	if(fz * fm == 0)
    	{
    		cout << "0z1m" << endl;
    		return ;
    	}
    	
    	if(fz * fm < 0)
    	{
    	    flag = true;
    	}
    	if(fz < 0)fz = -fz;
    	if(fm < 0)fm = -fm;
    	
    	if(flag)cout << "-";
    	
    	cout << fz << "z" << fm << "m" << endl;
    }
    
    FS FS::operator + (const FS &f)
    {
    	bool flag1 = false, flag2 = false; // false - true +
    	
    	if(fz*fm < 0)flag1 = true;
    	if(f.fz*f.fm < 0)flag2 = true;
    	
    	int newfz = 0,newfm = 0;
    	if(!flag1 && !flag2)
    	{
    		int common = 0;
    		if(fz == 0 || f.fz == 0)
    		{
    			if(fz == 0)
    			{
    				newfz = f.fz;
    				newfm = f.fm;
    			}
    			else if(f.fz == 0)
    			{
    				newfz = fz;
    				newfm = fm;
    			}
    		}
    		else
    		{
    			newfz = fz * f.fm + fm * f.fz;
    		    newfm = fm * f.fm;
    		}
    		
    		if(newfz != 0)
    		{
    			common = small(newfz,newfm);
    			newfz /= common;
    			newfm /= common;
    		}
     	}
     	else if(!flag1 && flag2)
     	{
     		int common = 0;
    		if(fz == 0 || f.fz == 0)
    		{
    			if(fz == 0)
    			{
    				newfz = f.fz;
    				newfm = f.fm;
    			}
    			else if(f.fz == 0)
    			{
    				newfz = fz;
    				newfm = fm;
    			}
    		}
    		else
    		{
    			newfz = fz * f.fm + fm * f.fz;
    		    newfm = fm * f.fm;
    		}
    		
    		if(newfz != 0)
    		{
    			common = small(newfz,newfm);
    			newfz /= common;
    			newfm /= common;
    		}
    	}
    	else if(flag1 && !flag2)
     	{
     		int common = 0;
    		if(fz == 0 || f.fz == 0)
    		{
    			if(fz == 0)
    			{
    				newfz = f.fz;
    				newfm = f.fm;
    			}
    			else if(f.fz == 0)
    			{
    				newfz = fz;
    				newfm = fm;
    			}
    		}
    		else
    		{
    			newfz = fz * f.fm + fm * f.fz;
    		    newfm = fm * f.fm;
    		}
    		
    		if(newfz != 0)
    		{
    			common = small(newfz,newfm);
    			newfz /= common;
    			newfm /= common;
    		}
    	}
    	else if(flag1 && flag2)
     	{
     		int common = 0;
     		newfz = - (fz * f.fm + fm * f.fz);
    		newfm = fm * f.fm;
    		
    		if(newfz != 0)
    		{
    			common = small(newfz,newfm);
    			newfz /= common;
    			newfm /= common;
    		}
    		
    		newfz = -newfz;
    	}
    	
    	FS f1(newfz,newfm);
    	return f1;
    }
    
    int main()
    {
    	int n;
    	int i,j,k;
    	cin >> n;
    	
    	string s1,s2;
    	stringstream stream;
    	int sfz = 0, sfm = 0;
    	for(k = 1; k <= n; k++)
    	{
    		cin >> s1 >> s2;
    		
    		string s;
    		for(i = 0; i < s1.length(); i++)
    	    {
    	    	if(s1[i] == 'z')
    			{
    				stream << s;
    				stream >> sfz;
    				stream.clear();
    				s = "";
    				
    				continue;
    			}
    			
    			if(s1[i] == 'm')
    			{
    				stream << s;
    				stream >> sfm;
    				stream.clear();
    				s = "";
    				
    				break;
    			}
    			
    			s += s1[i];
    		}
    		
    		FS c1(sfz,sfm);
    		s = "";
    		sfz = 0;
    		sfm = 0;
    		
    		for(i = 0; i < s2.length(); i++)
    	    {
    	    	if(s2[i] == 'z')
    			{
    				stream << s;
    				stream >> sfz;
    				stream.clear();
    				s = "";
    				
    				continue;
    			}
    			
    			if(s2[i] == 'm')
    			{
    				stream << s;
    				stream >> sfm;
    				stream.clear();
    				s = "";
    				
    				break;
    			}
    			
    			s += s2[i];
    		}
    		
    		FS c2(sfz,sfm);
    		s = "";
    		sfz = 0;
    		sfm = 0;
    		
    		FS c3 = c1 + c2;
    		c3.display();
    	}
    	
    	return 0;
    }
    

    5-5

    考虑的情况比较多,需要耐心。

    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <sstream>
    using namespace std;
    
    class Complex
    {
    	private:
    		int real, imag;
    	public:
    		Complex(int a = 0, int b = 0):real(a), imag(b){};
    		void display();
    		Complex operator * (Complex &b);
    };
    
    void Complex::display()
    {
    	if(real == 0 && imag == 0)
    	{
    		cout << "0" << endl;
    		return ;
    	}
    	
    	else if(real == 0)
    	{
    		if(imag != 1 && imag != -1)
    		cout << imag << "i" << endl;
    		else if(imag == 1)
    		cout << "i" << endl;
    		else
    		cout << "-i" << endl;
    		
    		return ;
    	}
    	
    	else if(imag == 0)
    	{
    		cout << real << endl;
    		return ;
    	}
    	
    	if(imag < 0)
    	{
    		if(imag != -1)
    		cout << real << imag << "i" << endl;
    		else cout << real << "-i" << endl;
    	}
    	else 
    	{
    		if(imag != 1)
    		cout << real << "+" << imag << "i" << endl;
    		else if(imag == 1)
    		cout << real << "+i" << endl;
    	}
    }
    
    Complex Complex::operator * (Complex &b)
    {
    	int creal = 0, cimag = 0;
    	creal = real * b.real - imag * b.imag;
    	cimag = real * b.imag + b.real * imag;
    	
    	Complex c(creal, cimag);
    	return c;
    }
    
    int main()
    {
    	string s1, s2;
    	int i;
    	while (cin >> s1 >> s2)
    	{
    		string s;
    		stringstream stream;
    		
    		int areal = 0, aimag = 0;
    		int breal = 0, bimag = 0;
    		for (i = 0; i < s1.length(); i++)
    		{
    			if (i == 0 && s1[0] == '-')
    			{
    				s += '-';
    				continue;
    			}
    			
    			if (s1[i] == '+' || s1[i] == '-')
    			{
    				stream << s;
    				stream >> areal;
    				stream.clear();
    				
    				s = "";
    				
    				if (s1[i] == '-')
    				s += '-';
    				
    				continue;
    			}
    			
    			if (s1[i] == 'i')
    			{
    				if(s == "-" || s == "")
    				{
    					if(s == "-")aimag = -1;
    					else aimag = 1;
    					
    					s = "";
    					
    					break;
    				}
    				
    				stream << s;
    				stream >> aimag;
    				stream.clear();
    				
    				s = "";
    				
    				break;
    			}
    			
    			s += s1[i];
    		}
    		
    		if (s != "")
    		{
    			stream << s;
    			stream >> areal;
    			stream.clear();
    			
    			s = "";
    		}
    		
    		s = "";
    		
    		for (i = 0; i < s2.length(); i++)
    		{
    			if (i == 0 && s2[0] == '-')
    			{
    				s += '-';
    				continue;
    			}
    			
    			if (s2[i] == '+' || s2[i] == '-')
    			{
    				stream << s;
    				stream >> breal;
    				stream.clear();
    				
    				s = "";
    				
    				if (s2[i] == '-')
    				s += '-';
    				
    				continue;
    			}
    			
    			if (s2[i] == 'i')
    			{
    				if(s == "-" || s == "")
    				{
    					if(s == "-")bimag = -1;
    					else bimag = 1;
    					
    					s = "";
    					
    					break;
    				}
    				
    				stream << s;
    				stream >> bimag;
    				stream.clear();
    				
    				s = "";
    				
    				break;
    			}
    			
    			s += s2[i];
    		}
    		
    		if (s != "")
    		{
    			stream << s;
    			stream >> breal;
    			stream.clear();
    			
    			s = "";
    		}
    		
    		Complex A(areal, aimag), B(breal, bimag);
    		Complex C = A * B;
    		//A.display();
    		//B.display();
    		C.display();
    	}
    	return 0;
    }
    
    //i -i
    
  • 相关阅读:
    bzoj4105: [Thu Summer Camp 2015]平方运算
    bzoj4035: [HAOI2015]数组游戏
    bzoj1022: [SHOI2008]小约翰的游戏John
    bzoj4665: 小w的喜糖
    CodeChef:Little Elephant and Colored Coins
    bzoj4664: Count
    bzoj4498: 魔法的碰撞
    bzoj4230: 倒计时
    bzoj4532: [BeiJing2014 WinterCamp] 珠链
    python 画正态曲线
  • 原文地址:https://www.cnblogs.com/qq952693358/p/5572854.html
Copyright © 2020-2023  润新知