您的位置:首页 > 其它

纪念第一次随机生成数据对拍成功

2018-03-25 11:37 351 查看
第一次随机生成数据进行对拍成功,贼开心,在此记录一下。
对拍程序的写法:大佬总结的很棒了
这里我使用绝对路径来寻找相应的exe文件
@echo off
:loop
F:\CodeBlocks\Code\rand\bin\Debug\rand.exe %random% > in.txt
F:\CodeBlocks\Code\yuan\bin\Debug\yuan.exe < in.txt > stdout.txt
F:\CodeBlocks\Code\zhi\bin\Debug\zhi.exe < in.txt > myout.txt
fc stdout.txt myout.txt
if not errorlevel 1 goto loop
pause
goto loop


题目:pat 关于堆的判断

数据的处理比较麻烦,事实上我就是在处理字符串时出错了。

错误代码#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>

using namespace std;

c
4000
onst int N = 1010*2;
const int INF = 0x3ffffff;

void HeapBuild(int a[],int s,int n)
{
int re = a[s];
for(int i = 2*s; i <= n; i *= 2){
if(i+1 <= n && a[i+1] < a[i]) i++;
if(re <= a[i]) break;
a[s] = a[i];
s = i;
}
a[s] = re;
}

void HeapInit(int a[],int n)
{
for(int i = n/2; i > 0; --i)
HeapBuild(a,i,n);
}

int find(int a[],int root,int t)
{
if(a[root] == t) return root;
if(a[root*2] != INF){
int p = find(a,root*2,t);
if(p) return p;
}
if(a[root*2+1] != INF){
int p = find(a,root*2+1,t);
if(p) return p;
}
return 0;
}

int data
;
char st
;

int main()
{
//freopen("F:\\CodeBlocks\Code\in.txt","r",stdin);
//freopen("F:\\CodeBlocks\Code\my.txt","w",stdout);
fill(data,data+N,INF);
int n,m;
cin >> n >> m;
for(int i = 1; i <= n; ++i){
cin >> data[i];
HeapInit(data,i);
}
getchar();
for(int i = 0; i < m; ++i){
gets(st);
if(st[strlen(st)-1] == 't'){
int root;
sscanf(st,"%d",&root);
if(root == data[1]) printf("T\n");
else printf("F\n");
}else if(st[strlen(st)-1] == 's'){
int a,b;
sscanf(st,"%d%*s%d",&a,&b);
a = find(data,1,a);
b = find(data,1,b);
if(a>>1 == b>>1) printf("T\n");
else printf("F\n");
}else{
char p[30];
int a,b;
sscanf(st,"%d%[^0-9]%d",&a,p,&b);
a = find(data,1,a);
b = find(data,1,b);
if(p[4] == 't'){
if(a == b>>1) printf("T\n");
else printf("F\n");
}else{
if(a>>1 == b) printf("T\n");
else printf("F\n");
}
}
}
//system("pause");
return 0;
}
正确代码#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>

using namespace std;

const int N = 1010*2;
const int INF = 0x3ffffff;

void HeapBuild(int a[],int s,int n)
{
int re = a[s];
for(int i = 2*s; i <= n; i *= 2){
if(i+1 <= n && a[i+1] < a[i]) i++;
if(re <= a[i]) break;
a[s] = a[i];
s = i;
}
a[s] = re;
}

void HeapInit(int a[],int n)
{
for(int i = n/2; i > 0; --i)
HeapBuild(a,i,n);
}

int find(int a[],int root,int t)
{
if(a[root] == t) return root;
if(a[root*2] != INF){
int p = find(a,root*2,t);
if(p) return p;
}
if(a[root*2+1] != INF){
int p = find(a,root*2+1,t);
if(p) return p;
}
return 0;
}

int data
;
char st
;

int main()
{
//freopen("F:\\CodeBlocks\Code\in.txt","r",stdin);
//freopen("F:\\CodeBlocks\Code\my.txt","w",stdout);
fill(data,data+N,INF);
int n,m;
cin >> n >> m;
for(int i = 1; i <= n; ++i){
cin >> data[i];
HeapInit(data,i);
}
getchar();
for(int i = 0; i < m; ++i){
gets(st);
if(st[strlen(st)-1] == 't'){
int root;
sscanf(st,"%d",&root);
if(root == data[1]) printf("T\n");
else printf("F\n");
}else if(st[strlen(st)-1] == 's'){
int a,b;
sscanf(st,"%d%*s%d",&a,&b);
a = find(data,1,a);
b = find(data,1,b);
if(a>>1 == b>>1) printf("T\n");
else printf("F\n");
}else{
char p[30];
int a,b;
sscanf(st,"%d%[^0-9,^-]%d",&a,p,&b);
a = find(data,1,a);
b = find(data,1,b);
if(p[4] == 't'){
if(a == b>>1) printf("T\n");
else printf("F\n");
}else{
if(a>>1 == b) printf("T\n");
else printf("F\n");
}
}
}
//system("pause");
return 0;
}
数据生成器:#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <sstream>
#include <set>
#include <vector>

using namespace std;

stringstream ss;

set<int>st;
vector<int>vec;

int main(int argc, char *argv[])
{
int seed = time(NULL);
if(argc > 1){
ss.clear();
ss<<argv[1];
ss>>seed;//把参数转换成整数赋值给seed
}
srand(seed);
int n,m;
n = rand()%1000;
m = rand()%20;
cout << n << " " << m << endl;
while(1){
if(st.size() == n) break;
st.insert(rand()%20000-10000);
}
set<int>::iterator it;
for(it = st.begin(); it != st.end(); ++it){
vec.push_back(*it);
}
cout << vec[0];
for(int i = 1; i < n; ++i)
cout << " " << vec[i];
cout << endl;

int a,b,t;
for(int i = 0; i < m; ++i){
t = rand()%4;
a = rand()%n;
b = rand()%n;
switch(t){
case 0:{
printf("%d is the root\n",vec[a]);
};break;

case 1:{
printf("%d and %d are siblings\n",vec[a],vec[b]);
};break;

case 2:{
printf("%d is the parent of %d\n",vec[a],vec[b]);
};break;

case 3:{
printf("%d is a child of %d\n",vec[a],vec[b]);
}break;
}
}
//system("pause");
return 0;
}

错因:
使用sscanf处理数据时,因为数据可能为负数,所以把负号也读到字符串里面去了。
不过sscanf真的挺好用的。
sscanf的用法:大佬链接


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: