首页 > 试题广场 >

小易的升级之路

[编程题]小易的升级之路
  • 热度指数:36291 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
小易经常沉迷于网络游戏.有一次,他在玩一个打怪升级的游戏,他的角色的初始能力值为 a.在接下来的一段时间内,他将会依次遇见n个怪物,每个怪物的防御力为b1,b2,b3...bn. 如果遇到的怪物防御力bi小于等于小易的当前能力值c,那么他就能轻松打败怪物,并 且使得自己的能力值增加bi;如果bi大于c,那他也能打败怪物,但他的能力值只能增加bi 与c的最大公约数.那么问题来了,在一系列的锻炼后,小易的最终能力值为多少?

输入描述:
对于每组数据,第一行是两个整数n(1≤n<100000)表示怪物的数量和a表示小易的初始能力值.
然后输入n行,每行整数,b1,b2...bn(1≤bi≤n)表示每个怪物的防御力


输出描述:
对于每组数据,输出一行.每行仅包含一个整数,表示小易的最终能力值
示例1

输入

3 50
50 105 200
5 20
30 20 15 40 100

输出

110
205
推荐
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int gcd(int a,int b){
	int tmp;
	while(b){
		tmp = b; b = a % b ; a = tmp;
	}
	return a;
}
int main(){
	int n,a;
	while(scanf("%d%d",&n,&a) != EOF){
		for(int i = 0,x;i < n;++ i){
			scanf("%d",&x);
			if(x <= a) a += x;
			else a += gcd(x,a);
		}
		printf("%d\n",a);
	}
	return 0;
}
编辑于 2016-03-01 14:24:00 回复(14)
直接模拟打怪过程就可以了,不过很坑,示例里面怪物的防御力是一行输入并用空格分隔的,但提交之后根本不是这样,是换行输入的!
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while((line = br.readLine()) != null){
            String[] params = line.split(" ");
            int n = Integer.parseInt(params[0]);
            int a = Integer.parseInt(params[1]);
            for(int i = 0; i < n; i++){
                int b = Integer.parseInt(br.readLine());
                if(a >= b){
                    a += b;
                }else{
                    a += gcd(b, a);
                }
            }
            System.out.println(a);
        }
    }
    
    private static int gcd(int n, int m) {
        if(m == 0){
            return n;
        }
        return gcd(m, n % m);
    }
}

发表于 2022-01-13 11:21:57 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int n = sc.nextInt();
            int a = sc.nextInt();
            int[] nums = new int[n];
            for(int i = 0;i < n;i++){
                nums[i] = sc.nextInt();
                if(nums[i] <= a) a += nums[i];
                else a += method(a,nums[i]);
            }
            System.out.println(a);
        }
    }
    
    private static int method(int a ,int b){
        //辗转相除法
        while(b != 0){
            int r = a % b;
            a = b;
            b = r;
        }
        return a;
    }
}

发表于 2021-08-19 22:59:32 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int a = sc.nextInt();
            
            for(int i = 0;i<n;i++){
                int bi = sc.nextInt();
                if(bi < a){
                    a += bi;
                }else{
                    a += getDivisor(a,bi);
                }
            }
            
            System.out.println(a);
        }
    }
    
    public static int getDivisor(int c,int b){ //这时候的c一定是小于bi的
        long temp;
        if(b == 0)
            return c;
        else
            return getDivisor(b,c%b);
        
    }
}


发表于 2021-08-05 10:45:47 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int num = sc.nextInt();
            int pow = sc.nextInt();
            for(int i=0;i<num;i++){
                int number = sc.nextInt();
                if(pow > number) pow += number;
                else pow += gcd(pow,number);
            }
            
           System.out.println(pow); 
        }
    }
    
    static int gcd(int n1,int n2){
        if(n1 % n2 == 0) return n2;
        return gcd(n2,n1%n2);
    }
    
    
}

