C++ 程序设计 (西北工业大学)

2022/5/1 17:12:53

本文主要是介绍C++ 程序设计 (西北工业大学),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  NOJ练习一

  1、计算A+B

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int a,b;
 7     cin >> a >> b;
 8     cout << a + b;
 9     
10     return 0;
11 }
View Code

  2、面积及体积的计算

 1 #include <iostream>
 2 using namespace std;
 3 
 4 const double PI = 3.1415926;
 5 int main()
 6 {
 7     double r, h;
 8     cin >> r >> h;
 9     printf("%.2f\n", 2*PI*r);
10     printf("%.2f\n", PI*r*r);
11     printf("%.2f\n", 4*PI*r*r);
12     printf("%.2f\n", 4.0/3*PI*r*r*r);
13     printf("%.2f\n", PI*r*r*h);
14     return 0;
15 }
View Code

  3、总成绩和平均成绩

#include <iostream>
using namespace std;

int main()
{
    double m,l,e;
    cin >> m >> l >> e;
    printf("%.6f\n", m+l+e);
    printf("%.6f\n", (m+l+e)/3);
    return 0;
}
View Code

  NOJ练习八

  1、三角形

 1 #include <iostream>
 2 #include <cmath>
 3 using namespace std;
 4 
 5 class Point{
 6     double x;
 7     double y;
 8 public:
 9     Point(double x = 0.0, double y = 0.0){
10         this->x = x;
11         this->y = y;
12     }
13     double getX()const{
14         return x;
15     }
16     double getY()const{
17         return y;
18     }
19 };
20 
21 class Triangle{
22     Point p1,p2,p3;
23     double lengthOfTwoPoint(const Point& p1, const Point& p2){
24         return sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX()) + 
25             (p1.getY()-p2.getY())*(p1.getY()-p2.getY()) );
26     }
27 public:
28     Triangle(const Point& p1, const Point& p2, const Point& p3){
29         if(isTriangle(p1,p2,p3))
30         {
31             this->p1 = p1;
32             this->p2 = p2;
33             this->p3 = p3;
34         }        
35     }
36     bool isTriangle(const Point& p1, const Point& p2, const Point& p3){
37         if( lengthOfTwoPoint(p1,p2)+lengthOfTwoPoint(p1,p3)>lengthOfTwoPoint(p2,p3)&&
38             lengthOfTwoPoint(p2,p1)+lengthOfTwoPoint(p2,p3)>lengthOfTwoPoint(p1,p3)&&
39             lengthOfTwoPoint(p3,p1)+lengthOfTwoPoint(p3,p2)>lengthOfTwoPoint(p1,p2)
40             ){
41             cout << "YES" << endl;    
42             return true;
43         }
44         cout << "NO" << endl;    
45         return false;
46     }
47 };
48 
49 int main()
50 {
51     double x1,y1,x2,y2,x3,y3;
52     cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
53     Point p1(x1,y1),p2(x2,y2),p3(x3,y3);
54     Triangle t(p1,p2,p3);    
55     return 0;
56 }
View Code

  5、时钟

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Clock{
 5     int hour;
 6     int minute;
 7     int second;
 8 public:
 9     Clock& operator++(){
10         if(++second >= 60){
11             second -= 60;
12             minute++;
13         }
14         if(minute >= 60){
15             minute -= 60;
16             hour++;
17         }
18         hour %= 24;
19         return *this;
20     }
21     Clock operator++(int){ //后置++
22         Clock c = *this;
23         if(++second >= 60){
24             second -= 60;
25             minute++;
26         }
27         if(minute >= 60){
28             minute -= 60;
29             hour++;
30         }
31         hour %= 24;
32         return c;
33     }
34         Clock& operator--(){
35         if(--second <0){
36             second += 60;
37             minute--;
38         }
39         if(minute < 0){
40             minute += 60;
41             hour--;
42         }
43         if(hour < 0)
44             hour += 24;
45         return *this;
46     }
47     Clock operator--(int){ //后置--
48         Clock c = *this;
49         if(--second <0){
50             second += 60;
51             minute--;
52         }
53         if(minute < 0){
54             minute += 60;
55             hour--;
56         }
57         if(hour < 0)
58             hour += 24;
59         return c;
60     }
61     void SetTime(int hour, int minute, int second){
62         this->hour = hour;
63         this->minute = minute;
64         this->second = second;
65     }
66     void ShowTime(){
67         printf("%02d:%02d:%02d\n",hour, minute, second);
68     }
69 };
70 
71 int main()
72 {
73     int n;
74     int h,m,s;
75     scanf("%d:%d:%d",&h,&m, &s);
76     
77     Clock t1,t2;
78     t1.SetTime(h,m,s);
79     
80     scanf("%d",&n);
81     switch(n){
82         case 1: t2=t1++; t1.ShowTime(); t2.ShowTime();break;
83         case 2: t2=t1--; t1.ShowTime(); t2.ShowTime();break;
84         case 3: t2=++t1; t1.ShowTime(); t2.ShowTime();break;
85         case 4: t2=--t1; t1.ShowTime(); t2.ShowTime();break;        
86     }    
87     return 0;
88 }
View Code

 



这篇关于C++ 程序设计 (西北工业大学)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程