首页 > 其他分享 >打卡6

打卡6

时间:2023-04-28 21:34:51浏览次数:33  
标签:cout int void ++ using 打卡 include

代码验证

#include<iostream>
using namespace std;
template <class T>
T sum(T* array, int size = 0)
{
	T total = 0;
	for (int i = 0; i < size; i++)
		total += array[i];
	return total;
}

template <class T1, class T2>
T2 sum(T1* array1, T2* array2, int size = 0)
{
	T2 total = 0;
	for (int i = 0; i < size; i++)
		total += array1[i] + array2[i];
	return total;
}

char* sum(char* s1, char* s2)
{
	char* str = new char[strlen(s1) + strlen(s2)];
	strcpy(str, s1);
	return strcat(str, s2);
}
void main()
{
	int iArr[] = { 1,2,3,4,5 };
	double dArr[] = { 1.1,2.2,3.3,4.4,5.5 };
	char* p1 = "Hello,";
	char* p2 = "World";
	int iTotal = sum(iArr, 5);
	double dTotal = sum(dArr, 5);
	double idTotal = sum(iArr, dArr, 5);
	p1 = sum(p1, p2);
	cout << iTotal << endl;
	cout << dTotal << endl;
	cout << idTotal << endl;
	cout << p1 << endl;
}


#include<iostream>
using namespace std;
template <class T,int n>
class mysequence {
	T memblock[N];
public:
	void setmember(int x, T value)
	{
		memblock[x] = value;

	}
	T getmember(int x) {
		return memblock[x];
	}
};

int main()
{
	mysequence<int, 5>myints;
	mysequence<double, 5>myfloats;
	myints.setmember(0, 100);
	myfloats.setmember(3, 3.1416);
	cout << myints.getmember(0) << endl;
	cout << myfloats.getmember(3) << endl;
	return 0;
}

课本习题
//9-1
#include<iostream>
using namespace std;
template<class T>
class arry
{
	int  n;
	T score;
public:
	arry(){}
	arry(int m):n(m){}
	T sum()
	{
		T ans = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> score;
			ans += score;
		}
	return ans;
	}
	
};
int main()
{
	int n;
	cout << "请输入人数:" << endl;
	cin >> n;
	arry<float> s(n);
	cout << "输入依次输入每个人的成绩:";
	cout << "该课程的平均成绩为:" << s.sum() / n << endl;
}


//9-5
不会
//9-6
不会


//9-10
#include<iostream>
using namespace std;
void sort(int a[],int l,int r)
{
	if (l >= r)return;
	int i = l - 1, j = r + 1, x = a[l + r >> 1];
	while (i < j)
	{
		do i++; while (a[i] < x);
		do j--; while (a[j] > x);
		if (i < j)swap(a[i], a[j]);
	}
	sort(a, l, j);
	sort(a, j + 1, r);
}
int main()
{
	int data1[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
	sort(data1, 0, 19);
	for (int i = 0; i < 20; i++)
	{
		cout << data1[i] << " ";
	}
	return 0;
}

//10-3
#include <iostream>
#include<vector>
using namespace std;

int main()
{
	vector<int>s;
	for (int i = 0; i < 5; i++)
	{
		s.push_back(i);
		cout<<"第"<<i << "次添加后的容量: " << s.capacity() << endl;
	}
	return 0;
}


//9-12
#include<iostream>
using namespace std;
void sort(int a[],int n)
{
	for (int i = 0; i < n; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			if (a[i] > a[j])
			{
				swap(a[i], a[j]);
			}
		}
	}
		
}
int main()
{
	int data1[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
	sort(data1, 20);
	for (int i = 0; i < 20; i++)
	{
		cout << data1[i] << " ";
	}
	return 0;
}

//9-10
#include<iostream>
using namespace std;
void print(int a[], int n);
void sort(int a[],int n)
{
	for (int i = 0; i < n; i++)
	{
		print(a, 20);
		for (int j = 0; j < n-i; j++)
		{
			if (a[j] < a[j+1])
			{
				swap(a[j], a[j+1]);
			}
		}
	}
		
}
void print(int a[],int n)
{
	for (int i = 0; i < 20; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}
int main()
{
	int data1[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
	sort(data1, 20);
	cout << "最终结果:" << endl;
	print(data1, 20);
	return 0;
}


//9-17
#include<iostream>
using namespace std;
void print(int a[], int n);
int sort(int a[],int l,int r,int x)
{
	
	while (l < r)
	{
		int i = 0, j = r;
		int mid =l + r >> 1;
		if (a[mid] >= x) r = mid;
		else l = mid + 1;
	}
	return r;
}
void print(int a[],int n)
{
	for (int i = 0; i < 20; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}
int main()
{
	int data1[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
	cout<<sort(data1,0,19,5);
	//cout << "最终结果:" << endl;
	//print(data1, 20);
	return 0;
}

//10-4
#include <iostream>
#include<vector>
#include<list>
#include<typeinfo>
#include<ctime>
using namespace std;
template<class T>
void jp(T colect, int n, int m)
{
	if (n < 1 || m < 1) {
		cout << "错误的问题假设" << endl;
		return;
	}
	for (int i = 1; i <= n; i++)
	{
		colect.push_back(i);
	}
	class T::iterator iter=colect.begin(), del;
		int counter=1;
		while (colect.size() > 1)
		{
			while (counter % m == 0 && colect.size() > 1) {
				counter = 1;
					if (typeid(colect) != typeid(list<int>))
						iter = colect.erase(iter);
					else {
						del = iter;
						iter++;
						colect.erase(del);
					}
				if (iter == colect.end())
					iter = colect.begin();
			}
			counter++;
			iter++;
			if (iter == colect.end())
				iter = colect.begin();
		}
	//finish = colock();
	cout << "最后剩余人的编号为" << *iter << endl;
	cout << "使用容器" << typeid(colect).name() << endl;
}
int main()
{
	vector<int>v;
	jp(v, 100000, 5);
	
	return 0;
}



//10-5
template<class T>
void exchange(list<T>& l1, class list<T>::iterator p1, list<T>& l2, class list<T>::iterator p2)
{
	list<T>temp;
	temp.splice(temp.begin(), l1, p1, l1.end());
	l1.splice(l1.end(), l2, p2, l2.end());
	l2.splice(l2.end(), temp, temp.begin(), temp.end());
}

//10-7
#include <iostream>
#include<vector>
#include<list>
#include<typeinfo>
#include<ctime>
#include<queue>
#include<stack>
using namespace std;

int main()
{
	int a[] = { 5,4,1,6 };
	stack<int>s;
	queue<int>q;
	priority_queue<int>q1;
	cout << "stack" << endl;
	for (int i = 0; i < 4; i++) {
		s.push(a[i]);
	}
	if (!s.empty()) {
		cout << "第一次pop,取出:" << s.top() << endl;
		s.pop();
	}
	if (!s.empty()) {
		cout << "第二次pop,取出:" << s.top() << endl;
		s.pop();
	}
	if (!s.empty()) {
		cout << "第三次pop,取出:" << s.top() << endl;
		s.pop();
	}
	if (!s.empty()) {
		cout << "第四次pop,取出:" << s.top() << endl;
		s.pop();
	}


	cout << "queue" << endl;
	for (int i = 0; i < 4; i++) {
		q.push(a[i]);
	}
	if (!q.empty()) {
		cout << "第一次queue,取出:" << q.front() << endl;
		q.pop();
	}
	if (!q.empty()) {
		cout << "第二次queue,取出:" << q.front() << endl;
		q.pop();
	}
	if (!q.empty()) {
		cout << "第三次queue,取出:" << q.front() << endl;
		q.pop();
	}
	if (!q.empty()) {
		cout << "第四次queue,取出:" << q.front() << endl;
		q.pop();
	}


	cout << "priority_queue" << endl;
	for (int i = 0; i < 4; i++) {
		q1.push(a[i]);
	}
	
	if (!q1.empty()) {
		cout << "第一次 aurityqueue,取出:" << q1.top() << endl;
		q1.pop();
	}
	if (!q1.empty()) {
		cout << "第二次 aurityqueue,取出:" << q1.top() << endl;
		q1.pop();
	}
	if (!q1.empty()) {
		cout << "第三次 aurityqueue,取出:" << q1.top() << endl;
		q1.pop();
	}
	if (!q1.empty()) {
		cout << "第四次 aurityqueue,取出:" << q1.top() << endl;
		q1.pop();
	}
	
	return 0;
}


//10-8
#include <iostream>
#include<vector>
#include<list>
#include<typeinfo>
#include<ctime>
#include<queue>
#include<stack>
#include<set>
using namespace std;

int main()
{
	string str;
	multiset<string>strset;
	while (1)
	{
		cout << "请输入字符串" << endl;
		cin >> str;
		if (str == "QUIT")
		{
			break;
		}
		int counter = strset.count(str);
		if (counter > 0) {
			cout << str << "出现了" << counter << "次" << endl;
		}
		else {
			cout << str << "在集合中没有出现过" << endl;
		}
		strset.insert(str);
	}
	return 0;
}

标签:cout,int,void,++,using,打卡,include
From: https://www.cnblogs.com/gyg1222/p/17363201.html

相关文章

  • 4.28打卡
    #include<iostream>#include<iomanip>#include<cmath>usingnamespacestd;intfun2(intm){returnm*m;}intfun1(intx,inty){returnfun2(x)+fun2(y);}intmain(){inta,b;cout<<"pleaseentertwointeger......
  • 打卡15
    3.2亲密数 思路很简单,先遍历1-3000,把因子算一下,加一下,然后再算和的质因数和,看是否相等即可,时间复杂度不是很高 流程也很简单#include<bits/stdc++.h>usingnamespacestd;intf(intx)//求一个数的因子和{ intsum=0; for(inti=1;i<=x/2;i++) { if(x%i==0)sum+=i; } re......
  • c++打卡练习(19)
    1.问题描述相传国际象棋是古印度舍罕王的宰相达依尔发明的。舍罕王十分喜爱象棋,决定让宰相自己选择何种赏赐。这位聪明的宰相指着8x8共64格的象棋棋盘说:陛下,请您赏给我一些麦子吧。就在棋盘的第1格中放1粒,第2格放2粒,第3格放4粒,以后每一格都比前一格增加一倍,依此放完棋盘上64格,我......
  • 打卡 C++类与对象定义一个日期类 N天以后 - C/C++ 操作符重载
    改造练习13-1(日复一日)中的Date类并提交,使其可以与一个整数n相加或相减,得到该日期N天后/前的日期。提示:请参考题目(日复一日)中的Date类实现;注意考虑闰月;整数n的取值范围为[1,10000]。裁判测试程序样例: #include<iostream>#include<string>#include<assert.h>usingn......
  • 第十二天打卡
    问题: 算法设计:根据问题可以计算出每个颜色球的取值范围红球为M<3,白球为N<3,黑球为8-M-N<6流程图: 源代码:#include<stdio.h>intmain(){ intm,n,number=0; for(m=0;m<=3;m++) for(n=0;n<=3;n++) if(8-m-n<=6) printf("%2d:%d%d%d\n",++number,m,n,8-m-n); return0;......
  • 第十一天打卡
    一、问题描述编写一个计算个人所得税的程序,要求输入收入金额后,能够输出应缴的个人所得税。二、设计思路1.利用结构体记录不同起始征税点、结束点和税率;2.利用函数计算各个阶段的税;3.输出结果。三、四、代码实现 ......
  • 打卡10
    位反序数设N是一个四位数,它的9倍恰好是其反序数,求N。反序数就是将整数的数字倒过来形成的整数。例如:1234的反序数是4321。#include<stdio.h>intmain(){ inti; for(i=1002;i<1111;i++)/*穷举四位数可能的值*/ if(i%10*1000+i/10%10*100+i/100%10*10+i/1000==i*9)/*判断反序数......
  • 打卡11
    一辆以固定速度行驶的汽车,司机在上午10点看到里程表上的读数是一个对称数(即这个数从左向右读和从右向左读是完全一样的),为95859。两小时后里程表上出现了一个新的对称数。问该车的速度是多少?新的对称数是多少?#include<stdio.h>intmain(){ intt,a[5];/*数组a存放分解的数字位*......
  • 每日打卡-15
    一.问题描述该铁路经过N个城市,每个城市都有一个站。不过,由于各个城市之间不能协调好,于是乘车每经过两个相邻的城市之间(方向不限),必须单独购买这一小段的车票。第i段铁路连接了城市i和城市i+1(1<=i<N)。如果搭乘的比较远,需要购买多张车票。第i段铁路购买纸质单程票需要Ai博艾元。虽......
  • 打卡
    #include<iostream>#include<string>usingnamespacestd;intmain(){stringa[10]={"ling","yi","er","san","si","wu","liu","qi","ba",&qu......