zoukankan      html  css  js  c++  java
  • PTA第一次作业

    5-5

    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    struct Node
    {
    	int data;
    	Node *next;
    };
    
    int tot = 0;
    
    Node *Node_Creat()
    {
    	Node *head;
    	head = (Node *)malloc(sizeof(Node));
    	if(head == NULL)
    	{
    		printf("Overflow
    ");
    		exit(1);
    	}
    	
    	head = NULL;
    	
    	Node *p1,*p2;
    	p1 = (Node *)malloc(sizeof(Node));
    	if(p1 == NULL)
    	{
    		printf("Overflow
    ");
    		exit(1);
    	}
    	scanf("%d",&p1 -> data);
    	
    	while(p1 -> data != -1)
    	{
    		if(p1 -> data % 2 == 0)
    		{
    			scanf("%d",&p1 -> data);
    			continue;
    		}
    		
    		tot++;
    		
    		if(head == NULL)
    		{
    			head = p1;
    		}
    		else
    		{
    			p2 -> next = p1;
    		}
    		
    		p2 = p1;
    		p1 = (Node *)malloc(sizeof(Node));
    		if(p1 == NULL)
    		{
    			printf("Overflow
    ");
    			exit(1);
    		}
    		
    		scanf("%d",&p1 -> data);
    	}
    	p2 -> next = NULL;
    	
    	return head;
    }
    
    void Node_Print(Node *head)
    {
    	Node *p;
    	p = head;
    	
    	while(p -> next != NULL)
    	{
    		printf("%d ",p -> data);
    		p = p -> next;
    	}
    	
    	printf("%d",p -> data);
    }
    
    //没有用到 
    Node *Node_Delete(Node *head,int num)
    {
    	Node *p1,*p2;
    	
    	p1 = p2 = head;
    	
    	if(num == 1)
    	{
    		tot--;
    		head = head -> next;
    		return head;
    	}
    	
    	for(int i = 1; i < num; i++)
    	{
    		p2 = p1;
    		p1 = p1 -> next;
    	}
    	
    	p2 -> next = p1 -> next;
    	tot--;
    	
    	free(p1);
    	
    	return head;
    }
    
    int main()
    {
    	Node *head;
    	head = Node_Creat();
    	
    	Node_Print(head);
    	
    	return 0;
    }
    

    5-4

    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    struct Node
    {
    	int data;
    	Node *next;
    };
    
    int tot = 0;
    
    Node *Node_Creat()
    {
    	Node *head;
    	head = (Node *)malloc(sizeof(Node));
    	if(head == NULL)
    	{
    		printf("Overflow
    ");
    		exit(1);
    	}
    	
    	head = NULL;
    	
    	Node *p1,*p2;
    	p1 = (Node *)malloc(sizeof(Node));
    	if(p1 == NULL)
    	{
    		printf("Overflow
    ");
    		exit(1);
    	}
    	scanf("%d",&p1 -> data);
    	
    	while(p1 -> data != -1)
    	{
    		if(p1 -> data % 2 == 1)
    		{
    			scanf("%d",&p1 -> data);
    			continue;
    		}
    		
    		tot++;
    		
    		if(head == NULL)
    		{
    			head = p1;
    		}
    		else
    		{
    			p2 -> next = p1;
    		}
    		
    		p2 = p1;
    		p1 = (Node *)malloc(sizeof(Node));
    		if(p1 == NULL)
    		{
    			printf("Overflow
    ");
    			exit(1);
    		}
    		
    		scanf("%d",&p1 -> data);
    	}
    	p2 -> next = NULL;
    	
    	return head;
    }
    
    void Node_Print(Node *head)
    {
    	Node *p;
    	p = head;
    	
    	if(head == NULL)return ;
    	
    	while(p -> next != NULL)
    	{
    		printf("%d ",p -> data);
    		p = p -> next;
    	}
    	
    	printf("%d
    ",p -> data);
    }
    
    //还是没有用到 
    Node *Node_Delete(Node *head,int num)
    {
    	Node *p1,*p2;
    	
    	p1 = p2 = head;
    	
    	if(num == 1)
    	{
    		tot--;
    		head = head -> next;
    		return head;
    	}
    	
    	for(int i = 1; i < num; i++)
    	{
    		p2 = p1;
    		p1 = p1 -> next;
    	}
    	
    	p2 -> next = p1 -> next;
    	tot--;
    	
    	free(p1);
    	
    	return head;
    }
    
    int main()
    {
    	Node *head;
    	
    	int repeat;
    	scanf("%d",&repeat);
    	int i,j;
    	
    	for(i = 1; i <= repeat; i++)
    	{
    		head = Node_Creat();
    	
    	    Node_Print(head);
    	}
    	
    	return 0;
    }
    

    5-3

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    using namespace std;
    
    int store[1000005];
    
    int s[1000005];
    
    int main()
    {
    	int i,j;
    	int l,r;
    	scanf("%d%d",&l,&r);
    	
    	int tot = l;
    	for(i = l; i <= r; i++)
    	{
    		for(j = 1; j <= sqrt(i); j++)
    		{
    			if(i % j == 0)store[i]++;
    		}
    		
    		if(sqrt(i) * sqrt(i) == i)
    		{
    			store[i] = store[i]*2 - 1;
    		}
    		else
    		{
    			store[i] *= 2;
    		}
    		
    		if(store[i] > store[tot])tot = i;
    	}
    	
    	printf("[%d,%d] %d ",l,r,tot);
    	
    	int cnt = 1;
    	for(i = 1; i <= tot; i++)
    	{
    		if(tot % i == 0)
    		{
    			s[cnt] = i;
    			cnt++;
    		}
    	}
    	
    	printf("%d
    ",--cnt);
    	
    	for(i = 1; i <= cnt; i++)
    	{
    		if(i != cnt)
    		printf("%d ",s[i]);
    		else
    		printf("%d",s[i]);
    	}
    	
    	return 0;
    }
    

    5-2

    分文件写法:

    student.h

    #ifndef STUDENT_H
    #define STUDENT_H
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Student
    {
    	private:
    		int height;
    		int weight;
    	public:
    		string name;
    		void creat();
    		void cmp(string cmpname,int h,int w);
    		void Print();
    };
    
    #endif // STUDENT_H
    
    

    main.cpp

    #include "student.h" // class's header file
    #include <string>
    #include <iostream>
    #include <stdlib.h>
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    
    Student stu[1000005];
    
    void Student::creat()
    {
    	if(height > 0)return ;
    	
    	height = 0;
    	weight = 0;
    }
    
    void Student::cmp(string cmpname,int h,int w)
    {
    	if(h > height)
    	{
    		height = h;
    		weight = w;
    		name = cmpname;
    	}
    }
    
    void Student::Print()
    {
    	cout << name << " " << height << " "<< weight << endl;
    }
    
    int turn[1000005];
    
    int main()
    {
    	int i,j;
    	int n;
    	cin >> n;
    	int a,b,c;
    	string name;
    	
    	for(i = 1; i <= n; i++)
    	{
    		cin >> a;
    		
    		turn[i] = a;
    		
    		stu[a].creat();
    		
    		cin >> name >> b >> c;
    		
    		stu[a].cmp(name,b,c);
    	}
    	
    	sort(turn+1,turn+n+1);
    	
    	int cnt = 0;
    	turn[0] = -1;
    	
    	for(i = 1; i <= n; i++)
    	{
    		if(turn[i] != turn[cnt])
    		{
    			cnt = i;
    			printf("%06d ",turn[i]);
    			stu[turn[i]].Print();
    		}
    	}
    	
    	return 0;
    }
    //比较懒,就直接把类中函数定义和主函数放一起了= = 
    

    单文件写法:

    #include <string>
    #include <iostream>
    #include <stdlib.h>
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    
    class Student
    {
    	private:
    		int height;
    		int weight;
    	public:
    		string name;
    		void creat();
    		void cmp(string cmpname,int h,int w);
    		void Print();
    };
    
    Student stu[1000005];
    
    void Student::creat()
    {
    	if(height > 0)return ;
    	
    	height = 0;
    	weight = 0;
    }
    
    void Student::cmp(string cmpname,int h,int w)
    {
    	if(h > height)
    	{
    		height = h;
    		weight = w;
    		name = cmpname;
    	}
    }
    
    void Student::Print()
    {
    	cout << name << " " << height << " "<< weight << endl;
    }
    
    int turn[1000005];
    
    int main()
    {
    	int i,j;
    	int n;
    	cin >> n;
    	int a,b,c;
    	string name;
    	
    	for(i = 1; i <= n; i++)
    	{
    		cin >> a;
    		
    		turn[i] = a;
    		
    		stu[a].creat();
    		
    		cin >> name >> b >> c;
    		
    		stu[a].cmp(name,b,c);
    	}
    	
    	sort(turn+1,turn+n+1);
    	
    	int cnt = 0;
    	turn[0] = -1;
    	
    	for(i = 1; i <= n; i++)
    	{
    		if(turn[i] != turn[cnt])
    		{
    			cnt = i;
    			printf("%06d ",turn[i]);
    			stu[turn[i]].Print();
    		}
    	}
    	
    	return 0;
    }
    
    

    5-2 纯sort写法

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <stdlib.h>
    #include <string>
    using namespace std;
    
    class P
    {
    	private:
    		int height;
    		int weight;
    	public:
    		string name;
    		int norm;
    		int display();
    		void play();
    		void f(int nor,string nam,int h,int w);
    };
    
    void P::play()
    {
    	cout << " " << height << " " << weight << endl;
    }
    
    int P::display()
    {
    	int a;
    	a = height;
    	return a;
    }
    
    void P::f(int nor,string nam,int h,int w)
    {
    	norm = nor;
    	name = nam;
    	height = h;
    	weight = w;
    }
    
    P p[105];
    
    bool cmp(P p1,P p2)
    {
    	if(p1.norm != p2.norm)return p1.norm > p2.norm;
    	else if(p1.display() != p2.display())return p1.display() < p2.display();
    }
    
    int main()
    {
    	int n;
    	string nam;
    	int i,j;
    	int w,h;
    	int nor;
    	
    	scanf("%d",&n);
    	
    	for(i = 1; i <= n; i++)
    	{
    		cin >> nor >> nam >> h >> w;
    		p[i].f(nor,nam,h,w);
    	}
    	
    	sort(p+1,p+n+1,cmp);
    	
    	int tot = -1;
    	
    	for(i = n; i >= 1; i--)
    	{
    		if(p[i].norm != tot)
    		{
    			tot = p[i].norm;
    			
    			printf("%06d ",p[i].norm);
    			
    			cout << p[i].name;
    			
    			p[i].play();
    		}
    	}
    	
    	
    	return 0;
    }
    
    

    5-1

    分文件写法:

    main.cpp

    #include "date.h"
    #include <iostream>
    #include <stdlib.h>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
    	int year,mouth,day;
    	int i,j;
    	while(scanf("%d%d%d",&year,&mouth,&day) != EOF)
    	{
    		bool flag = true;
    		Date D;
    		
    		D.fun1();
    		D.fun2(year,mouth,day);
    		
    		flag = D.is_leap();
    		D.display(flag);
    	}
    	return 0;
    }
    

    date.h

    #ifndef DATE_H
    #define DATE_H
    #include <iostream>
    using namespace std;
    
    class Date
    {
    	private :
    		int year;
    		int day;
    		int mouth;
    	public :
    		void fun1();
    		void fun2(int a,int b,int c);
    		void display(bool isleap);
    		bool is_leap();
    	//protected :
    };
    
    #endif // DATE_H
    
    

    date.cpp

    #include "date.h" // class's header file
    #include <iostream>
    #include <stdlib.h>
    #include <cstdio>
    using namespace std;
    
    int leap_year[15]={0,31,29,31,30,31,30,31,31,30,31,30,31};
    int nomal_year[15]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    
    void Date::fun1()
    {
    	year = 0;
    	mouth = 0;
    	day = 0;
    }
    
    void Date::fun2(int a,int b,int c)
    {
    	year = a;
    	mouth = b;
    	day = c;
    }
    
    void Date::display(bool isleap)
    {
    	int i,j;
    	int tot = 0;
    	if(isleap)
    	{
    		for(i = 1; i <= mouth-1; i++)
    		{
    			tot += leap_year[i];
    		}
    		
    		tot += day;
    		
    		printf("%d
    ",tot);
    	}
    	else
    	{
    		for(i = 1; i <= mouth-1; i++)
    		{
    			tot += nomal_year[i];
    		}
    		
    		tot += day;
    		
    		printf("%d
    ",tot);
    	} 
    }
    
    bool Date::is_leap()
    {
    	if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
    	{
    		return true;
    	}
    	else return false;
    }
    

    单文件写法:

    #include <iostream>
    #include <stdlib.h>
    #include <cstdio>
    using namespace std;
    
    int leap_year[15]={0,31,29,31,30,31,30,31,31,30,31,30,31};
    int nomal_year[15]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    
    class Date
    {
    	private :
    		int year;
    		int day;
    		int mouth;
    	public :
    		void fun1();
    		void fun2(int a,int b,int c);
    		void display(bool isleap);
    		bool is_leap();
    	//protected :
    };
    
    void Date::fun1()
    {
    	year = 0;
    	mouth = 0;
    	day = 0;
    }
    
    void Date::fun2(int a,int b,int c)
    {
    	year = a;
    	mouth = b;
    	day = c;
    }
    
    void Date::display(bool isleap)
    {
    	int i,j;
    	int tot = 0;
    	if(isleap)
    	{
    		for(i = 1; i <= mouth-1; i++)
    		{
    			tot += leap_year[i];
    		}
    		
    		tot += day;
    		
    		printf("%d
    ",tot);
    	}
    	else
    	{
    		for(i = 1; i <= mouth-1; i++)
    		{
    			tot += nomal_year[i];
    		}
    		
    		tot += day;
    		
    		printf("%d
    ",tot);
    	} 
    }
    
    bool Date::is_leap()
    {
    	if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
    	{
    		return true;
    	}
    	else return false;
    }
    
    int main()
    {
    	int year,mouth,day;
    	int i,j;
    	while(scanf("%d%d%d",&year,&mouth,&day) != EOF)
    	{
    		if(year == 0 || mouth == 0 || day == 0)break;
    		 
    		bool flag = true;
    		Date D;
    		
    		D.fun1();
    		D.fun2(year,mouth,day);
    		
    		flag = D.is_leap();
    		D.display(flag);
    	}
    	return 0;
    }
    
  • 相关阅读:
    link rel=”canonical”标签
    overflow:hidden导致元素高度增加的问题
    python读取字节中的位
    chrome extension 扩展开发中 popup 页面卡顿问题
    chrome extension打开新窗口
    禁止enter回车键出发页面的onsubmit事件
    ArgumentError("subject table for an INSERT, UPDATE or DELETE expected, got Column('id', Integer(), table=<user>, primary_key=True, nullable=False)."
    urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)>
    vite-v3-ts-0到0.8
    执行shell脚本无法访问 logs/catalina.out^M: 没有那个文件或目录
  • 原文地址:https://www.cnblogs.com/qq952693358/p/5520850.html
Copyright © 2011-2022 走看看