输入包括两行,第一行包含两个整数n和A(1 ≤ n ≤ 200, 1 ≤ A ≤ 1,000,000,000) 第二行包括n个整数,表示牛牛依次遇到的草料堆大小a_i(1 ≤ a_i ≤ 1,000,000,000)
输出一个整数,表示牛牛最后的大小。
5 1 2 1 3 1 2
4
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int A = scanner.nextInt(); HashSet<Integer> set = new HashSet<Integer>(); for(int i=0;i<n;i++){ set.add(scanner.nextInt()); } while(set.contains(A)){ A*=2; } System.out.println(A); } }
#include<bits/stdc++.h> typedef long long ll; using namespace std; const int inf = 0x3f3f3f3f; const ll INF = (ll)inf*inf; const int maxn = 55; int main() { int n,A;cin>>n>>A; int x; for(int i=0;i<n;i++){ scanf("%d",&x); if(x==A)A+=x; } cout<<A<<endl; return 0; }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Main { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String string = ""; while ((string = bufferedReader.readLine()) != null) { String[] firstLine = string.split(" "); String[] secondLine = bufferedReader.readLine().split(" "); ArrayList<Long> arrayList = new ArrayList<>(); for (int i = 0; i < secondLine.length; i++) { arrayList.add(Long.parseLong(secondLine[i])); } System.out.println(boomingNiu(arrayList, Long.parseLong(firstLine[1]))); /* System.out.println(); Long a = 128l; Long b = 128l; System.out.println(a == b);*/ /*因为Long中有一个静态的内部类LongCache,专门用于缓存-128至127之间的值,一共256个元素。 如果仅仅是缓存下来而不去使用那么就没有任何意义。valueOf(long l)就是使缓存派上用场的方法,它会判断传入的参数是否在-128-127之间, 如果是则直接从缓存中返回对应的引用,否则新创建一个Long的实例。 128l==128l return false. 所以,最好还是使用equals进行比较。也可以直接用.longValue()比较*/ } } public static Long boomingNiu(ArrayList<Long> arrayList, Long initailA) { Long res = initailA; for (int i = 0; i < arrayList.size(); i++) { if (arrayList.get(i).equals(res)) { res = res * 2; } } return res; } }第一次知道Long在-127~128可以直接比较,大于了不能这么比。
import java.util.*; public class quanguo43 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); //表示草料个数几个; int m=sc.nextInt(); //表示初始大小; TreeSet <Integer> tree=new TreeSet<Integer>(); for(int i=0;i<n;i++){ tree.add(sc.nextInt()); } //tree.contains(m);也可以; Iterator it=tree.iterator(); while(it.hasNext()){ if((int)(it.next())==m){ m*=2; } } System.out.println(m); } sc.close(); } }
importjava.util.Scanner;publicclassMain {publicstaticvoidmain(String[] args) {Scanner in = newScanner(System.in);intn=in.nextInt();intx=in.nextInt();inta[]=newint[n];for(inti=0;i<n;i++){a[i]=in.nextInt();}intsum=WedgeCount(a,x);System.out.println(sum);}publicstaticintWedgeCount(inta[],intx){intcount=x;for(inti=0;i<a.length;i++){if(a[i]==count) {count+=a[i];}}returncount;}}
import java.util.Scanner; public class Main{ public static void main(String args[]) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int A = scanner.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } for(int i = 0; i < n; i++) { if(arr[i] == A) { A *= 2; } } System.out.println(A); } }
}
<?php
header("charset=utf-8");
$s = trim(fgets(STDIN));
$d = explode(" ",$s);
$n = $d[0];
$A = $d[1];
// var_dump($n);
// var_dump($A);
$i = 0;
if($n=1&& 1<=$A && $A<=1000000000){
$c = trim(fgets(STDIN));
$f = explode(" ",$c);
for($i=0; $i<$n; $i++){
$arr[$i]=$f[$i];
}
for($i=0; $i<$n; $i++){
if($arr[$i] == $A){
$A=$A*2;
}
}
echo $A;
return$A;
}else{
$n = trim(fgets(STDIN));
$A = trim(fgets(STDIN));
}
?>
添加笔记
#include <iostream> #include <string> #include <vector> #include <algorithm> int main() { using namespace std; int n; long long a; while (cin >> n >> a) { for (int i = 0; i < n; i++) { long long num; cin >> num; if (num == a) { a *= 2; } } cout << a << endl; } return 0; }
// 也太简单了 import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int A = in.nextInt(); for(int i=0;i<n;i++){ int now = in.nextInt(); if(now==A) A = 2*A; } System.out.print(A); } }