BestCoder Round #54 (div.2)(hdu5427,hdu5428,hdu5429,hdu5430)

1.A problem of sorting

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5427

解题思路:

官方题解:

选择任意喜欢的排序方法即可。注意名字中可能有空格。

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;

struct node{
    int year;
    string name;
}no[110];

bool cmp(node a,node b){
    return a.year > b.year;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        string strr;
        getchar();
        for(int i = 0; i < n; i++){
            getline(cin,strr);
            int l = strr.size();
            int year = 0;
            for(int j = l-4; j < l; j++)
                year = year*10+(strr[j]-'0');
            no[i].name = strr.substr(0,l-5);
            no[i].year = year;
        }
        sort(no,no+n,cmp);
        for(int i = 0; i < n; i++)
            cout<<no[i].name<<endl;
    }
    return 0;
}


2.The Factor

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5428

解题思路:

官方题解:

对于每一个数字,它有用的部分其实只有它的所有质因子(包括相等的)。求出所有数的所有质因子中最小的两个,相乘就是答案。如果所有数字的质因子个数不到两个,那么就是无解。时间复杂度O(n*sqrt(a))O(nsqrt(a))

大水题呀。。。比赛时,solve函数里面,i<nprime这位置,手痒,多打了一个等号,RE。。。泪奔呀。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

typedef long long ll;
int nprime,ans;
int vis[100005];
int prime[100005];
ll a[110];
vector<ll> v;

void getprime(){
    nprime = 0;
    memset(vis,0,sizeof(vis));
    memset(prime,0,sizeof(prime));
    for(int i = 2; i <= 100000; i++){
        int t = 100000/i;
        for(int j = 2; j <= t; j++){
            vis[i*j] = 1;
        }
    }
    for(int i = 2; i <= 100000; i++){
        if(!vis[i])
            prime[nprime++] = i;
    }
}

void solve(ll x){
    ans = 0;
    for(int i = 0; i < nprime; i++){
        if(x < prime[i])
            return;
        if(x % prime[i] == 0){
            while(x % prime[i] == 0){
                v.push_back((ll)prime[i]);
                ans++;
                x /= prime[i];
            }
        }
        if(ans >= 2)
            return;
    }
}

int main(){
    getprime();
    int T;
    scanf("%d",&T);
    while(T--){
        v.clear();
        int n;
        scanf("%d",&n);
        for(int i = 0; i < n; i++){
            scanf("%lld",&a[i]);
            solve(a[i]);
            if(ans == 0 && a[i] != 1)
                v.push_back(a[i]);
        }
        sort(v.begin(),v.end());
        if(v.size() <= 1)
            printf("-1\n");
        else
            printf("%lld\n",v[0]*v[1]);
    }
    return 0;
}



3.Geometric Progressio

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5429

解题思路:

http://blog.csdn.net/piaocoder/article/details/48246829

AC代码:

import java.math.BigDecimal;  
import java.util.Scanner;  
  
public class Main {  
    public static void main(String[] args){  
        Scanner sca = new Scanner (System.in);  
        BigDecimal a[] = new BigDecimal[110];  
        BigDecimal ZERO = BigDecimal.ZERO;  
        int T = sca.nextInt(),t = 1;  
        while(t <= T){  
            t++;  
            int n = sca.nextInt();  
            int sum = 0;  
            for(int i = 0; i < n; i++){  
                a[i] = sca.nextBigDecimal();  
                if(a[i].equals(ZERO))  
                    sum++;  
            }  
            if(n == 1){  
                System.out.println("Yes");  
                continue;  
            }  
            if(sum != 0){  
                if(sum == n)  
                    System.out.println("Yes");  
                else  
                     System.out.println("No");  
                continue;  
            }  
            int flag3 = 1;  
            for(int i = 1; i < n-1; i++){  
                if(!(a[i].multiply(a[i])).equals(a[i-1].multiply(a[i+1]))){  
                    flag3 = 0;  
                    break;  
                }  
            }  
            if(flag3 == 1)  
                System.out.println("Yes");  
            else  
                System.out.println("No");  
        }  
    }  
} 


4.Reflect

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5430

解题思路:

http://blog.csdn.net/piaocoder/article/details/48247811

AC代码:

#include <iostream>  
#include <cstdio>  
#include <cstring>  
using namespace std;  
  
typedef long long ll;  
const int maxn = 1000005;  
ll phi[maxn],n;  
  
void solve(){  
    memset(phi,0,sizeof(phi));  
    phi[1] = 1;  
    for(ll i = 2; i <= maxn-3; i++){  
        if(!phi[i]){  
            for(ll j = i; j <= maxn-3; j += i){  
                if(!phi[j])  
                    phi[j] = j;  
                phi[j] = phi[j]/i*(i-1);  
            }  
        }  
    }  
}  
int main(){  
    int T;  
    solve();  
    scanf("%d",&T);  
    while(T--){  
        int n;  
        scanf("%d",&n);  
        printf("%lld\n",phi[n+1]);  
    }  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值