您的位置:首页 > 其它

13-14年华师上机题

2020-04-05 07:25 134 查看
  1. 编写一个程序,给出方程3x+2y-7z=5(其中,0≤x,y,z≤10),求满足方程的所有x,y和z,输出之
#include<fstream>
#include <iostream>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
int x, y, z;
for(x=0;x<=10;x++)
for(y=0;y<=10;y++)
for (z = 0; z <= 10; z++)
{
if ((3 * x + 2 * y - 7 * z) == 5)
cout << "x=" << x << " y=" << y << " z=" <<z<< endl;
}
return 0;
}
  1. 编写函数,实现如下功能,键盘输入一个整数,判断每一个数字(0-9)在这个整数中重复出现的次数,输出重复出现的数字及其出现次数。比如:输入3321181,则输出3出现了2次、1出现了3次;输入2186,则输出没有重复数字。
#include<fstream>
#include <iostream>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
int n, a[10] = { 0 };
cin >> n;
while (n)
{
a[n % 10]++;
n = n / 10;
}
for (int i = 0; i < 10; i++)
if (a[i] > 1)
cout << i << " : " << a[i] << endl;
return 0;
}
  1. 类Person是一个描述人员信息的数据结构体,包括姓名(不定长)、性别、年龄。利用该结构体创建数组emp[5],调用自身的Get()方法可以输入人员的信息,并通过Show()方法显示输入的信息。请编写程序完成上述功能。
#include<fstream>
#include <iostream>
#include<algorithm>
#include<string>
using namespace std;

class Person{
private:
string name;
string sex;
int age;
public:
void Get() {
cout << "输入姓名 性别 年龄:" << endl;
cin >> name >> sex >> age;
}
void Show() {
cout << "输出姓名 性别 年龄:" << endl;
cout << name <<" "<< sex <<" "<< age << endl;
}
}emp[5];
int main()
{
for (int i = 0; i < 5; i++)
emp[i].Get();
for (int i = 0; i < 5; i++)
emp[i].Show();
return 0;
}

14年真题
4. 写一个求素数的算法。 20分

#include<fstream>
#include <iostream>
#include<algorithm>
#include<string>
using namespace std;

bool isprime(int n)
{
for (int i = 2; i <= sqrt(n); i++)
{
if (n%i == 0)
return false;
}
return true;
}

int main()
{
int n;
cin >> n;
if (isprime(n))
cout << n << "是素数" << endl;
else
cout << n << "不是素数" << endl;
return 0;
}
  1. 在一个有序数列中插入一个数,使得数列依然有序,并且把最大的那个数剔除出队列 比如 3,5,9,12, 插入6,然后得 3,5,6,9 20分
#include<fstream>
#include <iostream>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
int n, a[100];
cout << "输入数组个数:" << endl;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << "变为有序序列:" << endl;
sort(a, a + n);
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << endl;
cout << "输入要插入的数字:" << endl;
cin >> a[n];
sort(a, a + n + 1);
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}
  1. 输入一个任意数如451215,输出重复出现数字的次数,比如输出 5重复了2次 1重复了2次 20分
#include<fstream>
#include <iostream>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
int n, cnt = 0, a[10] = { 0 }, i = 0;
cin >> n;
while (n)
{
a[n % 10] ++;
n = n / 10;
cnt++;
}
for (int i = 0; i < 10; i++)
{
if (a[i] > 1)
cout << i << ": " << a[i] << endl;
}
return 0;
}
  1. 写一个类,类里面包含了私有成员变量和方法,并且用该类里面的方法给私有成员变量赋值和输出 40分
#include<fstream>
#include <iostream>
#include<algorithm>
#include<string>
using namespace std;

class Student {
private:
string num;
string name;
public:
void setData()
{
cout << "输入学生 学号 姓名:" << endl;
cin >> num >> name;
}
void getData()
{
cout << "输出学生 学号 姓名:" << endl;
cout << num << " " << name << endl;
}
};
int main()
{
Student s;
s.setData();
s.getData();
return 0;
}
  • 点赞
  • 收藏
  • 分享
  • 文章举报
一只很菜但是好学的猪 发布了38 篇原创文章 · 获赞 1 · 访问量 1549 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: