HDU 水题十道,慢慢品味

来源:互联网 发布:星际战甲漂亮捏脸数据 编辑:程序博客网 时间:2024/05/16 09:12

转载自:提高鸟语能力~~


1 hdu 1032

[cpp] view plain copy
 print?
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int len(int n)  
  5. {  
  6.     int s  = 1;  
  7.     while (n!=1)  
  8.     {  
  9.         if (n&1) n = 3*n +1;  
  10.         else n /= 2;  
  11.         s++;  
  12.     }  
  13.     return s;  
  14. }  
  15.   
  16. int main()  
  17. {  
  18.     int i, j, n, l, m;  
  19.     while (~scanf("%d %d", &i, &j))  
  20.     {  
  21.         printf("%d %d", i, j);  
  22.         if (i>j) swap(i, j);  
  23.         m = 0;  
  24.         for (;i<=j;++i)  
  25.         {  
  26.             l = len(i);  
  27.             if (m<l) m = l;  
  28.         }  
  29.         printf(" %d\n", m);  
  30.     }  
  31.     return 0;  
  32. }  

 2 hdu 1029 (鸽巢原理) 

[cpp] view plain copy
 print?
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.     int ans, n, c, num;  
  7.     while (~scanf("%d", &n))  
  8.     {  
  9.         c = 0;  
  10.         for (int i=1;i<=n;++i)  
  11.         {  
  12.             scanf("%d", &ans);  
  13.             if (c==0)  
  14.             {  
  15.                 num = ans;  
  16.                 c ++;  
  17.             }  
  18.             else if (ans == num) c ++;  
  19.             else c --;  
  20.         }  
  21.         printf("%d\n", num);  
  22.     }  
  23.     return 0;  
  24. }  

3 hdu 1033 题目看到蛋碎 

[cpp] view plain copy
 print?
  1. #include <iostream>  
  2. using namespace std;  
  3. int f[4][2] = {0,10,10,0,0,-10,-10,0};  
  4. void move(int &x, int &y, char c, int &s)  
  5. {  
  6.     int u;  
  7.     switch (s)  
  8.     {  
  9.         case 0: if (c=='V') u = 0, s = 1; else u = 2, s = 3; break;  
  10.         case 1: if (c=='V') u = 3, s = 2; else u = 1, s = 0; break;  
  11.         case 2: if (c=='V') u = 2, s = 3; else u = 0, s = 1; break;  
  12.         case 3: if (c=='V') u = 1, s = 0; else u = 3, s = 2; break;  
  13.     }  
  14.     x += f[u][0];  
  15.     y += f[u][1];  
  16. }  
  17.   
  18. int main()  
  19. {  
  20.     char c[300];  
  21.     int i, k;  
  22.     while (~scanf("%s", c))  
  23.     {  
  24.         k = strlen(c);  
  25.         printf("300 420 moveto\n310 420 lineto\n");  
  26.         int x = 310, y = 420, s = 0;  
  27.         for (i=0;i<k;++i)  
  28.         {  
  29.             move(x, y, c[i], s);  
  30.             printf("%d %d lineto\n", x, y);  
  31.         }  
  32.         printf("stroke\nshowpage\n");  
  33.     }  
  34.     return 0;  
  35. }  

4 hdu 1036  sscanf的用法

[cpp] view plain copy
 print?
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.     int f, n, i, a, sum, h, m, s;  
  7.     double d;  
  8.     char c[20], t[100];  
  9.     scanf("%d%lf", &n, &d);  
  10.     while (~scanf("%d", &a))  
  11.     {  
  12.         sum = f = 0;  
  13.         for (i=1;i<=n;++i)  
  14.         {  
  15.             scanf("%s", c);  
  16.             if (c[0] == '-') f = 1;  
  17.             sscanf(c, "%d:%d:%d", &h, &m, &s);  
  18.             sum += h*3600 + m*60 + s;  
  19.         }  
  20.         if (f) printf("%3d: -\n", a);  
  21.         else  
  22.         {  
  23.             sum = int(sum/d + 0.5);  
  24.             printf("%3d: %d:%2.2d min/km\n", a, sum/60, sum%60);  
  25.         }  
  26.     }  
  27.     return 0;  
  28. }  
  29.               

5 hdu 1037 不解释 

[cpp] view plain copy
 print?
  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.   int h1, h2, h3;  
  6.   while (~scanf("%d %d %d", &h1, &h2, &h3))  
  7.   {  
  8.     if (h1>=168 && h2>=168 && h3>=168)  
  9.     printf("NO CRASH\n");  
  10.     else  
  11.     {  
  12.       printf("CRASH ");  
  13.       if (h1<168)  
  14.       {  
  15.          printf("%d\n",h1);  
  16.          break;  
  17.       }  
  18.       if (h2<168)  
  19.       {  
  20.          printf("%d\n",h2);  
  21.          break;  
  22.       }  
  23.       if (h3<168)  
  24.       {  
  25.          printf("%d\n",h3);  
  26.          break;  
  27.       }  
  28.     }  
  29.   }   
  30.   return 0;  
  31. }  