发表于 2021-08-04 15:51:54 回复(0)
//我去eclipse测试是对的,可是在这里不知道哪里错了
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
       Scanner sc=new Scanner(System.in);
       int n=sc.nextInt();
       int a=sc.nextInt();
       int[] b=new int [n];
        //获取输入判断
            for(int i=0;i<n;i++)
            {
                //获取对应的数值
                b[i]=sc.nextInt();
            }
        System.out.println(game(a,b));
        //关闭scanner
        sc.close();
    }
    public static int maxD(int a,int b)
    {
        for(int i=a;i>1;i--)
        {
            if(b%i==0 && a%i==0)
            {
                return i;
            }
        }
        return 1;
    }
    public static int game(int a,int[] b)
    {
        for(int i=0;i<b.length;i++)
        {
            if(a>=b[i])
            {
                a=a+b[i];
            }
            else
            {
                a=a+maxD(a,b[i]);
            }
        }
        return a;
    }
}
发表于 2021-04-06 16:28:15 回复(1)
求大佬解答,为什么这样不行??😭
import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException{
        int n,a;
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while((line=bf.readLine())!=null){
            String[] str1 = line.split(" ");
            n=Integer.parseInt(str1[0]);
            a=Integer.parseInt(str1[1]);
            int[] b=new int[n];
            String[] str2 = bf.readLine().split(" ");
            for(int i=0;i<n;i++){
                b[i]=Integer.parseInt(str2[i]);
            }
            for(int i=0;i<n;i++){
                if(a>=b[i])
                    a+=b[i];
                 else{
                     int p=a;
                     int q=b[i];
                     while(p%q!=0){
                         int temp=p %q;
                         p=q;
                         q=temp;
                     }
                  a+=q;
                 }
            }
            System.out.println(a);
        }
    }
}


发表于 2020-08-16 16:50:38 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int a = sc.nextInt();
            for(int i = 0; i < n; i++){
                int cur = sc.nextInt();
                if(a >= cur) a += cur;
                else a += maxGCD(a, cur);
            }
            System.out.println(a);
        }
        sc.close();
    }
    private static int maxGCD(int a, int cur){
        if(cur == 0) return a;
        return maxGCD(cur, a % cur);
    }
}

发表于 2020-08-15 14:30:21 回复(0)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SmallYiUp {
    public static void main(String args[]){
        Scanner sc =new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        while (sc.hasNext()) {
            int monsterNumber = sc.nextInt();
            int power = sc.nextInt();
            int[] moster = new int[monsterNumber];
            for (int i = 0; i < monsterNumber; i++) {
                moster[i] = sc.nextInt();
            }
            for (int i = 0; i < moster.length; i++) {
                if (power > moster[i]) {
                    power = power + moster[i];
                } else {
                    power = power + isPublicYueNum(power, moster[i]);
                }
            }
            list.add(power);
        }
        for (int i = 0;i<list.size();i++){
            System.out.println(list.get(i));
        }
    }
    public static int  isPublicYueNum(int xiaoYi,int monster){
        int num = 0;
        for (int i = xiaoYi;i>0;i--){
            if(xiaoYi%i==0&&monster%i==0){
                num = i;
                break;
            }
        }
        return num;
    }
}

发表于 2019-12-02 21:29:46 回复(0)
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int a = sc.nextInt();
            int[] b = new int[n];
            for (int i = 0; i < n; i++) {
                b[i] = sc.nextInt();
                if (b[i] <= a) {
                    a += b[i];
                } else {
                    a += gcd(a, b[i]);
                }
            }
            System.out.println(""+a);
        }
        sc.close();
    }
    private static int gcd(int a, int b) {
        if (b % a == 0) {
            return a;
        } else {
            return gcd(b % a, a);
        }
    }
}
编辑于 2018-09-26 17:01:57 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()){
            int n = sc.nextInt();
            int ambition = sc.nextInt();
            for (int i = 0; i < n; i++) {
                int temp = sc.nextInt();
                if(ambition > temp) ambition += temp;
                else ambition += gcd(ambition,temp);
            }
            System.out.println(ambition);
        }
    }
    public static int gcd(int x,int y){//求最大公约数
        while (y != 0){
            int temp = y;
            y = x % y;
            x = temp;
        }
        return x;
    }
}

编辑于 2017-10-12 15:29:38 回复(0)
import java.util.Scanner;
public class Main {
public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int[] a=new int[2];
        int c=0;
        
        for(int i=0;i<2;i++){
            a[i]=sc.nextInt();
        }
int n=a[0];
        int[] b=new int[n];
        for(int i=0;i<n;i++){
            b[i]=sc.nextInt();
        }
        
