个人选拔赛第三场 解题代码

hdu 2131

View Code 
#include<stdio.h>
#include<string.h>
int main()
{
    char ch[2],s[210];
    while(scanf("%s %s",ch,s)!=EOF)
    {
          char x = ch[0];
          int len = strlen(s);   
          int cnt = 0;
          if( x >= 'A' && x <= 'Z' )   
          {
              for(int i = 0; i < len; ++i )
                  if( s[i] == x || s[i] == x+32 ) 
                      cnt++;  
          }          
          else
          {
              for(int i = 0; i < len; ++i )
                  if( s[i] == x || s[i] == x-32 )
                      cnt++;    
          }
          printf("%.5f\n",1.0*cnt/len);
    }    
    return 0;
}

hdu 2132

View Code
#include<stdio.h>
int main()
{
    int n;
    __int64 f[100010]={0,1};
    for(__int64 i = 2; i <= 100000; ++i )
    {
        if(i%3==0)
           f[i] = f[i-1]+i*i*i;
        else
           f[i] = f[i-1]+i;        
    }    
    while(scanf("%d",&n), n >= 0 )
    {
        printf("%I64d\n",f[n]);                      
    }
    return 0;
}

hdu 2133

View Code
#include <iostream>
using namespace std;
int main()
{
    int d,m,y;
    int month[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
                      {0,31,29,31,30,31,30,31,31,30,31,30,31}};
    while(cin>>y>>m>>d)
    {
        int sum=d;
        int leap=y%4==0&&y%100!=0||y%400==0;
         for(int i=0;i<m;i++)
          sum+=month[leap][i];
        if(m==2)
         {
            if(leap==0&&d>28)
           {
             cout<<"illegal"<<endl;
             continue;
           }
           if(leap&&d>29)
           {
             cout<<"illegal"<<endl;
             continue;
           }
         }
         if((m==1||m==3||m==5||m==7||m==8||m==10||m==12)&&d>31)
         {
             cout<<"illegal"<<endl;
             continue;
         }
          if((m==4||m==6||m==9||m==11)&&d>30)
          {
              cout<<"illegal"<<endl;
              continue;
          }
          if(m==0||d==0)
          {
              cout<<"illegal"<<endl;
              continue;
          }
         for(int i=1;i<y;i++)
         {
            int leap=i%4==0&&i%100!=0||i%400==0;
            sum+=((365+leap)%7);
         }
            int k=sum%7;
            if(k==1) cout<<"Monday"<<endl;
            else if(k==2) cout<<"Tuesday"<<endl;
            else if(k==3) cout<<"Wednesday"<<endl;
            else if(k==4) cout<<"Thursday"<<endl;
            else if(k==5) cout<<"Friday"<<endl;
            else if(k==6) cout<<"Saturday"<<endl;
            else cout<<"Sunday"<<endl;
    }
}

hdu 2134

View Code
//R/r1=sqrt(1.5),r1/r2=sqrt(2)
#include<stdio.h>
#include<math.h>
int main()
{
    int r;
    while(scanf("%d",&r), r )
    {
          double r1 = 1.0*r/sqrt(3);
          double r2 = r1*sqrt(2);
          printf("%.3f %.3f\n",r1,r2);                      
    }     
    return 0;
}

hdu 2135

View Code
#include<stdio.h>
char a[11][11];
void change(int n,bool flag)
{
    char b[11][11];
    if(flag)   
    {  
       for(int i = 1; i <= n; ++i )
           for(int j = 1; j <= n; ++j )
               b[j][n-i+1] = a[i][j];              
    }
    else
    {
       for(int i = 1; i <= n; ++i  )
           for(int j = 1; j <= n; ++j ) 
               b[n-j+1][i] = a[i][j];
    }
    
    for(int i = 1; i <= n; ++i )
       for(int j = 1; j <= n; ++j )
           a[i][j] = b[i][j]; 
}
int main()
{
    int n, m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
          getchar();
          for(int i = 1; i <= n; ++i )
                  scanf("%s",a[i]+1);
          bool flag = true; //
          m %= 4;
          if( m < 0 ) flag = false, m = -m;
          while(m--) change(n,flag);    
      //    puts("-->") ;
          for(int i = 1; i <= n; ++i ) 
          {
              for(int j = 1; j <= n; ++j )
                 printf("%c",a[i][j]);
              putchar('\n');       
          }                  
    }    
    return 0;
}

hdu 2136

View Code
#include <stdio.h> 
#include <string.h>
#define N 1000001
int prime[N];
int main ()
{
    for ( int i = 0; i < N; i++ )
          prime[i] = -1;
    int num = 0;
    for ( int i = 2; i < N; i++ )
    {
          if ( prime[i] == -1 )
          {     num++;
                for ( int j = i; j < N; j += i ) 
                    prime[j] = num;    
          }
    } 
    int n;
    while ( scanf ( "%d", &n ) == 1  )
    {
          if ( n == 1 )   
              printf ( "0\n");      
          else
              printf ( "%d\n", prime[n] );
    }
    return 0;
}