6 hdu 1038 

[cpp] view plain copy
 print?
  1. #include <stdio.h>  
  2. #define P 3.1415926  
  3. int main()  
  4. {  
  5.     double diameter, revolutions, time;  
  6.     double sum, zong;  
  7.     int n = 0;  
  8.   
  9.     while(~scanf("%lf%lf%lf",&diameter,&revolutions,&time))  
  10.     {  
  11.         if (revolutions==0) break;  
  12.         n++;  
  13.         sum = 0;  
  14.         zong = 0;  
  15.         sum = diameter / 12 / 5280 * P * revolutions * time;  
  16.         zong = sum/time;  
  17.         sum = zong / time * 60 * 60;  
  18.         printf("Trip #%d: %.2lf %.2lf\n",n,zong,sum);  
  19.     }  
  20.     return 0;  
  21. }  

7 hdu 1039  

[cpp] view plain copy
 print?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <assert.h>  
  5. #define MAX_LEN 1111  
  6.   
  7. char psd[MAX_LEN];  
  8. const char end[] = "end";  
  9. const char s1[] = "aeiou";  
  10.   
  11. int c1(void)  
  12. {  
  13.     int i,j;  
  14.     for(i=0;psd[i]!='\0';i++)  
  15.         for(j=0;s1[j]!='\0';j++)  
  16.             if(psd[i]==s1[j])  
  17.                 return 1;  
  18.     return 0;  
  19. }  
  20.   
  21. int c2(void)  
  22. {  
  23.     int i,j,cnt1,cnt2,tag;  
  24.     cnt1=cnt2=0;  
  25.     for(i=0;psd[i]!='\0';i++)  
  26.     {  
  27.         for(j=0,tag=0;s1[j]!='\0';j++)  
  28.             if(psd[i]==s1[j])  
  29.             {  
  30.                 cnt1++;  
  31.                 cnt2=0;  
  32.                 tag=1;  
  33.                 break;  
  34.             }  
  35.         if(tag==0)  
  36.         {  
  37.             cnt1=0;  
  38.             cnt2++;  
  39.         }  
  40.         if(cnt1>=3 || cnt2>=3)  
  41.             return 0;  
  42.     }  
  43.     return 1;  
  44. }  
  45.   
  46. int c3(void)  
  47. {  
  48.     int i;  
  49.     for(i=1;psd[i]!='\0';i++)  
  50.         if(psd[i]==psd[i-1])  
  51.             if(psd[i]!='e' && psd[i]!='o')  
  52.                 return 0;  
  53.     return 1;  
  54. }  
  55.   
  56. int main(void)  
  57. {  
  58. #ifndef ONLINE_JUDGE  
  59.     assert(freopen("1039.in","r",stdin));  
  60. #endif  
  61.     while(scanf("%s",psd),strcmp(psd,end))  
  62.     {  
  63.         if(c1() && c2() && c3())  
  64.             printf("<%s> is acceptable.\n",psd);  
  65.         else  
  66.             printf("<%s> is not acceptable.\n",psd);  
  67.     }  
  68.     return 0;  
  69. }  

8 hdu 1047

大正整数加。

脑抽用string。。。越写越丑。。。还好一遍a了,避免了重敲的宿命。。

[cpp] view plain copy
 print?
  1. #include <iostream>  
  2. #include <string>  
  3. #include <vector>  
  4. #include <algorithm>  
  5. #define clr(x, k) memset((x), (k), sizeof(x))  
  6. const int N = 1000;  
  7. using namespace std;  
  8. //char a[N], x[N];  
  9. string x, a;  
  10. int l;  
  11. void re(string &x)  
  12. {  
  13.     int i = 0;  
  14.     int j = x.length() -1;  
  15.     while (i<j)  
  16.         swap(x[i++], x[j--]);  
  17. }  
  18. void add(string &a, string &x)  
  19. {  
  20.     int i, j, ma, mi;  
  21.     ma = a.length();  
  22.     mi = x.length();  
  23.     if (ma<mi)  
  24.     {  
  25.         swap(ma, mi);  
  26.         swap(a, x);  
  27.     }  
  28.     for (i=0;i<mi;++i) a[i] += x[i]-'0';  
  29.     for (i=0;i<ma;++i)  
  30.     if (a[i]>'9')  
  31.     {  
  32.         if (i+1<ma) a[i+1] += (a[i]-'0')/10;  
  33.         else a.insert(i+1,1,(a[i]-'0')/10+'0');  
  34.     a[i] -= 10;  
  35.     }  
  36. }  
  37. int main()  
  38. {  
  39.     int n;  
  40.     scanf("%d", &n);  
  41.     while (n--)  
  42.     {  
  43.         l = 1;  
  44.         a = "0";  
  45.         while (cin>>x)  
  46.         {  
  47.             re(x);  
  48.             if (x == "0")  
  49.             {  
  50.                 re(a);  
  51.                 cout<<a<<endl;  
  52.                 if (n!=0) cout<<endl;  
  53.                 break;  
  54.             }  
  55.             add(a, x);  
  56.         }  
  57.     }  
  58.     return 0;  
  59. }  
  60.           

