目录
L1-002 打印沙漏 (20 分)(模拟+注意输出格式问题)
L1-049 天梯赛座位分配(20 分)(模拟&&vector)
L1-056 猜数字 (20 分)(map--upper_bound)
L1-002 打印沙漏 (20 分)(模拟+注意输出格式问题)
【分析】
先预处理一下数组,存一下深度为k时共用符号数;然后获得整张图的size;
然后数学知识的线性规划,两条之间夹的部分输出字符;
注意,后面不要用空格补全....题目没说后面要用空格补全... 不然就只有4分了QAQ
【代码】
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+10;
int a[maxn];
char mp[maxn][maxn];
void init()
{
for(int i=1;i<=maxn;++i)//等差数列前n项和
a[i]=i*i;
for(int i=1;i<maxn;++i)//深度为i-1时用掉的符号数
a[i]=2*a[i]-1;
//for(int i=1;i<=10;++i)cout<<a[i]<<",";
}
int main()
{
init();
int n;char c;
scanf("%d %c",&n,&c);
int deep=upper_bound(a,a+maxn,n)-a;//二分查找~
deep--;
int wid=2*deep-1,x=0;
if(deep==0)wid=1;
for(int i=0;i<wid;++i)
{
for(int j=0;j<wid;++j)
{
if(i>=j && i>=-j+wid-1)printf("%c",c),x++;
else if(i<=j && i<=-j+wid-1)printf("%c",c),x++;
else if(i>j && i<-j+wid-1)printf(" ");
}
printf("\n");
}
printf("%d\n",n-x);
}
L1-020 帅到没朋友 (20 分)(并查集)
【分析】看完题就想用并查集做啦
并查集!
就是把连通的放在一个集团中~
然后 存邻接表中
然后 size判断存vector中~
【代码】
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int pre[maxn],vis[maxn];
vector<int>mp[maxn];
int findRoot(int x){ return x==pre[x]?x:pre[x]=findRoot(pre[x]);}
void join(int x,int y)
{
int fx=findRoot(x);
int fy=findRoot(y);
if(fx!=fy)
{
if(fx<fy)swap(fx,fy);
pre[fx]=fy;
}
mp[fy].push_back(fx);
}
int main()
{
int n;scanf("%d",&n);
memset(vis,0,sizeof(vis));
for(int i=0;i<maxn;++i)pre[i]=i;
while(n--)
{
int k;scanf("%d",&k);
int id;scanf("%d",&id);
k--;
while(k--)
{
int x;scanf("%d",&x);
join(id,x);
}
}
int m;scanf("%d",&m);
vector<int>v;
while(m--)
{
int x;scanf("%d",&x);
int fx=findRoot(x);
// cout<<"x="<<x<<",fx="<<fx<<",size="<<mp[fx].size()<<endl;
if(mp[fx].size()==0 &&!vis[x])v.push_back(x),vis[x]=1;
}
if(v.size()==0)puts("No one is handsome");
else{
int l=v.size();
for(int i=0;i<l;++i)
(i==l-1)?printf("%05d\n",v[i]):printf("%05d ",v[i]);
}
}
L1-025 正整数A+B (15 分)(细节注意)
【分析】注意细节!如果是0不行,有其他字符不行,有空格不行,大于1000也不行!
【代码】
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s1,s2;
cin>>s1;getchar();
getline(cin,s2);
int f1=1,f2=1;
int len1=s1.length(),len2=s2.length();
for(int i=0;i<len1;++i)
if(!isdigit(s1[i])){f1=0;break;}
for(int i=0;i<len2;++i)
if(!isdigit(s2[i])){f2=0;break;}
int x,y;
if(f1)x=stoi(s1);
if(f2)y=stoi(s2);
if(!x || x>1000)f1=0;if(!y || y>1000)f2=0;
if(f1 && f2)
printf("%d + %d = %d\n",x,y,x+y);
else if(f1 && !f2)
cout<<s1<<" + ? = ?\n";
else if(!f1 && f2)
cout<<"? + "<<s2<<" = ?\n";
else puts("? + ? = ?");
}
L1-027 出租 (20 分)(字符串模拟)
【代码】
#include<bits/stdc++.h>
using namespace std;
int vis[10];
int main()
{
string s;cin>>s;
int len=s.length();
memset(vis,0,sizeof(vis));
int cnt=0;
string str="";
for(int i=0;i<len;++i)
if(!vis[s[i]-'0'])vis[s[i]-'0']=1,str+=s[i],cnt++;
sort(str.begin(),str.end());
reverse(str.begin(),str.end());
printf("int[] arr = new int[]{");
for(int i=9;i>=0;--i)
{
if(!vis[i])continue;
if(cnt-1)printf("%d,",i),cnt--;
else printf("%d};\n",i);
}
printf("int[] index = new int[]{");
for(int i=0;i<len-1;++i)
{
int x=str.find(s[i]);
printf("%d,",x);
}
int x=str.find(s[len-1]);
printf("%d};\n",x);
}
L1-030 一帮一 (15 分)
【分析】把0/1和姓名放在pair里存进vector,然后遍历,输出过的erase掉;
【代码】
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,string> >v;
int main()
{
int n;scanf("%d",&n);
int cnt1=0,cnt2=n/2-1;
for(int i=0;i<n;++i)
{
int x;scanf("%d",&x);
string s;cin>>s;
v.push_back(make_pair(x,s));
}
for(int i=0;i<v.size();++i)
{
for(int j=v.size()-1;j>=0;--j)
{
if(v[i].first!=v[j].first)
{
cout<<v[i].second<<" "<<v[j].second<<endl;
v.erase(v.begin()+j);
break;
}
}
}
}
L1-043 阅览室 (20 分)(细节处理+思维)
【分析】
- 题上说线路问题... 然后就是只有当指令是还书并且有借阅记录的时候才进行阅读时间与人数的计算,并且将此id的借还时间置-1;其他情况均不进行计算;
- 平均阅读时间要四舍五入!!!不是简单的取整啥的...
-
floor :向下取整
ceil :向上取整
round:四舍五入取整
round(a,b) 把a精确到b位数
【代码】
#include<bits/stdc++.h>
using namespace std;
struct node{
int st=-1;
int ed=-1;
}a[1010];
int main()
{
int n;scanf("%d",&n);
int sum=0,cnt=0;
while(n)
{
int id,h,m;
string s;
scanf("%d",&id);cin>>s;
scanf("%d:%d",&h,&m);
if(id==0)
{
if(cnt==0)puts("0 0");
else
{
double x=sum*1.0/cnt;
x=round(x);
printf("%d %.0lf\n",cnt,x);
}
sum=0,cnt=0;
n--;
continue;
}
if(s[0]=='S')a[id].st=h*60+m;
else
{
if(a[id].st!=-1)cnt++,sum+=(h*60+m)-a[id].st,a[id].ed=a[id].st=-1;//计算完要重置为-1
}
}
}
L1-046 整除光棍 (20 分)(模拟除法运算)
【分析】模拟除法竖式的计算过程;首先先把除数的位数扩展到值比被除数大,然后进行计算,末尾不断加1直到能整除即可;
【代码】
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x;scanf("%d",&x);
int y=0,cnt=0;
while(y<x)y=y*10+1,cnt++;
while(1)
{
printf("%d",y/x);
y%=x;
if(y==0)break;
y=y*10+1;
cnt++;
}
printf(" %d\n",cnt);
}
L1-048 矩阵A乘以B (15 分)(矩阵乘法)
【分析】如果可以相乘,还要输出行列数!!!!!!!!!!!!
【代码】
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+10;
typedef long long ll;
int mp1[maxn][maxn],mp2[maxn][maxn];
ll mp[maxn*maxn];
int main()
{
int x1,y1,x2,y2;
scanf("%d%d",&x1,&y1);
for(int i=0;i<x1;++i)
for(int j=0;j<y1;++j)
scanf("%d",&mp1[i][j]);
scanf("%d%d",&x2,&y2);
for(int i=0;i<x2;++i)
for(int j=0;j<y2;++j)
scanf("%d",&mp2[i][j]);
if(y1!=x2)printf("Error: %d != %d\n",y1,x2);
else
{
int sum=0,cnt=0;
for(int k=0;k<x1;++k)
{
for(int i=0;i<y2;++i)
{
for(int t=0;t<y1;++t)
sum+=mp1[k][t]*mp2[t][i];
mp[cnt++]=sum;sum=0;
}
}
printf("%d %d\n",x1,y2);/////这里!!!!不要忘了输出!!!气死我了!!!哼!
for(int i=0;i<cnt;++i)
((i+1)%y2==0)?printf("%lld\n",mp[i]):printf("%lld ",mp[i]);
}
}
L1-056 猜数字 (20 分)(map--upper_bound)
【分析】n=1时特判一下,不然upper_bound失效
【代码】
#include<bits/stdc++.h>
using namespace std;
map<int,string>mp;
int main()
{
int n;scanf("%d",&n);
int sum=0;
for(int i=0;i<n;++i)
{
string s;cin>>s;
int x;scanf("%d",&x);
if(n==1)
{
cout<<x/2<<" "<<s<<endl;
return 0;
}
mp[x]=s;
sum+=x;
}
int y=sum/n/2;
map<int,string>::iterator it;
it=mp.upper_bound(y);
int a=abs(it->first-y);
it--;
int b=abs(it->first-y);
if(a>b)cout<<y<<" "<<it->second<<endl;
else it++,cout<<y<<" "<<it->second<<endl;
}