A(HDU 1877)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1877
思路:简单的进制转换,由于没有特判0,wrong了
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long LL; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; void huang(unsigned long long a,int k) { if(a == 0) return; huang(a/k,k); printf("%I64d",a%k); } int main() { int m,a,b; while(scanf("%d",&m) && m) { scanf("%d%d",&a,&b); unsigned long long sum; sum = (LL)(a + b); if(sum == 0) { printf("0\n"); continue; } huang(sum,m); printf("\n"); } return 0; }B(HDU 1878)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1878
思路:欧拉图有2个条件(1)是一个联通图(2)每个点的度数是偶数
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long LL; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; int visit[1010]; int pre[1010]; int fin(int x) { return pre[x] == x ? x : pre[x] = fin(pre[x]); } void union1(int a,int b) { int fa = fin(a); int fb = fin(b); if(fa != fb) { pre[fa] = fb; } } int main() { int n,m; while(scanf("%d",&n) && n) { memset(visit,0,sizeof(visit)); scanf("%d",&m); int a,b,i; for(i=1; i<=n; i++) { pre[i] = i; } for(i=0; i<m; i++) { scanf("%d%d",&a,&b); visit[a]++; visit[b]++; union1(a,b); } for(i=1; i<=n; i++) { if(visit[i] & 1) break; } if(i <= n) { printf("0\n"); continue; } int sum = 0; for(int i=1; i<=n; i++) { if(pre[i] == i) sum++; } if(sum == 1) printf("1\n"); else printf("0\n"); } return 0; }C(HDU 1879)
思路:建成的路花费标记为0,跑最短路
D(HDU 1880)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1880
思路:刚开是判断2次,结果超时了,后来队友说,有一种情况只要特判一下就行
超时代码1:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long LL; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; char a[25],b[85]; char c[110]; int main() { map<string,string> map1; while(true) { gets(c); if(strcmp(c,"@END@") == 0) break; int len = strlen(c); int k = 0; int f = 0; while(c[f] != ']') { a[k++] = c[f]; f++; } a[k++] = ']'; a[k] = '\0'; int m = 0; for(int i=f+2; i<len; i++) { b[m++] = c[i]; } b[m] = '\0'; string s1 = a; string s2 = b; map1[a] = b; } int n; scanf("%d",&n); getchar(); map<string,string>::iterator it; for(int i=0; i<n; i++) { string s3; gets(c); s3 = c; bool flag = false; if(s3[0] == '[') { if(map1[s3] == "") printf("what?\n"); else cout<<map1[s3]<<endl; } else { for(it=map1.begin(); it!=map1.end(); it++) { string s4 = (*it).first; string s5 = (*it).second; if(s3.compare(s5) == 0) { flag = true; int len = s4.length(); string s6 = ""; for(int i=1; i<len-1; i++) s6 += s4[i]; cout<<s6<<endl; break; } } if(!flag) cout<<"what?"<<endl; } } return 0; }
超时代码2:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long LL; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; char a[25],b[85]; char c[110]; int main() { map<string,string> map1; while(true) { gets(c); if(strcmp(c,"@END@") == 0) break; int len = strlen(c); int k = 0; int f = 0; while(c[f] != ']') { a[k++] = c[f]; f++; } a[k++] = ']'; a[k] = '\0'; int m = 0; for(int i=f+2; i<len; i++) { b[m++] = c[i]; } b[m] = '\0'; string s1 = a; string s2 = b; map1[a] = b; } int n; scanf("%d",&n); getchar(); map<string,string>::iterator it; for(int i=0; i<n; i++) { string s3; gets(c); s3 = c; bool flag = false; for(it=map1.begin(); it!=map1.end(); it++) { string s4 = (*it).first; string s5 = (*it).second; if(s3.compare(s4) == 0) { flag = true; cout<<s5<<endl; break; } else if(s3.compare(s5) == 0) { flag = true; int len = s4.length(); string s6 = ""; for(int i=1; i<len-1; i++) s6 += s4[i]; cout<<s6<<endl; break; } } if(!flag) cout<<"what?"<<endl; } return 0; }
E(HDU 1881)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1881
解题地址:http://blog.csdn.net/qq_25605637/article/details/51179368
G(HDU 1798)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1798
解题地址:http://blog.csdn.net/qq_25605637/article/details/51190878
H (HDU 1795)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1795
思路:给了一个素数求下一个素数是多少,直接暴力过了,网上还有人用二分,有时间学学
大神地址:http://blog.csdn.net/hpuhjh/article/details/47184421
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long LL; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; bool isprime[11000]; void doprime() { memset(isprime,true,sizeof(isprime)); for(int i=2; i<=1000; i++) { if(isprime[i]) { for(int j=i*i; j<=11000; j+=i) { isprime[j] = false; } } } } int main() { doprime(); int t; scanf("%d",&t); while(t--) { int k; scanf("%d",&k); k++; while(true) { if(isprime[k]) { printf("%d\n",k); break; } k++; } } return 0; }
I(HDU 1794)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1794
思路:打一个三维的表就行了,累加次数最多的和最大的数乘起来就行,贪心吧
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long LL; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; int map1[35][35][35]; int b[1000],c[10010]; bool cmp(int a,int b) { return a > b; } int main() { memset(map1,0,sizeof(map1)); for(int f=1; f<=30; f++) { for(int i=1; i<=f; i++) { for(int j=1; j<=f; j++) { for(int k=0; k<=min(f-i,f-j); k++) { for(int l=i; l<=i+k; l++) { for(int m=j; m<=j+k; m++) { map1[f][l][m]++; } } } } } } int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); int k = 0; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { int x; scanf("%d",&x); if(x == 0) b[k++] = map1[n][i][j]; } } int m; scanf("%d",&m); for(int i=0; i<m; i++) scanf("%d",&c[i]); sort(b,b+k,cmp); sort(c,c+m,cmp); LL sum = 0; for(int i=0; i<k; i++) sum += (LL)b[i] * (LL)c[i]; printf("%I64d\n",sum); } return 0; }