9 hdu 1856 ans初值为1,wa了几次才找到。。。 

[cpp] view plain copy
 print?
  1. #include <iostream>  
  2. using namespace std;  
  3. const int N = 10000002;  
  4. int n, m;  
  5. int f[N], r[N];  
  6.   
  7. int find(int x)  
  8. {  
  9.     if (f[x]!=x) f[x] = find(f[x]);  
  10.     return f[x];  
  11. }  
  12.   
  13. int main()  
  14. {  
  15.     int x, y, i, ans;  
  16.     while (scanf("%d", &n)!=EOF)  
  17.     {  
  18.         for (i=0;i<=N;++i)  
  19.         {  
  20.             f[i] = i;  
  21.             r[i] = 1;  
  22.         }  
  23.         ans = 1;  
  24.         for (i=0;i<n;++i)  
  25.         {  
  26.             scanf("%d %d", &x, &y);  
  27.             x = find(x);  
  28.             y = find(y);  
  29.             if (x>y) swap(x, y);  
  30.             if (x!=y)  
  31.             {  
  32.                 f[y] = x;  
  33.                 r[x] += r[y];  
  34.                 if (r[x]>ans) ans = r[x];  
  35.             }  
  36.         }  
  37.         printf("%d\n", ans);  
  38.     }  
  39.     return 0;  
  40. }  
  41.           

 

10 hdu 1060 

题目大意是输入N,求N^N的最高位数字。1<=N<=1,000,000,000
N^N  = a*10^x;
我们要求的最右边的数字就是(int)a,即a的整数部分
两边同时取以10为底的对数 lg(N^N) = lg(a*10^x) ;
N*lg(N)  = lg(a) + x;

N*lg(N) - x = lg(a)

a = 10^(N*lg(N) - x);

现在就只有x是未知的了,如果能用n来表示x的话,这题就解出来了。

又因为,x是N^N的位数-1。比如 N^N = 1200  ==>  x = 3;   

实际上就是 x 就是lg(N^N) 向下取整数,表示为[lg(N^N)]

a = 10^(N*lg(N) – [lg(N^N)]);

然后(int)a 就是答案了。

[cpp] view plain copy
 print?
  1.    
  2. #include<iostream>  
  3. #include<cstring>  
  4. #include<cmath>  
  5. using namespace std;  
  6. int main(){  
  7.     int t;  
  8.     __int64 ans,n;  
  9.     double m;  
  10.     scanf("%d",&t);  
  11.     while(t--){  
  12.          scanf("%I64d",&n);  
  13.          m=n*log10(n+0.0);  
  14.          m-=(__int64)m;  
  15.          ans=pow((double)10,m);  
  16.          printf("%I64d\n",ans);     
  17.     }  
  18. return 0;  
  19. }  

11 hdu 1061 N^N%10

[cpp] view plain copy
 print?
  1. #include <cstdio>  
  2. #include <cstdlib>  
  3. #define I64 __int64  
  4. int Fuction(I64 n)  
  5. {  
  6.    int res = 1;  
  7.    I64 b = n;  
  8.    if(!n)                             // n==0, 输出1  
  9.       return 1;  
  10.    if(!(n % 10))  
  11.       return 0;  
  12.    while(b)  
  13.    {  
  14.       if(b & 1)  
  15.       {  
  16.          res *= n;  
  17.          res %= 10;                      //  要取模,否则溢出  
  18.       }  
  19.       n *= n;  
  20.       n %= 10;                            //  要取模,否则溢出  
  21.       b >>= 1;  
  22.    }  
  23.    return res % 10;  
  24. }  
  25. int main()  
  26. {  
  27.    int t;  
  28.    I64 n;  
  29.    scanf("%d", &t);  
  30.    while(t--)  
  31.    {  
  32.       scanf("%I64d", &n);  
  33.       printf("%d\n", Fuction(n));  
  34.    }  
  35.    return 0;  
  36. }  
  37.   
  38. //题目分析:求n^n的个位数,只要根据每一个数的幂的周期性规律,就行了  
  39. #include <stdio.h>  
  40. int main()  
  41. {  
  42.        int m,n,a;  
  43.        int s[10][4]={{0},{1},{6,2,4,8},{1,3,9,7},{6,4},{5},{6},  
  44.        {1,7,9,3},{6,8,4,2},{1,9}};  
  45.   
  46.        scanf("%d",&m);  
  47.        while(m--)  
  48.              {  
  49.             scanf("%d",&n);  
  50.             a=n % 10;  
  51.             if(a==0||a==1||a==5||a==6)  
  52.                    printf("%d\n",a);  
  53.             else if(a==4||a==9)  
  54.                    printf("%d\n",s[a][n%2]);  
  55.             else if(a==2||a==3||a==7||a==8)  
  56.                    printf("%d\n",s[a][n%4]);  
  57.        }  
  58.        return 0;  
  59. }  

0 0