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;
}
#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;
}