Skip to content

Commit 0e3e312

Browse files
committed
2020.02.05
1 parent 9e4a468 commit 0e3e312

File tree

7 files changed

+283
-0
lines changed

7 files changed

+283
-0
lines changed

huawei/0042.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// 0042.学英语
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
string ge[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven","eight", "nine"};
7+
string ot[10] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
8+
string shi[10] = {"zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
9+
10+
string printfHundred(int n)
11+
{
12+
string s = "";
13+
bool hasHundred = false;
14+
if(n/100 != 0) {
15+
s += ge[n/100] + " hundred";
16+
hasHundred = true;
17+
}
18+
n %= 100;
19+
if(n != 0) {
20+
if (hasHundred) {
21+
s += " and ";
22+
}
23+
if (n < 10) {
24+
s += ge[n];
25+
}else if (n < 20) {
26+
s += ot[n-10];
27+
}else {
28+
s += shi[n/10];
29+
if(n%10){
30+
s += " " + ge[n%10];
31+
}
32+
}
33+
}
34+
return s;
35+
}
36+
37+
int main()
38+
{
39+
int n;
40+
while (cin >> n)
41+
{
42+
if (n == 0) {
43+
cout << "zero" << endl;
44+
continue;
45+
}
46+
if (n/1000000000 != 0) {
47+
cout << ge[n/1000000000] << " billion ";
48+
}
49+
n %= 1000000000;
50+
if(n/1000000 != 0) {
51+
cout << printfHundred(n/1000000) << " million ";
52+
}
53+
n %= 1000000;
54+
if(n/1000 != 0) {
55+
cout << printfHundred(n/1000) << " thousand ";
56+
}
57+
n %= 1000;
58+
if(n != 0) {
59+
cout << printfHundred(n);
60+
}
61+
cout << endl;
62+
}
63+
return 0;
64+
}

huawei/0044.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// 0044.Sudoku-Java
2+
3+
// 测试数据含有非唯一解,不在尝试AC
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <queue>
8+
#include <map>
9+
using namespace std;
10+
11+
int v[9][9];
12+
int lin[9][10], row[9][10], check[9][10];
13+
14+
bool test(int x, int y)
15+
{
16+
int cnt = 0, t = 0, np[10];
17+
fill(np, np+10, 0);
18+
for(int i = 1; i < 10; i++) {
19+
np[i] = lin[x][i] + row[y][i] + check[x/3*3+y/3][i];
20+
if(np[i] != 0) {
21+
cnt ++;
22+
} else {
23+
t = i;
24+
}
25+
}
26+
if(cnt == 8) {
27+
v[x][y] = t;
28+
lin[x][t] = 1;
29+
row[y][t] = 1;
30+
check[x/3*3+y/3][t] = 1;
31+
return true;
32+
}
33+
34+
return false;
35+
}
36+
37+
int main()
38+
{
39+
queue<pair<int, int> > q;
40+
for(int i = 0; i < 9; i++){
41+
for (int j = 0; j < 9; j++){
42+
cin >> v[i][j];
43+
lin[i][v[i][j]] = 1;
44+
row[j][v[i][j]] = 1;
45+
check[i/3*3+j/3][v[i][j]] = 1;
46+
if(v[i][j] == 0) {
47+
q.push(make_pair(i,j));
48+
}
49+
}
50+
}
51+
while(!q.empty()) {
52+
pair<int, int> p = q.front();
53+
q.pop();
54+
if (!test(p.first, p.second)) q.push(p);
55+
}
56+
for(int i = 0; i < 9; i++){
57+
for (int j = 0; j < 9; j++){
58+
cout << v[i][j] << ' ';
59+
}
60+
cout << endl;
61+
}
62+
return 0;
63+
}

huawei/0045.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// 0045.名字的漂亮度
2+
3+
#include <iostream>
4+
#include <vector>
5+
#include <map>
6+
#include <algorithm>
7+
using namespace std;
8+
9+
int score(string s)
10+
{
11+
map<char,int> mp;
12+
vector<int> v;
13+
int sum = 0;
14+
for(int i = 0; i < s.size(); i++)
15+
{
16+
if (mp.find(s[i]) == mp.end()) {
17+
mp[s[i]] = 1;
18+
}else {
19+
mp[s[i]] ++;
20+
}
21+
}
22+
for(auto it = mp.begin(); it != mp.end(); it ++)
23+
{
24+
v.push_back(it->second);
25+
}
26+
sort(v.begin(), v.end());
27+
reverse(v.begin(), v.end());
28+
for(int i = 0; i < v.size(); i++)
29+
{
30+
sum += (26-i)*v[i];
31+
}
32+
return sum;
33+
}
34+
35+
int main()
36+
{
37+
int n;
38+
string s;
39+
while(cin >> n)
40+
{
41+
getchar();
42+
for(int i = 0; i < n; i ++)
43+
{
44+
getline(cin, s);
45+
cout << score(s) << endl;
46+
}
47+
}
48+
return 0;
49+
}

huawei/0046.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// 0046.按字节截取字符串
2+
3+
// 根据题意,在判题系统中,汉字以2个字节存储。
4+
#include <iostream>
5+
#include <cstring>
6+
using namespace std;
7+
8+
int main()
9+
{
10+
string s;
11+
int n;
12+
while(cin >> s >> n)
13+
{
14+
if(s[n-1] < 0) s = s.substr(0, n-1);
15+
else s = s.substr(0, n);
16+
cout << s << endl;
17+
}
18+
return 0;
19+
}

huawei/0055.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// 0055.(练习用)挑7
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
int main()
7+
{
8+
int n = 0;
9+
int mp[30001];
10+
mp[0] = 0;
11+
for(int i = 1; i <= 30000; i++)
12+
{
13+
string s = to_string(i);
14+
if(i%7==0 || s.find('7') != s.npos) mp[i] = mp[i-1]+1;
15+
else mp[i] = mp[i-1];
16+
}
17+
while(cin >> n)
18+
{
19+
cout << mp[n] << endl;
20+
}
21+
return 0;
22+
}

huawei/0056.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 0056.iNOC产品部--完全数计算
2+
3+
// 只计算一遍500000的个数,也容易超时的可能原因:
4+
// 1. 可能我算法的时间复杂度太大
5+
// 2. 题目有点问题,说明实际测试的数应该小于明显500000.
6+
// 实际只有以下4个数:6 28 296 8128
7+
#include <iostream>
8+
#include <cmath>
9+
using namespace std;
10+
11+
bool isPerf(int n)
12+
{
13+
int sum = 1;
14+
int t = sqrt(n);
15+
for(int i = 2; i <= t; i++){
16+
if(n%i == 0) sum += i + n/i;
17+
}
18+
if(sum == n) return true;
19+
return false;
20+
}
21+
22+
int main()
23+
{
24+
int n = 0;
25+
int mp[500001];
26+
mp[1] = 0;
27+
for(int i = 2; i <= 100001; i++)
28+
{
29+
if(isPerf(i)) mp[i] = mp[i-1]+1;
30+
else mp[i] = mp[i-1];
31+
}
32+
while(cin >> n)
33+
{
34+
cout << mp[n] << endl;
35+
}
36+
return 0;
37+
}

huawei/0059.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// 0059.找出字符串中第一个只出现一次的字符
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
int main()
7+
{
8+
string s;
9+
int np[128];
10+
while(getline(cin, s))
11+
{
12+
fill(np, np+128, 0);
13+
for(int i = 0; i < s.size(); i++) np[s[i]] ++;
14+
string r = "";
15+
for(int i = 0; i < 128; i++) if(np[i] == 1) r+= (char)i;
16+
if (r.empty()) {
17+
cout << -1 << endl;
18+
continue;
19+
}
20+
for(int i = 0; i < s.size(); i++)
21+
{
22+
if(r.find(s[i]) != r.npos){
23+
cout << s[i] << endl;
24+
break;
25+
}
26+
}
27+
}
28+
return 0;
29+
}

0 commit comments

Comments
 (0)