2008年 浙工大考研计算机专业课试题C++

2008年 浙工大考研计算机专业课试题C++

个人闲暇之余整理,可能会有许多细节问题且题目实现代码不唯一,若有需要可邮件与我交流。


一,改程序错误

1-1,计算半径为(a+b)的圆的面积

// kaoshi.cpp : 定义控制台应用程序的入口点。

//2008-1-1

#include "stdafx.h"

#include <iostream>

#include <stdio.h>

#define PI 3.1415926

double S(double r)

{

       doublearea=PI*r*r;

       returnarea;

};

//define S(r)PI*r*r

void main() //main()

{

       doublea,b,area;

       a=3.6,b=4.4;

       area=S(a+b);

       printf("%f",area);

}

结果:

201.061926

1-2,用循环打印1到20的整数,要求每行只打印5个整数

#include "stdafx.h"

#include <iostream>

using namespace std;

int main(int argc, char* argv[])

{

       inti,k;

       for(i=1;i<=20;i++)

       {

              cout<<i;

              if(i%5==0)

              {

                     cout<<endl;

              }

       }

}

1-3,计算N!

#include "stdafx.h"

#include <iostream>

using namespace std;

double f(double i)

{

if(i==0||i==1) return 1;

else return i*f(i-1);

}

void main()

{

       doubleN;

       cout<<"请输入一个整数N:";

       cin>>N;

       cout<<N<<"!的值是:"<<f(N)<<endl;

}

1-4,删除字符串尾部的空格符、制表符和换行符

// shijuan.cpp : Defines the entry pointfor the console application.

//

#include "stdafx.h"

#include <iostream>

using namespace std;

int trim(char s[])

{

int n;

for (n = strlen(s)-1; n >= 0; n--)

//从s[0] ...s[strlen(n) - 1] 因为要求删除字符串尾部的空格符、制表符与换行符

//也就是从字符串尾部开始判断若果是 空格符、制表符与换行符中的一个n--否则 跳出循环此时记录了最后一个不是空格符、制表符与换行符的字符的位置

// n 我们只需要在原来的字符串的后面加上一个 '\0'结束符 就可以删除后面的空格符、制表符与换行符了

if (s[n] != ' ' && s[n] != '\t'&& s[n] != '\n')

break;

s[n+1] = '\0';

return n;

}

void main()

{

       charc[80];

       cout<<"请任意输入一些字符串:";

       cin.getline(c,80);        //从键盘读入一行字符

       cout<<"您输入的字符串是:";

       for(inti=0;c[i]!=0;i++)      //对字符逐个处理,直到遇′/0′为止

              cout<<c[i];

       cout<<endl;

       intk=trim(c);

       cout<<"删除字符串尾部空格符、制表符、换行符后是:";

       for(k=0;c[k]!=0;k++)      //对字符逐个处理,直到遇′/0′为止

       cout<<c[k];

}

二,读程序写结果

// kaoshi.cpp : 定义控制台应用程序的入口点。

//2008-2-1

#include "stdafx.h"

#include <iostream>

#include <stdio.h>

using namespace std;

int d=2;

int fun2(int a,int b)

{

       staticint c=3;

       c=a*b%3+c;

       printf("thec is%d\n",c);

       returnc;

}

int fun1(int a,int b)

{

       intc=3;

       a+=a;b+=b;

       {

              intd;

              d=4;

       }

       c=fun2(a,b)+d;

       d++;

       printf("thed is%d\n",d);

       returnc*c;

}

 voidmain()

{

       inti,a,b;

       a=2;b=6;

       for(i=1;i<3;i++)

       printf("%d\n",fun1(a,b));

}

结果:

the c is3

the d is3

25

the c is3

the d is4

36

// kaoshi.cpp : 定义控制台应用程序的入口点。

//2008-2-2

#include "stdafx.h"

#include <iostream>

#include <stdio.h>

using namespace std;

void main()

{

       intb[2][3]={2,4,6,8,12};

       int*a[2][3]={*b,*b+1,*b+2,*(b+1)+2,*(b+1)+1,*(b+1)};

       int**q,k;

       q=a[0];

       for(k=1;k<6;k++)

       {

              printf("%4d\n",**q);

              q++;

       }

      

}

结果:

   2

   4

   6

   0

  12

三,编程

3-1,编写一个程序,实现将多个文本文件连接成一个指定的文本文件cat.txt。要求使用命令行参数,参数将被解释为需要连接的文件名,并按照顺序逐个进行处理。

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

