河大赛 赛前抱佛脚

写在赛前:这个比赛是文件存取操作的,不是像OJ和PAT一样提交代码通过测试用例。就这种文件存取的方式而言,有优点也有缺点,优点就是不会出现TLE啊。缺点就是无法提交代码去通过多个测试用例,如果有部分测试用例无法AC,可能出了考场还发现不了,就很难受。好了骚话少说,拿例题练练手,就本菜鸡自己输入用例进行调试而言,没有发现错误。

特别注意1:请在你使用的计算机 D: 盘建立一个以你学号为名的文件夹,将考试提供的“程序设计输入文件”文件夹中的文件都拷贝至该文件夹备用。最后提交时,将你的学号文件夹中的所有内容打包为一个以你学号命名的压缩文件(.rar)格式,然后提交该文件。

特别注意2:所有程序代码均需用注释说明求解原理或思路。

1、求解 mn之内所有的素数(mn由文件 input01.txt读入),并将结果输出到(output01.txt中,数字与数字由英文逗号“,”分割),请写出你自己认为最高效的运行代码,并在程序顶部注释说明。

我的源代码:写出自认为最高效的代码,经过亲测,我发现用优先队列比无脑for循环快那么一丢丢。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 bool isPrime(int n)   //判断一个数是否为素数
 5 {
 6     if(n<=1)
 7     {
 8         return false;
 9     }
10     else
11     {
12         for (int i = 2; i <= sqrt(n); i++)
13         {
14             if(n%i == 0)
15             {
16                 return false;
17             }
18         }
19         return true;
20     }
21 }
22 
23 int main()
24 {
25     ifstream txtfile;   //建立输入文件流对象
26     txtfile.open("d:\\代码\\20171101231\\input01.txt");
27     fstream myfile;    //建立文件流
28     myfile.open("d:\\代码\\20171101231\\output01.txt",ios::out|ios::trunc);
29     //打开目标文件,ios::out表示写入文件操作,ios::trunc当文件存在时,清空文件内容
30     int m,n;
31     while(!txtfile.eof())   //eof()用来侦测是否到达文件结尾,读到文件尾返回true
32     {
33         txtfile >> m >> n;    //读取文件中的数据
34     }
35     priority_queue<int,vector<int>,greater<int>> l;  //优先队列
36     for(int i = m; i <= n; i++)
37     {
38         if(isPrime(i))   
39         {
40             l.push(i);   //若这个数是在[m,n]这个区间上的素数,执行入队操作  
41         }
42     }
43     bool flag = true;   //为了输出格式而立的flag,只有第一个数才是true
44     while(!l.empty())   //清仓大甩卖,直到队列全输出完为止
45     {
46         if(flag)    //输出第一个在[m,n]这个范围内的素数
47         {
48             myfile << l.top();     //将队首的元素写入到目标文件中
49             l.pop();    //将队首元素删除
50             flag = false;
51         }
52         else    //除第一个外,其他在[m,n]这个范围内的素数
53         {
54             myfile << "," << l.top();
55             l.pop();
56         }
57     }
58     return 0;
59 }

2、有ABC三个盘子用来盛饼,饼的个头有大有小,没有大小完全相同的,饼在盘子中必须大个的在下面,小个的放在上面。现在 A 盘中放着 n 张薄饼(n由文件input02.txt读入),需要借助 B 盘放在 C 盘中,请写出程序代码实现,并在程序中注释说明原理,最后结果请输出在output02.txt中。 

