A题:转换序列,a[i]=abs(a[i]-a[i+1]),a[n]=abs(a[n]-a[1]),使得每一个数都想等的步骤,大与1000的按要求输出
代码:
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int a[25];
int main()
{
int n,cas=0;
while(scanf("%d",&n),n)
{
cas++;
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
int k=0;
while(1)
{
int j=2;
for(; j<=n; j++)
{
if(a[j]!=a[1])break;
}
if(j>n)//相同的话跳出
break;
int t=a[1];
for(int i=1; i<n; i++)
{
a[i]=abs(a[i]-a[i+1]);
}
a[n]=abs(a[n]-t);
k++;
if(k>1000)break;
}
// cout<<k<<endl;
printf("Case %d: ",cas);
if(k<=1000)
printf("%d iterations\n",k);
else
printf("not attained\n");
}
return 0;
}
B题题意:给出n个股票的价格,k1,k2,前股票价格前k1小的是哪些天,从小到大输出,前k2大的是哪些天,从大到小输出
直接结构体排序
代码:
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int M=1000005;
int n,s,e;
struct node
{
int value;
int day;
}a[M];
int s1[M];
bool cmp(node a,node b)
{
if(a.value==b.value)
return a.day<b.day;
return a.value<b.value;
}
bool cmp2(int a,int b)
{
return a > b;
}
int main()
{
int cas=0;
while(scanf("%d%d%d",&n,&s,&e)!=EOF)
{
if(!n && !s && !e)break;
cas++;
memset(s1,0,sizeof(s1));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].value);
a[i].day=i;
}
printf("Case %d\n",cas);
sort(a+1,a+n+1,cmp);//从小到大
for(int i=1;i<=s;i++)
s1[i]=a[i].day;
sort(s1+1,s1+s+1);
for(int i=1;i<=s-1;i++)
printf("%d ",s1[i]);
printf("%d\n",s1[s]);
int j=1;
for(int i=n;i>=n-e+1;i--)
s1[j++]=a[i].day;
sort(s1+1,s1+e+1,cmp2);
for(int i=1;i<=e-1;i++)
printf("%d ",s1[i]);
printf("%d\n",s1[e]);
}
return 0;
}
G题:G比较简单,但是赛中一直出错,以后还是学会多debug,这是赛后A的,就是一个判断条件弄错了,搞得我头晕了
直接模拟即可
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
const int M=1100;
char a[M];
map<string,int>mp;
int main()
{
int n,m,num,len,cas=0,i,j;
while(scanf("%d%d",&n,&m),n|m)
{
cas++;
getchar();
mp.clear();
num=0;
printf("Case %d\n",cas);
while(n--)
{
gets(a);
int l=strlen(a);
string s1="",s2="";
for(i=0; i<l; i++)
{
if(a[i]>='A' && a[i]<='Z')
{
s1+=(a[i]+32);
break;
}
else if(a[i]>='a' && a[i]<='z')
{
s1+=a[i];
break;
}
}
for(i=l-1; i>=0; i--)
{
if(a[i]==' ')
break;
}
for(int k=i+1; k<l; k++)
{
if(a[k]>='A' && a[k]<='Z')
s2+=(a[k]+32);
else if(a[k]>='a' && a[k]<='z')
s2+=a[k];
}
/*for(int k=i+1; k<l; k++)
{
if(a[k]>='A' && a[k]<='Z')
{
s2+=(a[k]+32);
len++;
if(len>=m)break;//错在这个条件了,其实不需要,原来是先存在map里,大于m的输出再截掉
}
else if(a[k]>='a' && a[k]<='z')
{
s2+=a[k];
len++;
if(len>=m)break;
}
}*/
s1+=s2;
len=s1.length();
if(!mp[s1])
{
mp[s1]++;
if(len>m)
{
for(int kk=0; kk<m; kk++)
printf("%c",s1[kk]);
cout<<endl;
}
else
cout<<s1<<endl;
}
else
{
mp[s1]++;
num=mp[s1]-1;
if(num<10)
{
if(len+1<=m)
{
cout<<s1<<num<<endl;
}
else
{
for(int kk=0; kk<m-1; kk++)
printf("%c",s1[kk]);
cout<<num<<endl;
}
}
else
{
if(len+2<=m)
{
cout<<s1<<num<<endl;
}
else
{
char g1=(num%10+'0');
char g2=(num/10+'0');
s1[m-2]=g2;
s1[m-1]=g1;
for(int kk=0; kk<m; kk++)
printf("%c",s1[kk]);
cout<<endl;
}
}
}
}
}
return 0;
}
/*
2 6
Jenny Ax
Christos H Papadimitriou
11 8
Jean-Marie d'Arboux
Jean-Marie A d'Arboux
Jean-Marie B d'Arboux
Jean-Marie C d'Arboux
Jean-Marie D d'Arboux
Jean-Marie D d'Arboux
Jean-Marie F d'Arboux
Jean-Marie G d'Arboux
Jean-Marie H d'Arboux
Jean-Marie I d'Arboux
Jean-Marie J d'Arboux
11 9
Jean-Marie d'Arboux
Jean-Marie A d'Arboux
Jean-Marie B d'Arboux
Jean-Marie C d'Arboux
Jean-Marie D d'Arboux
Jean-Marie D d'Arboux
Jean-Marie F d'Arboux
Jean-Marie G d'Arboux
Jean-Marie H d'Arboux
Jean-Marie I d'Arboux
Jean-Marie J d'Arboux
*/