牛客第一期

2023-12-16 19:45:30

1.创建动态数组

#include <iostream>
using namespace std;

int main() {

int n;
cin>>n;
int *p=new int [n];
int i=0;
for(i=0;i<n;i++)
{
    *(p+i)=n+i;
}
int j=0;
for(j=0;j<n;j++)
{
    printf("%d ",*(p+j));
}
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
int **p=new int*[n];
int i=0;
for(i=0;i<n;i++)
{
    p[i]=new int [n];
}
int j=0;
int k=0;
for(j=0;j<n;j++)
{
    for(k=0;k<n;k++)
    {
        p[j][k]=j+k;
    }
}
int z=0;
int x=0;
for(z=0;z<n;z++)
{
    for(x=0;x<n;x++)
    {
        printf("%d ",p[z][x] );
    }
    printf("\n");
}
delete []p;
    return 0;
}

#include <iostream>
#include <cstring>
#pragma warning(disable : 4996)
using namespace std;

class Person {

    public:
        char* name; // 姓名
        int age;    // 年龄

        Person(const char* name, int age) {
            this->name = new char[strlen(name) + 1];
            strcpy(this->name, name);
            this->age = age;
        }

        // write your code here......
        Person(const Person &p)
        {
        age=p.age;
        name=new char[100];
        strcpy(name,p.name);
        }
        

        void showPerson() {
            cout << name << " " << age << endl;
        }

        ~Person() {
            if (name != nullptr) {
                delete[] name;
                name = nullptr;
            }
        }

};

int main() {

    char name[100] = { 0 };
    int age;

    cin >> name;
    cin >> age;

    Person p1(name, age);
    Person p2 = p1;

    p2.showPerson();

    return 0;
}

#include<bits/stdc++.h>
#include<cstring>
using namespace std;
class Array{
	private:
		int n;//数组大小 
		int *a;//数组 
	public:
		Array(){
			cin>>n;
			a=new int [n];
			for (int i=0;i<n;i++) cin>>a[i];
		}
	
		~Array(){
			delete []a;
		}
		int getlen(){
			return n;
		}
		int get(int i){
			return a[i];
		}
		// write your code here......
        
			Array( Array &p)
		{ n=p.getlen();
			a=new int[n];
			int j=0;
			for(j=0;j<n;j++)
			{
				a[j]=p.get(j);
			}
		}
		void show(){
			for (int i=0;i<n;i++) cout<<a[i]<<' ';
		}
	
};
int main(){
	Array a;
	Array b=a; 
	b.show();
	return 0;
}

#include <iostream>
using namespace std;

class Person {
    // write your code here......
    friend void showAge(Person &a);

    public:
        Person(int age) {
            this->age = age;
        }

    private:
        int age;
};

 void showAge(Person& p) {
    cout << p.age << endl;
}

int main() {

    Person p(10);
    showAge(p);

    return 0;
}

文章来源:https://blog.csdn.net/whcwhc111111/article/details/135034168
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。