        for(int i=0;i<n;i++){
            if(a[1]>=b[i]){
                 a[1]=a[1]+b[i];
            }
            else   {
                c=GetGYS(a[1],b[i]);
                a[1]=a[1]+c;
            } 
        }
        System.out.println(a[1]);
    }
    public static int GetGYS(int n,int m)
{
if(m%n==0)
return n;
else return GetGYS(m%n,n);
}
}
//为什么总是通不过案例,谁能帮忙看一下,谢谢
发表于 2017-07-15 17:12:14 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            int c = in.nextInt();
            for (int i = 0; i < n; i++) {
                int b = in.nextInt();
                c += (c < b) ? getGcd(b, c) : b;
            }
            System.out.println(c);
        }
        in.close();
    }
    static int getGcd(int a, int b) {
        return (b == 0) ? a : getGcd(b, a % b);
    }
}
发表于 2017-06-13 18:14:31 回复(0)
import java.util.*;

public class nengli 
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while(in.hasNextInt())
{
Integer n = in.nextInt();
Integer a = in.nextInt();

for(int i=0;i<n;i++)
{
Integer b = in.nextInt();
if(a>=b)
{
a += b;
}
else
{
a += divisor(a,b);
}
}
System.out.println(a);
}

}

public static Integer divisor(Integer m,Integer n) {
if (m % n == 0) {
return n;
} else {
return divisor(n,m % n);
}
}
}

发表于 2016-09-11 22:16:46 回复(0)
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			int[] arr = new int[n + 1];
			arr[0] = sc.nextInt();
			for (int i = 1; i < n + 1; i ++ ) {
				arr[i] = sc.nextInt();
			}
			for (int i = 1; i < arr.length; i ++ ) {
				if(arr[i] <= arr[i - 1]) arr[i] += arr[i - 1];
				else arr[i] = arr[i - 1] + gongyue(arr[i], arr[i - 1]);
			}
			System.out.println(arr[n]);
		}
	}
	public static int gongyue(int x, int y) {
		int min = x > y ? y : x;
		int max = x == min ? y : x;
		while (max % min != 0) {
			int temp = max % min;
			max = min;
			min = temp;
		}
		return min;
	}
}

发表于 2016-09-09 13:37:10 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            int n = in.nextInt();
            int a = in.nextInt();
            int[] b = new int[n];
            for(int i = 0; i < n; i++){
                b[i] = in.nextInt();
            }
            for(int j = 0; j < n; j++){
                if(b[j] <= a){
                    a += b[j];
                }else{
                    a += GCD(a,b[j]);
                }
            }
            System.out.println(a);
        }
    }
    
    public static int GCD(int a, int b){
        int gcd = 1;
        for(int i = 2; i <= Math.min(a,b); i++){
            if((a%i == 0) && (b%i == 0))
                gcd = i;
        }
        return gcd;
    }
}

发表于 2016-08-31 09:31:04 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            int n = scan.nextInt();
            int a = scan.nextInt();
            for(int i = 0;i < n;i++){
                 int b = scan.nextInt();
                if(a >= b){
                    a = a + b;
                }else{
                    int c = a;
                    while(b % c != 0){
                        int temp = b % c;
                        b = c;
                        c = temp;
                    }
                    a = a + c;
                }
            }
            System.out.println(a);
        }
    }
}
编辑于 2016-08-01 12:19:29 回复(0)
import java.util.*;

public class Main {
	public static void main(String[] args) {
		//先构造对应的输入输出格式
		Scanner scanner=new Scanner(System.in);
		//获取输入判断
		while(scanner.hasNext())
		{
			//初始化
			int n=scanner.nextInt();
			int init=scanner.nextInt();
			int[] monster=new int [n];
			for(int i=0;i<n;i++)
			{
				//获取对应的数值
				monster[i]=scanner.nextInt();
				if(init>monster[i])
				{
					init+=monster[i];
				}
				else
				{
					//这时候一定monster大
					init+=GetGYS(init,monster[i]);
				}
			}
			System.out.println(""+init);
		}		
        //关闭scanner
		scanner.close();
	}
	
	//获取对应最大公约数
	public static int GetGYS(int n,int m)
	{
		if(m%n==0)
			return n;
		else
		{
			return GetGYS(m%n,n);
		}
		
	}
}


编辑于 2016-06-17 15:09:20 回复(3)