PTA练习题

Posted Lyh3012648079

tags:

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

定义一个Dog类,包括体重和年龄两个数据成员及其成员函数,声明一个实例dog1,体重5,年龄10,使用I/O流把dog1的状态写入磁盘文件。再声明一个实例dog2,通过读取文件dog1的状态赋给dog2。分别用文本方式和二进制方式操作文件。

 #include <iostream>
 #include <fstream>
 using namespace std;
 class  Dog
     public:
         int ages;
         int weight;
         Dog(int w,int a)
         
             weight=w;
             ages=a;
         
         Dog()
         
 ;
 int main()
 
     Dog dog1(5,10);
     fstream file1;
     file1.open("text.txt",ios::out);
     file1<<dog1.weight<<endl;
     file1<<dog1.ages<<endl;
     file1.close();
     ifstream file2;
     file2.open("text.txt",ios::in);
     if(!file2.is_open())
     
         cout<<"文件打开失败"<<endl;
         return 0;
     
     Dog dog2(1,1);
     file2>>dog2.weight>>dog2.ages;
     cout<<dog2.weight<<endl;
     cout<<dog2.ages<<endl;
     file2.close();    
 
 #include <iostream>
 #include <fstream>
 using namespace std;
 class  Dog
     public:
         int ages;
         int weight;
         Dog(int w,int a)
         
             weight=w;
             ages=a;
         
         Dog()
         
 ;
 int main()
 
     Dog dog1(5,10);
     fstream file1("text.txt",ios::out|ios::binary);
     file1.write((const char*)&dog1,sizeof(dog1));
     file1.close();    
     ifstream file2;
     file2.open("text.txt",ios::in|ios::binary);
     if(!file2.is_open())
     
         cout<<"文件打开失败"<<endl;
     
     Dog dog2;
     file2.read((char*)&dog2,sizeof(dog1));
     cout<<dog2.weight<<endl;
     cout<<dog2.ages<<endl;
     file2.close();
 

 

PTA的Python练习题

从 第3章-8 字符串逆序 开始

1.

n = str(input())
n1=n[::-1]
print(n1)

 

2.

不是很好做这道题,自己还是C语言的思维,网上几乎也找不到什么答案

s = input()
idx = s.find("#")
s = s[:idx]
ss = s[:idx]
s_16 = "0123456789ABCDEFabcdef"

for c in s:
    if c not in s_16:
        s = s.replace(c, \'\')

flag = 0
for c in ss:
    if c in s_16:
        flag = ss.find(c)
        break

if \'-\' in ss[:flag]:
    s = \'-\' + s

if len(s) == 0:
    num_10 = 0
else:
    num_10 = int(s, 16)
print(num_10)

 

3.

滤掉5个字母,还有空格和感叹号

a=input()
count=0
for i in range(0,len(a)):
    if(a[i]!=\'A\' and a[i]!=\'E\' and a[i]!=\'I\' and a[i]!=\'O\' and a[i]!=\'U\' and a[i]!=\'!\' and a[i]!=\' \' and \'A\'<=a[i]<=\'Z\'):
        count=count+1
print(count)

 

4.

我还在思考字符串怎么排序,发现其实之前做过的sort()函数可以用来无差别排序,一下子就简单了:

ls =list(input().split())
ls1=sorted(ls)
print("After sorted")
for i in ls1:
    print(i)

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

PTA的Python练习题(二十)

PTA的Python练习题

PTA的Python练习题

PTA练习题

PTA的Python练习题(十九)

PTA的Python练习题(十五)