我的源代码:这个题说白了就是汉诺塔问题,我觉得难点就在于直接在自定义函数中把结果输出到txt文件里面,要在函数中引用文件流对象。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 //看完题目之后,可以知道这是一个汉诺塔类问题
 4 int Count = 0;   //用来记录移动薄饼的总次数
 5 void move(fstream &myfile,char getone,char putone);
 6 void hanoit(fstream &myfile,int n,char a,char b,char c);
 7 
 8 int main()
 9 {
10     ifstream txtfile;      //建立输入文件流对象
11     txtfile.open("d:\\代码\\20171101231\\input02.txt");
12     fstream myfile;       //建立文件流
13     myfile.open("d:\\代码\\20171101231\\output02.txt",ios::out|ios::trunc);
14     //打开目标文件,ios::out表示写入文件操作,ios::trunc当文件存在时,清空文件内容
15     int n;
16     while(!txtfile.eof())    //eof()用来侦测是否读到文件尾,读到文件尾返回true
17     {
18         txtfile >> n;   //n张薄饼
19     }
20     hanoit(myfile,n,'A','B','C');
21     myfile << "一共移动了" << Count << "次薄饼。" << endl;
22     return 0;
23 }
24 
25 void move(fstream &myfile,char getone,char putone)
26 {
27     Count++;
28     myfile << getone << "-->" << putone << endl;   //输出是从哪一个盘移动到了哪一个盘
29 }
30 
31 void hanoit(fstream &myfile,int n,char a,char b,char c)
32 {
33     if(n == 1)   //若只有一张薄饼,直接从A盘移动到C盘
34     {
35         move(myfile,a,c);
36     }
37     else
38     {
39         hanoit(myfile,n-1,a,c,b);
40         move(myfile,a,c);
41         hanoit(myfile,n-1,b,a,c);
42     }
43 }

3因工作需要,需进行10进制和12进制的转换,请编写程序实现10进制到12进制的转换函数 decToDoz(n1, n2) 12进制到10进制的转换函数 dozToDec(n1, n2),只考虑整数转换。

我的源代码: 这题有点懵,我不晓得为啥转换函数有俩个形参,然后我就自定义函数用的是void型。写出来之后发现输出不对,就不上传代码来丢人了。

4、读取文件 input04.txt (文件中有10个人的英文名字),按名字长短从小到大排列。请在程序中用注释说明求解原理。 

我的源代码:这送分题啊,直接用vector排序就行了。要不是河大赛只能选一种语言,我肯定用Python写。刷PAT乙级题库就是出其不意来段Python几行AC。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define N 10   //文件中有10个人的英文名字
 4 
 5 bool Cmp(string s1,string s2) 
 6 {
 7     return s1.length() < s2.length();
 8 }
 9 
10 int main()
11 {
12     ifstream txtfile;   //建立文件输入流对象
13     txtfile.open("d:\\代码\\20171101231\\input04.txt");
14     fstream myfile;    //建立文件流
15     myfile.open("d:\\代码\\20171101231\\output04.txt",ios::out|ios::trunc);
16     //打开目标文件,ios::out表示写入文件操作,ios::trunc当文件存在时,清空文件内容
17     vector<string> v(N);
18     for (int i = 0; i < N; ++i)
19     {
20         txtfile >> v[i];
21     } 
22     sort(v.begin(),v.end(),Cmp);   //将这些名字按长短从小到大排列
23     for(auto it:v)
24     {
25         myfile << it << endl;
26     }
27     return 0;
28 }

5、有一架电梯,上升一层需要7秒,下降一层需要5秒,到达指定楼层时会停顿10秒用于开门关门。请编写程序,读取输入文件(input05.txt),计算并输出电梯共用时多少秒(假设电梯从0层出发)。

输入样例文件示例如:

1
5
2

输出文件内容为:

80

我的源代码:按照题目意思来就行了,这题就是个欺负理工男的文字游戏。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int getTime(int start,int end); //start为当前楼层,end为目标楼层,求start到end的运行时间
 5 
 6 int main()
 7 {
 8     ifstream txtfile;   //建立文件输入流对象
 9     txtfile.open("d:\\代码\\20171101231\\input05.txt");
10     fstream myfile;    //建立文件流
11     myfile.open("d:\\代码\\20171101231\\output05.txt",ios::out|ios::trunc);
12     //打开目标文件,ios::out表示写入文件操作,ios::trunc当文件存在时,清空文件内容
13     int start = 0,end;   //start为当前楼层,end为目标楼层,电梯从0层出发
14     int time = 0;  //电梯总用时
15     while(!txtfile.eof())  //eof()用来侦测是否到达文件结尾,读到文件尾返回true
16     {
17         txtfile >> end;
18         time += getTime(start,end);
19         start = end;
20     }
21     myfile << time; 
22     return 0;
23 }
24 
25 int getTime(int start,int end)  
26 {
27     int time = 0;
28     if(start > end)  //电梯下降
29     {
30        time += 5*(start-end);
31     }
32     else    //电梯上升
33     {
34        time += 7*(end-start);   
35     }
36     time += 10;   //到达指定楼层停10秒用于开门关门
37     return time;
38 }

刷完之后发现上面的全是程序设计例题,下面的才是去年的赛事真题。。。。。。。。。。分界线。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

1.本题目保存的源文件名应为:prog01.cpp 或 prog01.c 或 prog01.py (按使用编程工具确定),该文件应该保存在你的学号文件夹中用于提交。

已知 e 可以用公式 e = 1 + 1/1! + 1/2! + 1/3! + … + 1/(n-1)! + 1/n! 近似求得,请编写程序读取in01.txt中的数据作为 n 使用,近似求得 e 值,保留6位有效数字,并将此值写入 out01.txt 中。 

 我的源代码:水题,送分题吧。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     ifstream txtfile;   //建立输入文件流对象
 7     txtfile.open("d:\\代码\\20171101231\\in01.txt");
 8     fstream myfile;    //建立文件流
 9     myfile.open("d:\\代码\\20171101231\\out01.txt",ios::out|ios::trunc);
10     //打开目标文件,ios::out表示写入文件操作,ios::trunc当文件存在时,清空文件内容
11     int n;
12     while(!txtfile.eof())   //eof()用来侦测是否到达文件结尾,读到文件尾返回true
13     {
14         txtfile >> n;    //读取文件中的数据
15     }
16     double e=1, temp=1;
17     int i=1;
18     for(int i=1;i<=n;i++)
19     {
20         temp *= 1.0/i;
21         e += temp;
22     }
23     myfile << setiosflags(ios::fixed) << setprecision(6) << e << endl;
24     return 0;
25 }

2.本题目保存的源文件名应为:prog02.cpp 或 prog02.c 或 prog02.py (按使用编程工具确定),该文件应该保存在你的学号文件夹中用于提交。

合数有一些基本性质,包括: 

(1) 所有大于2的偶数都是合数

(2) 所有大于5的基数中,各位为5的都是合数

(3) 除0以外,所有个位为0的自然数都是合数

(4) 最小的偶合数为4,最小的奇合数为9

(5) 每一个合数都可以以唯一形式被写成质数的乘积,即分解质因数

primeNum.txt中提供10000以内的所有素数,数字间以逗号分割,请阅读并参考合数的某些性质,编写程序读取 in02.txt 中的两个数据,求解这两个数据之间的所有素数,并输出到 out02.txt 中,输出的数字间用英文逗号分隔。

我的源代码:其实读题直接看最后一句话就行了。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 bool isPrime(int n)   //判断一个数是否为素数
 5 {
 6     if(n<=1)
 7     {
 8         return false;
 9     }
10     else
11     {
12         for (int i = 2; i <= sqrt(n); i++)
13         {
14             if(n%i == 0)
15             {
16                 return false;
17             }
18         }
19         return true;
20     }
21 }
22 
23 int main()
24 {
25     ifstream txtfile;   //建立输入文件流对象
26     txtfile.open("d:\\代码\\20171101231\\in02.txt");
27     fstream myfile;    //建立文件流
28     myfile.open("d:\\代码\\20171101231\\out02.txt",ios::out|ios::trunc);
29     //打开目标文件,ios::out表示写入文件操作,ios::trunc当文件存在时,清空文件内容
30     int m,n;
31     while(!txtfile.eof())   //eof()用来侦测是否到达文件结尾,读到文件尾返回true
32     {
33         txtfile >> m >> n;    //读取文件中的数据
34     }
35     priority_queue<int,vector<int>,greater<int>> l;  //优先队列
36     for(int i = m; i <= n; i++)
37     {
38         if(isPrime(i))   
39         {
40             l.push(i);   //若这个数是在[m,n]这个区间上的素数,执行入队操作  
41         }
42     }
43     bool flag = true;   //为了输出格式而立的flag,只有第一个数才是true
44     while(!l.empty())   //清仓大甩卖,直到队列全输出完为止
45     {
46         if(flag)    //输出第一个在[m,n]这个范围内的素数
47         {
48             myfile << l.top();     //将队首的元素写入到目标文件中
49             l.pop();    //将队首元素删除
50             flag = false;
51         }
52         else    //除第一个外,其他在[m,n]这个范围内的素数
53         {
54             myfile << "," << l.top();
55             l.pop();
56         }
57     }
58     return 0;
59 }

猜你喜欢

转载自www.cnblogs.com/m17773572025/p/10047039.html