从八点起来做了一个上午,两道题没做出来,后面也没研究,晚上回来rank滑到了第九 = =。简单写一下解题报告吧。
Problem A
A+B
没啥说的..
#include <stdio.h>
int main()
{
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
printf("%d\n",a+b);
}
return 0;
}
Problem B
猴子爬楼梯 (I)
扩展欧几里得算法,求出是否存在整数解.
//B
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
/*int k_gcd(int n, int m, int &x, int &y)
{
int x1 = 1, x2 = 0, x3 = n;
int y1 = 0, y2 = 1, y3 = m;
while (x3 % y3 != 0)
{
//puts("ok");
int d = n / m;
int t1, t2, t3;
t1 = x1 - d * y1; t2 = x2 - d * y2; t3 = x3 - d * y3;
x1 = y1; x2 = y2; x3 = y3;
y1 = t1; y2 = t2; y3 = t3;
}
x = y1; y = y2;
return y3;
}*/
int k_gcd(int n, int m, int &x, int &y)
{
if (m == 0)
{
x = 1; y = 0; return n;
}
int g = k_gcd(m, n % m, x, y);
int t = x - n / m * y;
x = y;
y = t;
return g;
}
int gcd(int a, int b) {
int r = a % b;
while (r) {
a = b;
b = r;
r = a % b;
}
return b;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
//puts("fd");
int u,d,num;
scanf("%d%d%d",&u,&d,&num);
if(u==0&&d==0)
{
printf("NO\n");
continue;
}
else if(u==0)
{
printf("NO\n");
continue;
}
else if(d==0)
{
if(num%u==0)
printf("YES\n");
else
printf("NO\n");
continue;
}
d=-d;
int i,j,flag=0;
int x,y;
k_gcd(u,d,x,y);
//printf("%d %d\n",x,y);
int g=gcd(u,d);
if(num%g==0)
{
if((x*num)%g==0 && (y*num)%g==0)
flag=1;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
}
Problem C
猴子爬楼梯 (II)
//不会做,等题解.
广度优先搜索,唉,被B题限制思维了...代码是非递归方法
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int u,d,num,top,vis[1010];
queue<int> q;
void q_empty()
{
while(!q.empty())
q.pop();
}
bool bfs()
{
q.push(0);
while(!q.empty())
{
int tmp=q.front();
q.pop();
if(!vis[tmp])
{
vis[tmp]=1;
if(tmp-d>=0)
{
if(tmp-d==num)
return true;
q.push(tmp-d);
}
if(tmp+u<=top)
{
if(tmp+u==num)
return true;
q.push(tmp+u);
}
}
}
return false;
}
int main()
{
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
q_empty();
memset(vis,0,sizeof(vis));
scanf("%d%d%d%d",&u,&d,&num,&top);
if(bfs())
puts("YES");
else
puts("NO");
}
return 0;
}
Problem D
统计学号
统计不同数字的个数,没什么难度,逐个比较。
#include <stdio.h>
#include <stdlib.h>
int a[100000001];
int comp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
int main()
{
int T,n,i,j,count;
scanf("%d",&T);
while(T--)
{
count=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",a+i);
qsort(a,n,sizeof(int),comp);
for(i=0;i<n-1;i++)
{
j=i+1;
if(a[i]!=a[j])
count++;
}
printf("%d\n",count+1);
}
return 0;
}
Problem E
德德的嗜好2.0
//名字很无语,WA了十一次的代码就不发了。。
看了题解,在编比较函数的时候比较a,b的两次凭借,如果a+b大于b+a就让a到b的前面...
好拙计...
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
string str[80];
bool cmp(string a,string b)
{
if(a+b>b+a)
return true;
else
return false;
}
int main()
{
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
cin>>str[i];
sort(str,str+n,cmp);
for(int i=0;i<n;i++)
cout<<str[i];
cout<<endl;
}
return 0;
}
Problem F
德德的嗜好3.0
找规律的题,刚开始想到了素数表,后来发现不对。把前8个打出来,num=k*60+r (k为循环次数,r是指向第几个)
#include <stdio.h>
int main()
{
long long all,T,n,k,r;
scanf("%lld",&T);
int a[]={0,7,11,19,23,31,43,47,59};
while(T--)
{
scanf("%lld",&n);
if(n%8==0)
k=n/8-1;
else
k=n/8;
r=n-k*8;
all=k*60+a[r];
printf("%lld\n",all);
}
return 0;
}
Problem G
不知道自己不知道
这个...c上机难度题...
#include <stdio.h>
int main()
{
int N;
while(scanf("%d",&N)!=EOF)
{
switch(N)
{
case 1: printf("2012\n"); break;
case 2: printf("2011\n"); break;
case 3: printf("2010\n"); break;
default: printf("2009\n");
}
}
}
Problem H
知道自己不知道
同上...
#include <stdio.h>
int main()
{
int a[101];
int T,n,m,i,all;
scanf("%d",&T);
while(T--)
{
all=0;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",a+i);
for(i=0;i<n;i++)
all+=a[i];
if(all%m==0)
printf("%d\n",all/m);
else
printf("%d\n",all/m+1);
}
return 0;
}
Problem I
不知道自己知道
字符串比较..用strcmp就好了..
#include <stdio.h>
#include <string.h>
char a[100][500];
char b[100][500];
int main()
{
int T,n,q,i,count=1,j;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",a[i]);
scanf("%d",&q);
for(i=0;i<q;i++)
scanf("%s",b[i]);
printf("Case #%d:\n",count++);
for(i=0;i<q;i++)
for(j=0;j<n;j++)
{
if(strcmp(a[j],b[i])==0)
{
printf("Yes\n");
break;
}
if(j==n-1)
printf("No\n");
}
}
return 0;
}
Problem J
知道自己知道
WA了好多次,没看见负数,改的代码不太好,大数据肯定会超时..
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int num[110],fnum[110];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(num,0,sizeof(num));
memset(fnum,0,sizeof(fnum));
int n;
scanf("%d",&n);
int i,j;
for(i=0;i<n;i++)
{
int tmp;
scanf("%d",&tmp);
if(tmp>=0)
num[tmp]++;
else
{
tmp=-tmp;
fnum[tmp]++;
}
}
int max=0;
for(i=0;i<110;i++)
{
max=(max>num[i])?max:num[i];
}
for(i=0;i<110;i++)
{
max=(max>fnum[i])?max:fnum[i];
}
int cnt=0;
for(i=0;i<110;i++)
if(num[i]!=0)
cnt++;
for(i=0;i<100;i++)
if(fnum[i]!=0)
cnt++;
printf("%d",cnt);
for(i=max;i>=1;i--)
{
for(j=110;j>=0;j--)
{
if(fnum[j]==i)
printf(" -%d",j);
}
for(j=0;j<110;j++)
{
if(num[j]==i)
printf(" %d",j);
}
}
printf("\n");
}
return 0;
}
嗯,就这么多吧,估计明天的题解报告肯定会短。C++的代码是我编的,ANSI C是舍友编的(他应该进校队的!)。