5.1求二维数组的鞍点
#include<iostream>
using namespace std;
int a[10][10];
int b[10],c[10];
int n,m;
int main(int argc, char const *argv[])
{
n = 3;
m = 4;
for(int i = 1; i <= n;++i)
for(int j = 1; j<= m;++j)cin>>a[i][j];
for(int i = 1; i <= n;++i){
int maxx = -1000;
int p = 0;
for(int j = 1; j <= m;++j){
if(a[i][j] > maxx){
maxx = a[i][j];
p = j;
}
}
b[i] = p;
}
for(int j = 1; j <= m; ++j){
int minn = 100000;
int p = 0;
for(int i = 1; i <= n ;++i){
if(minn > a[i][j]){
minn = a[i][j];
p = i;
}
}
c[j] = p;
}
int ansa = 0;
int ansb = 0;
for(int i = 1; i <= n;++i){
for(int j = 1; j <= m;++j){
if(b[i] == j && c[j] == i){
ansa = i;
ansb = j;
}
}
}
if(ansa&&ansb){
cout<<"["<<ansa - 1<<"]"<<"["<<ansb - 1<<"]="<<a[ansa][ansb]<<" is Saddle Point";
}
else cout<<"No Saddle Point";
return 0;
}
5.2学生成绩统计
#include<iostream>
using namespace std;
char s[1000];
int a[10];
int num = 0;
int main(){
cin>>s;
for(int i = 0;s[i] != ' ';++i){
if(s[i] >= 48 && s[i] <= 57){
num++;
a[s[i] - 48]++;
}
}
cout<<"Number's amount is:"<<num<<endl;
cout<<"'0...9' amount is:";
for(int i = 0;i <= 9;++i)cout<<a[i]<<" ";
return 0;
}
6.1插入字符串
#include<iostream>
#include<cstring>
using namespace std;
char s[100],c[100];
int main(int argc, char const *argv[])
{
cin>>s;
strcpy(c,s);
int maxx = -100;
int p = 0;
for(int i = 0;s[i] !=' ';++i){
if(s[i] > maxx){
maxx = s[i];
p = i;
}
}
s[p + 1] = 'a';
s[p + 2] = 'b';
for(int i = p + 3;c[i - 2] != ' ';++i)
s[i] = c[i - 2];
cout<<s;
return 0;
}
6.2统计整数个数
#include<iostream>
#include<cstring>
using namespace std;
int a[1000],ans[1000];
int num = 0;
int main(int argc, char const *argv[])
{
char s[10000];
cin.getline(s,10000);
for(int i = 0; s[i] != ' ';++i){
if(s[i]>=48 && s[i] <= 57){
int temp = 0;
num++;
while(s[i]>=48 && s[i] <= 57){
a[++temp] = s[i] - 48;
++i;
}
int p = 1;
for(int i = temp; i >= 1;--i){
ans[num] += a[i] * p;
p *= 10;
}
}
}
cout<<num<<endl;
for(int i = 1; i <= num;++i)cout<<ans[i]<<endl;
return 0;
}
/*a123x456 17935? 098tab583*/
6.3字符串排序
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct data
{
char s[100];
int num;
}a[10];
bool cmp(data a, data b){
return a.num < b.num;
};
int main(){
for(int i = 1; i <= 5;++i){
cin>>a[i].s;
a[i].num = strlen(a[i].s);
}
sort(a + 1, a + 5 + 1,cmp);
for(int i = 1; i <= 5;++i){
cout<<a[i].s<<" ";
}
cout<<endl<<"concatenate string:";
for(int i = 1; i <= 5;++i){
if(a[i].num >= 3)cout<<a[i].s[2];
else cout<<" ";
}
return 0;
}
7.1计票程序
#include<iostream>
#include<cstring>
using namespace std;
struct person {
char name[20];
int count;
}leader[3] = {"Li", 0, "Zhang", 0, "Fun", 0};
int main(int argc, char const *argv[])
{
int n;
cin>>n;
for(int i = 1; i <= n; ++i){
char s[20];
cin>>s;
int x = strlen(s);
if(x == 2)leader[0].count++;
if(x == 5)leader[1].count++;
if(x == 3)leader[2].count++;
}
for(int i = 0; i < 3;++i){
cout<<leader[i].name<<":"<<leader[i].count<<endl;
}
return 0;
}
7.2计算天数
#include<iostream>
using namespace std;
struct time
{
int year,month,day;
}t;
int m[2][14]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
bool isp(int x){
if((x % 4 == 0 && x % 100 != 0) || (x % 400 == 0)) return true;
return false;
}
int main(){
int ans = 0;
cin>>t.year>>t.month>>t.day;
for(int i = 1; i <= t.month - 1; ++i)
ans += m[isp(t.year)][i];
ans += t.day;
cout<<ans<<endl;
return 0;
}
8.1日期类
#include<iostream>
#include<cmath>
using namespace std;
int m[2][14]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
int psum[5000];
class date{
private:
int year,month,day;
public:
date(){};
void setDate(int a,int b,int c){
year = a;
month = b;
day = c;
};
int isp(){
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) return 1;
return 0;
};
int sol(date b){
int n,ma;
n = psum[year - 1];
for(int i = 1; i <= month - 1;++i) n += m[isp()][i];
n += day;
ma = psum[b.year - 1];
for(int i = 1; i <= b.month - 1;++i) ma += m[b.isp()][i];
ma += b.day;
return abs(n - ma);
};
};
int main(){
for(int i = 1 ;i <= 5000;++i){
if((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))psum[i] = psum[i - 1] + 366;
else psum[i] = psum[i - 1] + 365;
}
int a,b,c;
int aa,bb,cc;
cin>>a>>b>>c>>aa>>bb>>cc;
date A,B;
A.setDate(a,b,c);
B.setDate(aa,bb,cc);
if(A.isp())cout<<a<<" is leap year."<<endl;
else cout<<a<<" is not leap year."<<endl;
if(B.isp())cout<<aa<<" is leap year."<<endl;
else cout<<aa<<" is not leap year."<<endl;
cout<<"The skip of two date is "<<A.sol(B)<<".";
return 0;
}
8.2三角形类
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
class Beeline{
private:
double x1,x2,y1,y2;
public:
Beeline(double a = 0,double b = 0,double c = 0,double d = 0){
x1 = a;
y1 = b;
x2 = c;
y2 = d;
}
double length(){
return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2) *(y1 - y2));
}
void show(){
cout<<"("<<x1<<", "<<y1<<")"<<","<<"("<<x2<<", "<<y2<<")"<<endl;
}
};
class Triangle{
private:
Beeline line1,line2,line3;
public:
Triangle(int x1,int y1,int x2,int y2,int x3,int y3):line1(x1,y1,x2,y2),line2(x2,y2,x3,y3),line3(x3,y3,x1,y1){};
double Area(){
double a,b,c,p;
a = line1.length();
b = line2.length();
c = line3.length();
p = 0.5*(a + b + c);
return sqrt(p*(p-a)*(p-b)*(p-c));
}
void print(){
cout<<"Three edges' points are listed as follows:"<<endl;
line1.show();
line2.show();
line3.show();
cout<<"The area of this triangle is: ";
printf("%.2f",Area());
cout<<".";
}
};
int main(){
double x1,y1,x2,y2,x3,y3;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
Triangle t(x1,y1,x2,y2,x3,y3);
t.print();
return 0;
}
9.1点类
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
class point {
double x,y;
public:
point (double x0=0, double y0=0) {x=x0; y=y0;}
void display(){
cout<<"("<<x<<","<<y<<")"<<endl;
}
friend double dis(point A,point B);
};
double dis(point A,point B){
return sqrt((A.x - B.x) * (A.x - B.x)+(A.y - B.y) * (A.y - B.y));
}
int main(int argc, char const *argv[])
{
double x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
point a(x1,y1),b(x2,y2);
a.display();
b.display();
cout<<dis(a,b);
return 0;
}
9.2学生成绩类
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
class Score{
private:
int Chinese,Math,English;
static int TotalScore;
static int TotalStudent;
public:
Score(){};
void setScore(int c,int m,int e){
Chinese = c;
Math = m;
English = e;
TotalStudent++;
TotalScore += Sum();
}
int Sum(){
return Chinese + Math + English;
}
void show(){
cout<<Chinese<<" "<<Math<<" "<<English<<endl;
}
double static getAve(){
double ans = (double)TotalScore/(double)TotalStudent;
return ans;
};
}s[200];
int Score:: TotalStudent = 0;
int Score:: TotalScore = 0;
int main(int argc, char const *argv[])
{
int n;
int temp = 0;
cin>>n;
for(int i = 1; i <= n;++i){
int op;
cin>>op;
if(op == 1){
int x,y,z;
cin>>x>>y>>z;
s[++temp].setScore(x,y,z);
}
if(op == 2){
int j;
cin>>j;
cout<<s[j].Sum()<<endl;
}
if(op == 3){
int j;
cin>>j;
s[j].show();
}
if(op == 4){
double ans = Score::getAve();
printf("%.2f
",ans);
}
}
return 0;
}
/*
10
1 90 85 90
1 80 90 75
2 1
3 2
4
1 80 80 85
1 50 60 65
1 30 90 75
3 5
4
*/
10.1员工类
#include<iostream>
#include<cstring>
using namespace std;
class person{
char name[20];
char sex;
int age;
public:
person(char *n,char s,int a){
strcpy(name,n);
sex = s;
age = a;
}
void show(){
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
}
};
class emp:public person{
int basicsalary,holiday;
public:
emp(char *n,char s,int a,int b,int h):person(n,s,a){
basicsalary = b;
holiday = h;
}
void show(){
person::show();
cout<<"basicSalary:"<<basicsalary<<endl;
cout<<"leavedays:"<<holiday<<endl;
}
};
int main(int argc, char const *argv[])
{
char n[20];char s;int a,b,h;
cin>>n>>s>>a>>b>>h;
emp aa(n,s,a,b,h);
aa.show();
return 0;
}
10.2水果店
#include<cstring>
#include<iostream>
using namespace std;
class fruit{
protected:
double p,w;
public:
fruit(double a,double b):p(a),w(b){};
virtual double money() = 0;
double operator +(double total){
return money() + total;
}/*不用友元是第一个调用的*this*/
};
class Apple:public fruit{
public:
Apple(double a,double b):fruit(a,b){};
double money(){
return p*w;
}
};
class Banana:public fruit{
public:
Banana(double a,double b):fruit(a,b){};
double money(){
return p*w*0.5;
}
};
class Orange:public fruit{
public:
Orange(double a,double b):fruit(a,b){};
double money(){
if(w > 10) return p*w*0.5;
else if(w > 5) return p*w*0.8;
else return p*w;
}
};
int main(){
fruit* p;
char s;
double total;
total = 0.0;
while(1){
double weight =0;
double price = 0;
cin>>s;
if(s == 'q')break;
cin>>weight>>price;
if(s == 'a'){
Apple a(price,weight);
p = &a;
total = *p + total;
}
if(s == 'b'){
Banana b(price,weight);
p = &b;
total = *p + total;
}
if(s == 'o'){
Orange o(price,weight);
p = &o;
total = *p + total;
}
}
cout<<total<<endl;
return 0;
}
/*
a 10 10
a 10 10
b 10 10
o 20 10
q
*/