#include <string>

#include <iomanip>

#include <fstream>

using namespace std;

//将 in1-3.txt 的内容连接到 cat.txt 的末尾

int main()

{

 ifstream ifile1,ifile2,ifile3;

 string s1,s2,s3;

 cout<<"文件1的完整路径:";

 cin>>s1;cout<<endl;

 cout<<"文件2的完整路径:";

 cin>>s2;cout<<endl;

 cout<<"文件3的完整路径:";

 cin>>s3;cout<<endl;

 ifile1.open(s1);

 ifile2.open(s2);

 ifile3.open(s3);

  /*

 ifile1.open("c://in1.txt");

 ifile2.open("c://in2.txt");

 ifile3.open("c://in3.txt");

*/

 ofstream ofile;

 ofile.open("c://cat.txt",ofstream::app);

 string str;

 while(ifile1>>str)ofile<<str<<endl;

 while(ifile2>>str)ofile<<str<<endl;

 while(ifile3>>str)ofile<<str<<endl;

 ifile1.close();

 ifile2.close();

 ifile3.close();

 ofile.close();

 return 0;

}

3-2,编写一个程序,统计从键盘输入的字符串中出现的“auto”“break”“case”三个单词的次数。

3-3, 每行10个,输出1-500之间的所有素数。(20分)

1. 根据概念判断:

如果一个正整数只有两个因子,1和p,则称p为素数.

代码:

bool isPrime(int n)

{

   if(n < 2) return false;

   for(int i = 2; i < n; ++i)

        if(n%i == 0) return false;

   return true;

}

时间复杂度O(n).

2. 改进, 去掉偶数的判断

代码:

bool isPrime(int n)

{

   if(n < 2) return false;

   if(n == 2) return true;

   for(int i = 3; i < n; i += 2)

        if(n%i == 0) return false;

   return true;

}

时间复杂度O(n/2), 速度提高一倍.

3. 进一步减少判断的范围

定理: 如果n不是素数, 则n有满足1<d<=sqrt(n)的一个因子d.

证明: 如果n不是素数, 则由定义n有一个因子d满足1<d<n.

如果d大于sqrt(n), 则n/d是满足1<n/d<=sqrt(n)的一个因子.

代码:

bool isPrime(int n)

{

   if(n < 2) return false;

   if(n == 2) return true;

   for(int i = 3; i*i <= n; i += 2)

       if(n%i == 0) return false;

   return true;

}

时间复杂度O(sqrt(n)/2),速度提高O((n-sqrt(n))/2).

#include "stdafx.h"

#include <iostream>

using namespace std;

#include<math.h>

main()

{

int i,j,k;

int a[101];

for(i=1;i<101;i++)

{

a[i]=i;

}

for(j=2;j<101;j++)

{

for(k=2;k<sqrt(j)+1;k++)

{

if(a[j]%k==0&&j!=2)

{

a[j]=0;

}

}

if(a[j]!=0)

printf("%d\n",a[j]);

}

}

3-4,输入50个整数,其中有重复数,输出其中重复次数最多的那个数。(20分)

// kaoyan.cpp : Defines the entry point forthe console application.

#include "StdAfx.h"

#include "stdlib.h "

#include <iostream>

#include <string>

#include <iomanip>

#include <fstream>

using namespace std;

#define NUM 50

int main(void)

{

 // 一次过可输入50个数字

 intdata[NUM], n = 0;

 inti, j, k;

 inttimes[NUM], visited[NUM], max;

 while (cin.peek() != '\n')

 {

  cin>> data[n];

 ++n;

  if(n == NUM)

  break;

 }

 for(i = 0; i < n; ++i)

 {

 visited[i] = 0;

 times[i] = 1;

 }

 for(i = 0; i < n; ++i)

 {

  if(!visited[i])

  {

  for (j = i + 1; j < n; ++j)

   {

   if ((data[i] == data[j]) && !visited[j])

    {

    visited[j] = 1;

    ++times[i];

    }

   }

  }

 

 visited[i] = 1;

 }

 max= times[0];

 k =0;

 for(i = 1; i < n; ++i)

 {

  if(times[i] > max)

  {

  max = times[i];

   k= i;

  }

 }

 cout<< data[k] << endl;

 return 0;

}

undoner

LSOFT.CN (


原文地址:https://www.cnblogs.com/wuyida/p/6301037.html