BestCoder Round #54

【题目链接】:click here~~

HDU No.5427  A problem of sorting (简单排序)

代码:

/*
* Problem: HDU No.5427
* Running time: 0MS
* Complier: C++
* Author: javaherongwei
* Create Time: 11:48 2015/9/6 星期日
*/

#include <bits/stdc++.h>
using namespace std;

const int N=105;
char s1[N][N];
int a[N];
int b[N];

bool cmp(const int &x, const int &y){
    return a[x]>a[y];
}
char s2[N*10];

int main(){
    int k,m,n;
    int t;scanf("%d",&t);
    while(t--){
        scanf("%d",&n);getchar();
        for(int i=1; i<=n; i++){
            gets(s2);
            int len=strlen(s2);
            int id=len;
            while(s2[id]==' ') id--;int ix = id;
            while(s2[ix]!=' ') ix--;ix++;
            int num = sscanf(s2+ix, "%d",&k); // return the return value
            a[i]=k;
            ix--;
            s2[ix] = 0;
            strcpy(s1[i],s2);
            b[i]=i;
        }
        sort(b+1, b+1+n, cmp);
        for(int i=1; i<=n; i++) puts(s1[b[i]]);
    }
    return 0;
}

HDU No.5428   The Factor (模拟,选择最小的两个因子)

代码:

/*
* Problem: HDU No.5428
* Running time: 0MS
* Complier: C++
* Author: javaherongwei
* Create Time: 12:13 2015/9/6 星期日
*/

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N=1e6+10;
int num[N];
int n,m,ll;

void get(int a){ // get the factor
    for(int i=2; (LL)i*i<=(LL)a; ++i){
        while(a%i==0){
            num[++ll]=i;
            a/=i;
        }
    }
    if(a>1) num[++ll]=a;
}

int main(){
    int t;scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        ll=0;
        for(int i=1; i<=n; ++i){
            scanf("%d",&m);get(m);
        }
        if(ll<2){
            puts("-1");
            continue;
        }
        sort(num+1,num+1+ll);
        printf("%I64d\n",(LL)num[1]*num[2]); // get the min factor
    }
    return 0;
}


HDU 5429  Geometric Progression (判断一个序列是否是等比序列 +java模拟)

代码:

/*
* Problem: HDU No.5429
* Running time: 380MS
* Complier: JAVA
* Author: javaherongwei
* Create Time: 17:05 2015/9/6 星期日
*/

import java.util.*;
import java.io.*;
import java.math.*;
public class Main 
{
    public static void main(String args[])
    {
      Scanner cin=new Scanner(new BufferedInputStream(System.in));
      int t=cin.nextInt();
      BigInteger fac[]= new BigInteger[105];
      BigInteger f1,f2;
      while(t-->0)
      {
          int n=cin.nextInt();
          int sum=0;
          for(int i=0; i<n; ++i)
          {
              fac[i]=cin.nextBigInteger();
              if(fac[i].compareTo(BigInteger.ZERO)==0) sum++;
          }
          if(sum!=0)
          {
              if(sum==n) System.out.println("Yes");
              else System.out.println("No");
              continue;
          }
          int ok=0;
          for(int i=1; i<n-1; ++i)
          {
               if(fac[i-1].multiply(fac[i+1]).compareTo(fac[i].multiply(fac[i]))!=0)
               {
                       ok=1;
                       break;
               }
          }
          if(ok==1) System.out.println("No");
          else System.out.println("Yes");
      }
    }
}


HDU 5430: Reflect(几何趣题)
【思路+题解】

BestCoder Round #54_第1张图片

代码:

/*
* Problem: HDU No.5430
* Running time: 374MS
* Complier: C++
* Author: javaherongwei
* Create Time: 17:05 2015/9/6 星期日
*/
//solve one:
    
#include <bits/stdc++.h>
using namespace std;
#define N 1000005
int dp[1000005];
int phi[N];
void get() // get the prime factor 
{
    int i,j;
    memset(phi,0,sizeof(phi));
    phi[1]=1;
    for(i=2;i<N;i++)
    {
        if(phi[i]==0)
        {
            for(j=i;j<N;j+=i)
            {
                if(phi[j]==0) phi[j]=j;
                phi[j]-=phi[j]/i;
            }
        }
    }
    return ;
}
void init()
{
    dp[1]=1;
    dp[2]=2;
    for(int i=3;i<=1000000;i++)
    {
        dp[i]=phi[i+1];
    }
}
int main()
{
    int i,j,k,m,n,ans;
    get();
    init();
    while(cin>>m)
    {
        while(m--)
        {
            cin>>n;
            cout<<dp[n]<<endl;
        }
    }
    return 0;
}

//solve two:
    
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int ans=0;
        for(int i=1; i<=n; i++)
            if(gcd(i,n+1)==1) ans++;
        printf("%d\n",ans);
    }
    return 0;
}
 


你可能感兴趣的:(BestCoder#54)