我是靠谱客的博主 外向音响,这篇文章主要介绍牛客网华为机试2HJ101输入整型数组和排序标识,对其元素按照升序或降序进行排序HJ100等差数列HJ99自守数HJ98自动售货系统,现在分享给大家,希望可以做个参考。
HJ101输入整型数组和排序标识,对其元素按照升序或降序进行排序
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include<bits/stdc++.h> using namespace std; int sort1(int a ,int b ){ return a>b; } int sort2(int a ,int b ){ return a<b; } int main(){ int num; cin>>num; int x=num; vector<int> data(num,0); int i = 0; while(x>0){ cin>>data[i]; i++; x--; } int ans; cin>>ans; if(ans == 0){ sort(data.begin(),data.end(),sort2); for(i = 0;i<num;i++){ cout<<data[i]<<" "; } } else if(ans == 1){ sort(data.begin(),data.end(),sort1); for(i = 0;i<num;i++){ cout<<data[i]<<" "; } } return 0; }
HJ100等差数列
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include<iostream> using namespace std; int main(){ int num; cin>>num; int x=2; int d=3; int res = 0; for(int i =0;i<num;i++){ res += x; x += d; } cout<<res<<endl; return 0; }
HJ99自守数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include<iostream> using namespace std; int main() { int n; while (cin >> n) { int base = 10, count = 0; for (int i = 0; i <= n; i++) { if (i == base) base *= 10; if (i*i%base == i) count++; } cout << count << endl; } return 0; }
HJ98自动售货系统
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131// 存钱盒 投币余额 各种面额钱币张数 #include<cstring> #include<bits/stdc++.h> using namespace std; typedef struct { int hao; // 只存序号 int price; int num; }Prod; typedef struct { int hao; int num; }Face; vector<Prod> prod(6); vector<Face> face(4); int spare = 0; // 唯一一个单独的全局变量:‘投币余额’ void init(string s); void back(string s); void query(string s); void in(string s); void buy(string s); int main() { string line; while (getline(cin, line)) { stringstream ss(line); string s; while (getline(ss, s, ';')) { if (s[0] == 'r') init(s); if (s[0] == 'c') back(s); if (s[0] == 'q') query(s); if (s[0] == 'p') in(s); if (s[0] == 'b') buy(s); } } } void init(string s) { spare=0; s = s.substr(2); int pos = s.find(' '); string str1 = s.substr(0, pos), str2 = s.substr(pos + 1); stringstream ss1(str1), ss2(str2); char punc = '-'; // 这里有个疑问是,ss1中类型并非int、是如何传输成功的?// 但是VS里面传输失败,只有在牛客能成功 ss1 >> prod[0].num >> punc >> prod[1].num >> punc >> prod[2].num >> punc >> prod[3].num >> punc >> prod[4].num >> punc >> prod[5].num; ss2 >> face[0].num >> punc >> face[1].num >> punc >> face[2].num >> punc >> face[3].num; for (int i = 0; i < 6; i++) prod[i].hao = i; prod[0].price = 2; prod[1].price =3; prod[2].price = 4; prod[3].price = 5; prod[4].price = 8; prod[5].price = 6; face[0].hao = 1; face[1].hao = 2; face[2].hao = 5; face[3].hao = 10; cout << "S001:Initialization is successful" << endl; } void back(string s) { int sum=0; int backCot[4] = {0}; if (spare == 0) cout << "E009:Work failure" << endl; else if(spare>0){ for(int i=3;i>=0;i--){ int face_num = face[i].num; for(int j=0;j<face_num;j++){ if(face[i].hao>spare) break; if(sum+face[i].hao<=spare){ sum += face[i].hao; face[i].num--; backCot[i]++; } } } spare -= sum; for(int i=0;i<4;i++){ cout<<face[i].hao<<" yuan coin number="<<backCot[i]<<endl; } } } bool cmp(Prod a, Prod b) { if (a.num != b.num) return a.num > b.num; return a.hao < b.hao; } void query(string s) { if (s.size() <= 2) { cout << "E010:Parameter error"<<endl; return; } else if (s[2] == '0') { sort(prod.begin(), prod.end(), cmp); for (int i = 0; i < 6; i++) cout << 'A' << prod[i].hao + 1 <<' '<< prod[i].price << ' ' << prod[i].num<<endl; } else if (s[2] == '1') { for (int i = 0; i < 4; i++) cout << face[i].hao << " yuan coin number=" << face[i].num << endl; } else cout << "E010:Parameter error" << endl; } void in(string s){ int sN = stoi(s.substr(2)); bool limit = true,exist=false; if(sN == 1 || sN == 2) limit = false; for(int i=0;i<6;i++){ if(prod[i].num >0) exist=true; } if((sN>2 && sN<5)||(sN>5 &&sN<10)|| (sN>10)) cout<<"E002:Denomination error"<<endl; else if((face[0].num + face[1].num*2 < sN) && limit) cout<<"E003:Change is not enough, pay fail"<<endl; else if(!exist) cout<<"E005:All the goods sold out"<<endl; else{ // 投币成功! for(int i=0;i<4;i++){ if(face[i].hao == sN) face[i].num++; } spare += sN; // 投币余额 cout<<"S002:Pay success,balance="<<spare<<endl; } } void buy(string s){ int hao = stoi(s.substr(3)); // 先只提取hao,不包括‘A’ bool exist=false,nums=false; for(int i=0;i<6;i++){ if(i+1 == hao) exist=true; } if(!exist) cout<<"E006:Goods does not exist"<<endl; else if(prod[hao-1].num==0) cout<<"E007:The goods sold out"<<endl; else if(spare < prod[hao-1].price) cout<<"E008:Lack of balance"<<endl; else{ spare -= prod[hao-1].price; // 投币余额得减掉了 cout<<"S003:Buy success,balance="<<spare<<endl; } }
最后
以上就是外向音响最近收集整理的关于牛客网华为机试2HJ101输入整型数组和排序标识,对其元素按照升序或降序进行排序HJ100等差数列HJ99自守数HJ98自动售货系统的全部内容,更多相关牛客网华为机试2HJ101输入整型数组和排序标识内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复