SDUT 对内测试。。

1001 http://acm.sdut.edu.cn/web/problem.php?action=showproblem&problemid=2389

卡精度的题目,第一次的WA后就想到了应该是卡精度的问题,可是以前没接触过eps怎么用,于是就纠结在了eps的考虑上,好不容易弄好了。可是还是WA结果我真的就死心了,于是就去做最小生成树的那个题(后来还是纠结了,,唉悲剧啊、。。)今天拿来,就是加了个tmp数组的出事化。。就A了。。唉。每次比赛时对于时间的把握,以及拿过题目来之后思路的整理,都还不够完善。。需要锻炼。

View Code
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 1007
#define eps 1e-6
using namespace std;
char str[maxn];
struct node
{
char name[25];
double val;
}p[55];
char tmp[55][25];
int n,m;
double find(int x)
{
for (int i = 0; i < n; ++i)
{
if (strcmp(tmp[x],p[i].name) == 0)
return p[i].val;
}
return 0;
}
bool isok(double a,double b,char *q)
{
if (strcmp(q,"=") == 0)
{
if (!(a - b > eps) && !(b - a > eps)) return true;
else return false;
}
else if (strcmp(q,">") == 0)
{
if (a - b > eps) return true;
else return false;
}
else if (strcmp(q,"<") == 0)
{
if (b - a >eps) return true;
else return false;
}
else if (strcmp(q,">=") == 0)
{
if (!(b - a > eps)) return true;
else return false;
}
else if (strcmp(q,"<=") == 0)
{
if (!(a - b > eps)) return true;
else return false;
}
}
int main()
{
int i,j,k;
while (~scanf("%d%d",&n,&m))
{
for (i = 0; i < n; ++i)
scanf("%s%lf",p[i].name,&p[i].val);
getchar();
for (i = 1; i <= m; ++i)
{
gets(str);
int len = strlen(str);
j = 0;
k = 0;
memset(tmp,0,sizeof(tmp));//即使这里的出始化让我纠结啊。。

while (1)//把名字抠出来,存放到tmp数组里面
{
int ct = 0;
while (str[j] != ' ')
{
tmp[k][ct++] = str[j];
j++;
}
j++; k++;
if (str[j] == '>' || str[j] == '<' || str[j] == '=')
break;
j += 2;
}
/*for (int o = 0; o < k; ++o)
{
printf("%s\n",tmp[o]);
}
*/
char f[3] = {'0'};
f[0] = str[j];
if (str[j + 1] == '=')
f[1] = str[j + 1];//存放符号
//printf("%s\n",f);
double sum = 0;
double count = 0;
for (int p1 = j; p1 < len; ++p1)
{
if (str[p1] >= '0' && str[p1] <= '9')
sum =sum *10.0 + (str[p1] - '0');
}
for (j = 0; j < k; ++j)
{
count += find(j);
}
//printf("%.2lf %.2lf\n",count,sum);
if (isok(count,sum,f))
{
printf("Guess #%d was correct.\n",i);
}
else
{
printf("Guess #%d was incorrect.\n",i);
}
}
}
return 0;
}

1003 http://acm.sdut.edu.cn/web/problem.php?action=showproblem&problemid=2391

这是一道很裸的求最小生成树的题目,因为之前做最小生成树题目时,就用过一次Kruscal算法,其他基本上是prim算法,肯定上来就用prim了,还有以前老是喜欢用临界矩阵存路的信息,这次因为点太多开一个map[][]二维数组不行,于是就用邻接表写了一下,结果一交tle...其实这也在意料之中的。当时对于Kruscal有点生疏了,就没写直接看着很多人A了1006就去看1006(结果看错题意悲剧了有。。总之这次的比赛很悲剧) 唉》》。学过的知识还是不扎实啊。。。。好好反省自己。。

prim算法的时间复杂度为O(n^2)适用于求边稠密的图,Kruscal算法的时间复杂度为O(e*lge)适合于求边比较稀疏的图。

View Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 200007
using namespace std;
char str[maxn];
struct node
{
int v,u;
int dis;
}tagp[maxn];
int n,m;
int f[maxn];
int ans;
int cmp(node a,node b)
{
return a.dis < b.dis;
}
int find(int x)
{
if (x != f[x])
f[x] = find(f[x]);
return f[x];
}
void Union(int x,int y)
{
int a = find(x);
int b = find(y);
if (a != b) f[b] = a;
}
void Kruscal()
{
int i;
ans = 0;
sort(tagp,tagp + m,cmp);
//for (i = 0; i < m; ++i)
//printf("%d %d %d\n",tagp[i].u,tagp[i].v,tagp[i].dis);
for (i = 0; i < m; ++i)
{
int x = find(tagp[i].u);
int y = find(tagp[i].v);
if (x != y)
{
ans += tagp[i].dis;
Union(x,y);
}
}
}
int main()
{
int i;
while (scanf("%d%d",&n,&m))
{
int sum = 0;
if (!n && !m) break;
for (i = 0; i < n; ++i)
f[i] = i;
for (i = 0; i < m; ++i)
{
scanf("%d%d%d",&tagp[i].u,&tagp[i].v,&tagp[i].dis);
sum += tagp[i].dis;
}
Kruscal();
//printf("%d %d\n",sum,ans);
printf("%d\n",sum - ans);
}
return 0;
}


1006 http://acm.sdut.edu.cn/web/showproblem.php?pid=2394&cid=1086

这道题,看错了题意,本来很水的一道题目,结果纠结了好久,这次比赛真是一次沉痛的打击啊。。心拔凉拔凉的。。。。

平方后直接分出数来,然后从第五个开始数。而我是对于分出来的数取中间四个,而忽略了加前导0.自己给想麻烦了。。。不够认真,一到了赛场就惊慌失措了。。

View Code
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 10
using namespace std;
int a[maxn];
int b[10000];
int len;
int isok(int x)
{
for (int i = 0; i < len; ++i)
{
if (x == b[i]) return 0;
}
b[len++] = x;
return 1;
}
int main()
{
int n,i,j;
while (~scanf("%d",&n))
{
if (!n) break;
memset(b,0,sizeof(b));

int count = 0;
len = 0;
while (isok(n))
{
count++;
n = n*n; i = 0;
memset(a,0,sizeof(a));//注意出事化
while (n!=0)
{
a[i++] = n%10;
n /= 10;
}
n = 0;
for (j = 5; j >= 2; --j)
{
n = n*10 + a[j];
}
}
printf("%d\n",count);
}
return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值