HDU/HDOJ 1141 Factstone Benchmark(数学题,水题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1141

转换成对数来求解,这里先通过年数求出bit位,然后通过bit位求出2^bit,即最大范围。

n! <= 2^bit即可~~~

代码:

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
// Author: Tanky Woo
// Blog: www.WuTianQi.com
// Title: HDOJ 1141 Factstone Benchmark
// About: 数学题,用对数省去大数的麻烦
 
#include <iostream>
#include <cmath>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
 
int main()
{
    int year;
    while(cin >> year && year)
    {
        int t = (year-1960)/10 + 2;
        t = 1 << t;   // t位可以表示的范围
        //cout << t << endl;
        double ans = 0;
        int i;
        for(i=1; ; ++i)
        {
            ans += log((double)i)/log(2.0);
            if(ans > t)
                break;
        }
        //cout << ans << endl;
        cout << i-1 << endl;
    }
    return 0;
}

HDU/HDOJ 1128 Self Numbers(暴力题,水题)

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1128

暴力即可,不过这里先开始我给TLE了,因为我把每一个数,都从他的d(n),  d(d(n))…都算了,遍历从1~1000000   =。=

其实再想想,只需要算d(n)即可,然后遍历1~1000000,因为比如d(…d(n))是x,则d(x)任然会在后面遍历到。。。

AC代码:

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
// Author: Tanky Woo
// Blog: www.WuTianQi.com
// Title: HDOJ 1128 Self Numbers
// About: 
 
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
 
bool hash[1000005];
int t1, t2;
/*
inline void fun(int n)
{
    while(n <= 1000000)
    {
        t1 = n;
        t2 = n;
        while(t1)
        {
            t2 += t1%10;
            t1 /= 10;
        }
        n = t2;
        hash[n] = 1;
    }
}
*/
 
inline void fun(int n)
{
 
        t1 = n;
        t2 = n;
        while(t1)
        {
            t2 += t1%10;
            t1 /= 10;
        }
        n = t2;
        hash[n] = 1;
}
 
int main()
{
    memset(hash, 0, sizeof(hash));
    for(int i=1; i<= 1000000; ++i)
        fun(i);
    for(int i=1; i<=1000000; ++i)
        if(hash[i] == 0)
            printf("%d\n", i);
    return 0;
}

HDU/HDOJ 1113 Word Amalgamation(水题)

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1113

代码:

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
// Author: Tanky Woo
// Blog: www.WuTianQi.com
// Title: HDOJ 1113 Word Amalgamation
// About: 字符串
 
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
 
map<string, string> str;
string s, t;
 
int main()
{
	freopen("input.txt", "r", stdin);
	while(cin >> s && s != "XXXXXX")
	{
		t = s;
		sort(s.begin(), s.end());
		str[t] = s;
	}
	while(cin >> s && s != "XXXXXX")
	{
		bool flag = 0;
		t = s;
		sort(s.begin(), s.end());
		for(map<string, string>::iterator it=str.begin(); it!=str.end(); ++it)
			if(it->second == s)
			{
				cout << it->first << endl;
				flag = 1;
			}
		if(flag == 0)
			cout << "NOT A VALID WORD\n";
		cout << "******\n";
 
	}
}

HDU/HDOJ 1033 Edge(水题,模拟题)

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1033

题目就是说当输入A时,顺时针旋转,输入V时,逆时针旋转,很简单。

代码:

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
// Author: Tanky Woo
// Blog: www.WuTianQi.com
// Title: HDOJ 1033 Edge
// About: 模拟
 
#include <iostream>
#include <string>
using namespace std;
 
typedef struct Point{
    int x, y;
}Point;
 
Point p;
string s;
int dir; // 1-up, 2-down, 3-left, 4-right
 
void funA()
{
    if(dir == 1)
    {
        p.x += 10;
        dir = 4;
    }
    else if(dir == 2)
    {
        p.x -= 10;
        dir = 3;
    }
    else if(dir == 3)
    {
        p.y += 10;
        dir = 1;
    }
    else
    {
        p.y -= 10;
        dir = 2;
    }
    cout << p.x << " " << p.y << " lineto" << endl;
}
 
void funV()
{
    if(dir == 1)
    {
        p.x -= 10;
        dir = 3;
    }
    else if(dir == 2)
    {
        p.x += 10;
        dir = 4;
    }
    else if(dir == 3)
    {
        p.y -= 10;
        dir = 2;
    }
    else
    {
        p.y += 10;
        dir = 1;
    }
    cout << p.x << " " << p.y << " lineto" << endl;
}
 
int main()
{
    while(cin >> s)
    {
        cout << "300 420 moveto" << endl;
        cout << "310 420 lineto" << endl;
        dir = 4;
        p.x = 310;
        p.y = 420;
        for(int i=0; i<s.size(); ++i)
        {
            if(s[i] == 'A')
                funA();
            else
                funV();
        }
        cout << "stroke\nshowpage\n";
    }
    return 0;
}

看见网上有人这么写的,简单多了,我是枚举上下左右四个方向时的变化,那个代码是直接找到相对关系的:

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
#include <stdio.h>
#include <memory>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <time.h>
using namespace std;
const int N = 205; 
struct point{
int x, y; 
point(int x = 0, int y = 0): x(x), y(y) {}
void set(int _x, int _y){ x = _x, y = _y; }
};
char in[N]; 
void solve(){
point st(300, 420), ed(310, 420), ted; 
printf("300 420 moveto\n310 420 lineto\n");
int i; 
for(i = 0; in[i]; i++){
   if(in[i] == 'A'){ //顺时针转
    ted.x = (ed.y - st.y) + ed.x; 
    ted.y = -(ed.x - st.x) + ed.y; 
   }else{ //逆时针旋转
    ted.x = -(ed.y - st.y) + ed.x; 
    ted.y = (ed.x - st.x) + ed.y; 
   }
   st = ed; 
   ed = ted; 
   printf("%d %d lineto\n", ed.x, ed.y);
}
printf("stroke\nshowpage\n");
}
int main(){
freopen("in.txt", "r", stdin); 
//freopen("out.txt", "w", stdout); 
while(scanf("%s", in) != EOF) solve(); 
return 0;
}

HDU/HDOJ 3006 The Number of set(位状态压缩+枚举)

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3006

位状态压缩的经典体型。

题意是给出一些集合,求这些集合的并集一共有多少种?

这里有点类似模->数转换,由此想到位状态压缩,把输入的数据压缩成一位,一个set就转换成一个int型了。

至于如何转换,就要用到逻辑或“|”运算符,逻辑或用于把set压缩成int以及把两个集合合并。

具体见代码:

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
// Author: Tanky Woo
// Blog: www.WuTianQi.com
// Title: HDOJ 3306 The Number of set
// About: 位状态压缩+枚举
 
#include <iostream>
using namespace std;
 
int n, m, k;
int arr[ 1<<16 ];
int main()
{
    //freopen("input.txt", "r", stdin);
    while(cin >> n >> m)
    {
        memset(arr, 0, sizeof(arr));
        int ans = 0;   // 记录set种数
        int tmp, res;  // tmp - 保存输入数据    res - 保存一个set状态压缩后的值
        for(int i=1; i<=n; ++i)
        {
            cin >> k;
            res = 0;
            for(int j=1; j<=k; ++j)
            {
                cin >> tmp;
                res |= (1 << (tmp-1));
            }
            arr[res] = 1;
            //cout << "cnt = " << cnt << endl;
            for(int j=0; j<=(1<<15); ++j)
                if(arr[j])
                    arr[j|res] = 1;
        }
        for(int i=1; i<=(1<<15); ++i)
            if(arr[i])
                ++ans;
        cout << ans << endl;
    }
    return 0;
}