hdu 2137

View Code
题意:把一个字符串(奇数个字符)按中间字符为轴逆时针旋转n次后,输出。

mark:2WA,n居然可以为负。。。

# include <stdio.h>
# include <string.h>


void out0(char str[], int len){puts (str) ;}
void out1(char str[], int len)
{
    int i, j ;
    for (i = len-1 ; i >=0 ; i--)
    {
        for (j = 0 ; j < i ; j++) putchar (' ') ;
        printf ("%c\n", str[i]) ;
    }
}
void out2(char str[], int len)
{
    int i, j ;
    for(i=len-1;i>=0;i--)
    {
        for (j = 0 ; j < len/2 ; j++) putchar (' ') ;
        printf ("%c\n", str[i]) ;
    }
}
void out3(char str[], int len)
{
    int i, j ;
    for(i = len-1 ; i >= 0 ; i--)
    {
        for (j = 0 ; j < len-1-i ; j++) putchar (' ') ;
        printf ("%c\n", str[i]) ;
    }
}
void out4(char str[], int len)
{
    int i ;
    for (i = len-1 ; i >= 0 ; i--) putchar (str[i]) ;
    putchar ('\n') ;
}
void out5(char str[], int len)
{
    int i, j ;
    for (i = 0 ; i < len ; i++)
    {
        for (j = 0 ; j < len-i-1 ; j++) putchar (' ') ;
        printf ("%c\n", str[i]) ;
    }
}
void out6(char str[], int len)
{
    int i, j ;
    for(i=0;i<len ;i++)
    {
        for (j = 0 ; j < len/2 ; j++) putchar (' ') ;
        printf ("%c\n", str[i]) ;
    }
}
void out7(char str[], int len)
{
    int i, j ;
    for(i = 0 ; i < len ; i++)
    {
        for(j = 0 ; j < i ; j++) putchar (' ') ;
        printf("%c\n", str[i]) ;
    }
}


int main ()
{
    int k ;
    char str[100] ;
    while (~scanf ("%s %d%*c", str, &k))
    {
        if (k < 0) k = (-k)%8, k = 8-k ;
        switch (k%8){
        case 0: out0(str, strlen(str)) ; break ;
        case 1: out1(str, strlen(str)) ; break ;
        case 2: out2(str, strlen(str)) ; break ;
        case 3: out3(str, strlen(str)) ; break ;
        case 4: out4(str, strlen(str)) ; break ;
        case 5: out5(str, strlen(str)) ; break ;
        case 6: out6(str, strlen(str)) ; break ;
        case 7: out7(str, strlen(str)) ; break ;
        }
    }
    return 0 ;
}

 

hdu 2138

View Code
#include<stdio.h>
#include<math.h>
#define N 10000000
bool vis[N+10]={0};
bool isp(int n )
{
     int t = (int)sqrt(n);
     for( int i = 2; i <= t; ++i )
         if(n%i==0) return false;
     return true;
}
int main()
{
    for(int i = 2; i <= N; ++i )
    {  
        if(!vis[i])
           for(int j = i+i; j <= N; j += i )
               vis[j] = true;                 
    }
    int n,x;
    while(scanf("%d",&n)!=EOF)
    {
          int num = 0;
          for(int i = 1; i <= n; ++i )
          { 
               scanf("%d",&x );
               if( x <= N )
               {
                   if( !vis[x] ) 
                     num++;
               }
               else if(isp(x))
                     num++;
          }
          printf("%d\n",num);
    }
    return 0;
}

hdu 2139

View Code
/*
数学题。

已知: 1^2+2^2+3^2+……+n^2 =n(n+1)(2n+1)/6                      —①
那么  1^2+2^2+3^2+……+n^2+……+(2n+1)^2 =(2n+1)(n+1)(4n+3)/3 —②
又有  2^2+4^2+6^2+……+(2n)^2 =4[1^2+2^2+3^2+……+n^2]=4*①   = 2n(n+1)(2n+1)/3 —③
设所求为S 比较②和③可知 S=②-③=(2n+1)(n+1)(4n+3)/3-2n(n+1)(2n+1)/3
=(2n+1)(n+1)(2n+3)/3 —④
因为S是2n+1项的和 把它一般化 则奇数项平方和一般公式Sn=n(n+1)(n+2)/6
*/

#include<stdio.h>
int main()
{
    __int64 n;
    while(scanf("%I64d",&n) != EOF )
    {
          __int64 ans = n*(n+1)*(n+2)/6; 
          printf("%I64d\n",ans);       
    }    
    return 0;
} 

 

posted @ 2013-04-16 10:19  yefeng1627  阅读(155)  评论(0编辑  收藏  举报

Launch CodeCogs Equation Editor