xtu contest summary
湘潭邀请赛 代码与解题报告
这次第一做出来的的四道题全是水题~~ 在切过之后才发现~~囧~~~
还需要多切切这样的水题
网上练习的地址
http://acm.hunnu.edu.cn/online/?action=problem&type=list
湖南师范大学 acm网站 注册个号进去~~
在search 一栏打上 湘潭
即会出现这个页面
用gnu C++写吧~~我用gnuC 提交就出错了~~~
第一题
分金块
开始打这样的一个表 分的是 1,2,4,8.,16,32----------
“和”是 1 3 7,15,31,63------
对应的切的项是 1 2 3 4 5 6
举一个例子
Case1例如 14
14在‘和’中是位于7 和 15 之间的位置 即遍历一遍减去7,这时已经切了3块了,剩下的长度为7,这样就不用切了,在加上剩下的“7”这一块,结果是3+1=4
分的长度为 1,2,4,7 前三项可以组成1到7的任何值,加上后一项可以组成1到14的任何值
Case2 如果等于15 直接对应4
只要给的结果大于等于最少分的值就可以了~~~
Post code
|
|||
|
|||
|
|
第二题
三角形
锐角,钝角,直角的判断
利用a的平方 b的平方 c的平方 其中任意 两值之和和另一值进行比较得出结论
也就是利用高中在三角形中求cos 的值 ,仅看分母就可以了
Post code
#include<stdio.h>
int main()
{
int n,i,flag1,flag2,flag3;
int a[3];
scanf("%d",&n);
while(n--)
{
flag1=flag2=flag3=0;
scanf("%d %d %d",&a[0],&a[1],&a[2]);
for(i=0;i<=2;i++)
{
if(a[i%3]+a[(i+1)%3]<=a[(i+2)%3]){break;}
if(a[i%3]*a[i%3]+a[(i+1)%3]*a[(i+1)%3]<a[(i+2)%3]*a[(i+2)%3])flag1=1;
if(a[i%3]*a[i%3]+a[(i+1)%3]*a[(i+1)%3]==a[(i+2)%3]*a[(i+2)%3])flag2=2;
if(a[i%3]*a[i%3]+a[(i+1)%3]*a[(i+1)%3]>a[(i+2)%3]*a[(i+2)%3])flag3++;
}
if(i<3)printf("NO\n");
else{
if(flag2==2)printf("Right triangle\n");
if(flag3==3)printf("Acute triangle\n");
if(flag1==1)printf("Obtuse triangle\n");
}
}
}
第三题
Hambuerg
这道题十分的简单 认真读题 就可以了 数据量较少 注意匹配的顺序
#include<stdio.h>
int main()
{
int a[110],n,x,i,j,k,sum=0;
scanf("%d",&x);
while(x--)
{
scanf("%d",&n);
scanf("%d",&a[1]);
n=n-1;
i=2;
sum=0;
while(n--)
{
scanf("%d",&a[i]); // 从头开始匹配
for(j=1;j<i;j++)
if(a[j]==a[i]){sum++;i=j-1;}
i++;
}
printf("%d\n",sum);
}
}
直接匹配
第四题
Eculid
这就是求 最大公约数 和最小公倍数
我开始也犯了同样的错误
在子函数中
Gcd(a,b)
{
Int temp;
If(a<b){temp=a;a=b;b=temp;} // 交换
While(a%b)
{
Temp=a; // 这是关键之处 如果没有这一步,下一步会影响下下步的结果
a=b;
b=temp%b;
}
}
和错误的方法进行比较
Gcd(a,b)
{
Int temp;
If(a<b){temp=a;a=b;b=temp;} // 交换
While(a%b)
{
a=b;
b=a%b; // 错误,这是a的值改变了!!
}
}
会产生分母为0的情况
被程序报错
Post code
#include<stdio.h>
int gcd(int mother,int son);
int main()
{
int mother1,son1,mother2,son2,mother,son,divide,n;
scanf("%d",&n);
while(n--)
{
scanf("%d",&son1);
getchar();
scanf("%d",&mother1);
divide=gcd(mother1,son1);
mother1/=divide;
son1/=divide; //将输入的第一个数的分子分母化简
scanf("%d",&son2);
getchar();
scanf("%d",&mother2);
divide=gcd(mother2,son2);
mother2/=divide;
son2/=divide; //将输入的第二个数的分子分母化简
mother=mother1*mother2/gcd(mother1,mother2); //分母为公倍数
son=gcd(son1,son2); //分子公约数
divide=gcd(mother,son);
printf("%d/%d\n",son/divide,mother/divide); //分子分母化简
}
}
int gcd(int mother,int son) //求最大公约数
{
int temp;
if(mother<son){temp=mother;mother=son;son=temp;}
while(mother%son!=0)
{ temp=mother;
mother=son;
son=temp%son;
}
return son;
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
在遇到错误的时候,利用输出 来观察结果
看看是哪里出错了
这是误我开始调试的源代码
多个输出--------------------
#include<stdio.h>
int gcd(int mother,int son);
int main()
{
//freopen( "E.in","r",stdin );
//freopen( "test.out","w",stdout );
int mother1,son1,mother2,son2,mother,son,divide,n;
scanf("%d",&n);
while(n--)
{
scanf("%d",&son1);
getchar();
scanf("%d",&mother1);
//printf("%d\n",n);
divide=gcd(mother1,son1);
mother1/=divide;
son1/=divide;
// printf("divide=%d\n",divide);
// printf("son1=%d\n",son1);printf("mother1=%d\n",mother1);
scanf("%d",&son2);
getchar();
scanf("%d",&mother2);
divide=gcd(mother2,son2);
mother2/=divide;
son2/=divide;
// printf("son2=%d\n",son2);
// printf("mother2=%d\n",mother2);
// printf("divide=%d\n",divide);
mother=mother1*mother2/gcd(mother1,mother2);
//printf("mother1=%d mother2=%d gcd=%d",mother1,mother2,gcd(mother1,mother2));
son=gcd(son1,son2);
divide=gcd(mother,son);
// printf("son=%d mother=%d divide=%d\n",son,mother,divide);
printf("%d/%d\n",son/divide,mother/divide);
}
}
int gcd(int mother,int son)
{
int temp;
if(mother<son){temp=mother;mother=son;son=temp;}
// printf("%d %d\n",mother,son);
while(mother%son!=0)
{ temp=mother;
mother=son;
son=temp%son;
//printf("%d %d\n",mother,son);
}
return son;}
有一句话和大家共勉
搞acm的 每天写点代码是必须的(哪怕是书本上有的代码);
搞acm的 每天学点英语是必须的(哪怕是文档中用的词汇);
搞acm的 每天回头复习是必须的(哪怕是只看笔记的标题);
搞acm的 每天出点错误是必须的(哪怕是自己写错的字母);
搞acm的 每天逛逛论坛是必须的(哪怕是做一个潜水的主)。