纪念第一次随机生成数据对拍成功
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
![](https://img-blog.csdn.net/20180325113151728)
题目: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的用法:大佬链接
对拍程序的写法:大佬总结的很棒了
这里我使用绝对路径来寻找相应的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的用法:大佬链接
相关文章推荐
- php生成随机数据(数字、大小写字母混合)
- 随机生成数据的三种方法
- 快速生成10亿随机不重复数据代码(转)
- 如何使用Visual Studio 2010在数据库中生成随机测试数据
- mysql 生成随机时间 mysql 统计数量并根据最后面一个数据的时间排序
- Python随机生成数据后插入到PostgreSQL
- 随机生成数据 文件数据的输入输出
- 常见的随机数据生成方法
- 【转】Visual Studio 2010在数据库生成随机测数据
- Python:随机生成测试数据的模块--faker的基本使用
- 机器学习算法的随机数据生成
- 使用 Elizabeth 为应用生成随机样本数据
- C#随机获得字符串组成帐号和密码并插入到数据库,将插入成功的数据计入文本
- Vue+Express+Mock(Web后端随机生成数据)独立于服务端开发
- java生成随机大数据文件
- RandomUser – 生成随机用户 JSON 数据的 API
- 通过正则表达式生成满足条件的随机数据
- Excel 随机生成数据
- java生成随机大数据文件
- MOCK.JS 生成随机数据,拦截 Ajax 请求