练习:2888:字符串中的数字,2723:不吉利日期,2746:约瑟夫问题,6378:删除数组中的元素(链表)

2888:字符串中的数字:输入一个字符串,将其中是数字的字符提取出来,对它们进行从小到大排序

#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
#include<ctype.h>
#include<algorithm>
using namespace std;
int main()
{
	int i;
	string s;
	vector<int>ans;
	while(getline(cin,s))
	{
		ans.clear();
		for (i=0;i<s.length();i++)
			if (isdigit(s[i]))
				ans.push_back(s[i]-'0');
		sort(ans.begin(),ans.end());
		for (i=0;i<ans.size();i++)
			cout<<setw(4)<<ans[i];
		cout<<endl;
	}
	return 0;
}

2723:不吉利日期:已知某年的一月一日是星期w,并且这一年一定不是闰年,求出这一年所有13号那天是星期5的月份

思路:1月1号是星期m,则先求出当前日期和1月1号之间的天数(c+13-1),再往后数a个,则为当前星期数

#include<iostream>
using namespace std;
int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int tol[13];
int main()
{
	int a,i,c=0;
	cin>>a;
	for (i=1;i<=12;i++)
	{
		c+=mon[i-1];
		if ((c+13+a-1)%7==5)
			cout<<i<<endl;
	}
	return 0;
}

2746:约瑟夫问题:有n只猴子(编号从1到n),从第1号开始报数,一直数到m,数到m的猴子退出圈外

#include<iostream>
using namespace std;
int n,m;
struct node
{
	int v;
	struct node *next;
};
void solve()
{
	int i,count=1;
	struct node *head,*p,*q,*bef;
	head=new node;p=new node;head->next=p;bef=head;
	for (i=1;i<=n;i++)
	{
		p->v=i;
		q=new node;
		p->next=q;
		bef=p;
		p=q;
	}
	bef->next=head->next;
	p=head->next;bef=head;
	while(n>1)
	{
		if (count%m==0)
		{
			n--;
			bef->next=p->next;
			free(p);
			p=bef->next;
		}
		else
		{
			bef=p;p=p->next;
		}
		count++;
	}
	cout<<p->v<<endl;
}
int main()
{
	while(cin>>n>>m)
	{
		if (n==0 && m==0)
			break;
		solve();
	}
	return 0;
}

6378:删除数组中的元素(链表)

#include<iostream>
using namespace std;
struct node
{
	int v;struct node *next;
};
int main()
{
	node *head,*p,*q,*bef;
	int n,i,a;
	cin>>n;
	head=new node;p=new node;head->next=p;
	for (i=0;i<n;i++)
	{
		cin>>p->v;
		q=new node;
		p->next=q;
		p=q;
	}
	p->next=NULL;
	cin>>a;
	p=head->next;bef=head;
	while(p->next!=NULL)
	{
		if (p->v==a)
		{
			bef->next=p->next;
			free(p);
			p=bef->next;
		}
		else
		{
			bef=p;p=p->next;
		}
	}
	p=head->next;
	while(p->next!=NULL)
	{
		cout<<p->v<<" ";
		p=p->next;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Always_ease/article/details/82526837