PTA练习题

Posted Lyh3012648079

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PTA练习题相关的知识,希望对你有一定的参考价值。

 #include<iostream>
 using namespace std;
 class Time
 
     private:
         int hh;
         int mm;
         int ss;
     public:
         Time()
         
             hh = 0;
             mm = 0;
             ss = 0;
         
         void set(int h,int m,int s)
         
             hh=h;
             mm=m;
             ss=s;
         
         friend bool operator >(Time &t1,Time &t2);
         friend ostream & operator <<(ostream &,Time &);
  ;
 bool operator >(Time &t1,Time &t2)
 
     int re1=t1.hh*3600+t1.mm*60+t1.ss;
     int re2=t2.hh*3600+t2.mm*60+t2.ss;
     if(re1>re2)
     
         return true;
     
     else
     
         return false;
     
 
  
 ostream &operator <<(ostream &output,Time&t)
 
     output<<t.hh<<" "<<t.mm<<" "<<t.ss;
     return output;
 
  
 class date
 
     private:
         int year;
         int month;
         int day;
     public:
         date()
         
             year = 0;
             month = 0;
             day = 0;
         
         void set(int y,int m,int d)
         
             year=y;
             month=m;
             day=d;
         
         friend bool operator >(date &d1,date &d2);
         friend ostream & operator <<(ostream &,date &);
 ;
  
 bool operator >(date &d1,date &d2)
 
     int re1=d1.year*365+d1.month*30+d1.day;
     int re2=d2.year*365+d2.month*30+d2.day;
     if(re1>re2)
     
         return true;
     
     else
     
         return false;
     
 
  
 ostream & operator <<(ostream &output,date &d)
 
     output<<d.year<<" "<<d.month<<" "<<d.day;
     return output;
 
  
 template <class T>
 T maxn(T x[], int len)
 
     T max;
     max=x[0];
     for(int i=0; i<len; i++)
     
         if(x[i]>max)
         
             max=x[i];
         
     
     cout<<max<<endl;
     return max;
 
  
 int main()
 
     int intArray[100];
  
     double douArray[100];
     Time TimeArray[100];
     date dateArray[100];
     int ch;
     int i=0;
     while(cin>>ch)
     
         if(ch==-1)
         
             break;
         
         if(ch==1)
         
             while(cin>>intArray[i])
             
                 if(intArray[i]==0)
                 
                     break;
                 
                 i++;
             
             maxn(intArray,i);
         
         if(ch==2)
         
             while(cin>>douArray[i])
             
                 if(douArray[i]==0)
                 
                     break;
                 
                 i++;
             
             maxn(douArray,i);
         
         if(ch==3)
         
             int hour,minute,second;
             while(cin>>hour)
             
                 if(hour==0)
                 
                     break;
                 
                 cin>>minute>>second;
                 TimeArray[i].set(hour,minute,second);
                 i++;
             
             maxn(TimeArray,i);
         
         if(ch==4)
         
             int years,months,days;
  
             while(cin>>years)
             
                 if(years==0)
                 
                     break;
                 
                 cin>>months>>days;
                 dateArray[i].set(years,months,days);
                 i++;
             
             maxn(dateArray,i);
         
     
     return 0;
 

 

PTA的Python练习题(十九)

好久没写题目了,最近C语言也快忘得差不多了,要孰能生巧

1.


def fn(m, n):
  sum=res=0
  for i in range(1, n+1):
    sum=sum+m
    m=m*10
    res=res+sum
  return res

 

2.

我以为我看懂题目了,其实没有

def isPrime(num):
    num=int(num)
    for i in range(2,num):
        if num%i==0 :
            return False
    if(num!=1):
        return True
def PrimeSum(a,b):
    sum=0
    for i in range(a,b+1):
        if isPrime(i):
            sum+=i
    return sum      

这里要定义两个函数,第二个函数算sum时候会调用第一个函数判断是否是素数

以上是关于PTA练习题的主要内容,如果未能解决你的问题,请参考以下文章

PTA的Python练习题(二十)

PTA的Python练习题

PTA的Python练习题

PTA练习题

PTA的Python练习题(十九)

PTA的Python练习题(十五)