牛牛可以进行的操作是:将数组中的任意一个数改为这个数的两倍。
这个操作的使用次数不限,也可以不使用,并且可以对同一个位置使用多次。
数据范围:数组大小满足 ,数组中的数满足
输入一个正整数N (N <= 50) 接下来一行输入N个正整数,每个数均小于等于1e9.
假如经过若干次操作可以使得N个数都相等,那么输出"YES", 否则输出"NO"
2 1 2
YES
3 1 2 3
NO
#include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { vector<int> array; int in, a; cin >> in; int count = 0; for (int i = 0; i < in; i++) { cin >> a; array.push_back(a); //if (0== (a&(a - 1))) // count++; } std::vector<int>::iterator biggest = std::max_element(std::begin(array), std::end(array)); for (auto s:array) { if (*biggest%s == 0) { int temp = *biggest / s; if (0 == (temp&(temp - 1))) count++; } } if (count == in) cout << "YES" << endl; else cout << "NO" << endl; }
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
bool iseq(int a[],int len){
sort(a,a+len);
bool flag=true;
for(int k=1;k<len;k++){
if(a[k]%a[k-1]!=0){
flag=false;
break;
}
}
if(flag) return true;
return false;
}
int main(){
int n;
cin>>n;
int a[50];
for(int i=0;i<n;i++){
cin>>a[i];
}
if(iseq(a,n)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
import java.util.Scanner;
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i] = scanner.nextInt(); } for(int i=1;i<n;i++){ int small, big; if(a[i]>a[i-1]){ small = a[i-1]; big = a[i]; }else{ small = a[i]; big = a[i-1]; } int remainder = big % small; int quotient = big / small; if(remainder!=0||(quotient&(quotient-1))!=0) { System.out.println("NO"); return; } } System.out.println("YES"); } }
#include<iostream>
#include<cstring>
using namespace std;
int main(){
int n;
int a[51];
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
while(a[i] % 2 == 0) a[i] /= 2;
}
for(int i = 1; i < n; i++){
if(a[i] != a[i-1]){
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
return 0;
}
#include <bits/stdc++.h> using namespace std; const int N = 55; int n, a[N]; bool check(int ub) { for(int i = 0; i < n; i++) { if(ub % a[i] || __builtin_popcount(ub / a[i]) > 1) return false; } return true; } int main() { scanf("%d", &n); int mx = 0; for(int i = 0; i < n; i++) { scanf("%d", &a[i]); mx = max(mx, a[i]); } if(mx&1) mx <<= 1; bool flag = false; while(mx <= INT_MAX >> 1) { if(check(mx)) { flag = true; break; } mx <<= 1; } if(flag) puts("YES"); else puts("NO"); return 0; }
#include <iostream>
using namespace std;
bool judge(int a, int b) {
if (a != b) { //不相等且除不尽,首先排除
int mod = a > b ? a % b : b % a;
if (mod) {
return false;
}
}
if (a == b) {
return true;
}
int n = a > b ? a / b : b / a;
while (n > 1) {
if (n % 2 == 0) {
n /= 2;
} else {
return false;
}
}
return true;
}
int main(int argc, const char * argv[]) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
string ok = "YES";
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (!judge(arr[i], arr[j])) {
ok = "NO";
break;
}
}
if (ok == "NO") {
break;
}
}
cout << ok;
return 0;
}
用了几个例子思考了一下,操作只能乘以2或者不乘且次数不限,要求所有数字最终都相同。假设所有数字最终已经相同了,如x,那么它们肯定是2的某个次幂,那么变成x之前的任何数,都是x的因子。
原先更大的数,越接近最终的x,通过排序,就使得“因子”具有了传递性。遍历所有元素,如果前面的数不是后面的数的因子,那就不可以得到全部是x的序列,反之即可。
#include <bits/stdc++.h> using namespace std; const int N = 60; int a[N], n; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int i = 0; i < n - 1; i++) { if (a[i + 1] % a[i] != 0) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 处理输入 int n = in.nextInt(); long[] nums = new long[n]; for (int i = 0; i < n; i++) nums[i] = in.nextLong(); // 排序找出最大值 Arrays.sort(nums); for (int i = 0; i < n - 1; i++) { // 取余,如果!=0,直接return if (nums[i] != nums[n - 1] && nums[n - 1] % nums[i] != 0) { System.out.println("NO"); return; } } // 当循环走完,则证明可以变换 System.out.println("YES"); } }
#include <iostream> using namespace std; int main() { int n; cin>>n; int a[50]; for(int i=0;i<n;i++){ cin>>a[i]; } bool result = true; for(int i=0;i<n;i++){ while ((a[i] & 1) == 0) { a[i] = a[i] >> 1; } if (i > 0 && a[i] != a[i-1]) { result = false; break; } } printf(result?"YES":"NO"); }
bool yesorno(int x ,int y) { if((y/x)%2 == 0 || x==y) { return true; } return false; } int main() { int n; cin>>n; vector<int> tmp(n); for(int i = 0;i<n;i++) { cin>>tmp[i]; } sort(tmp.begin(),tmp.end()); for(int i = 0;i < n-1;i++) { if(!yesorno(tmp[i], tmp[i+1])) { cout<<"NO"<<endl; return 0; } } cout<<"YES"<<endl; return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int length = sc.nextInt(); if(length == 1){ System.out.println("YES"); return; } int[] nums = new int[length]; for(int i = 0; i < length; i++){ nums[i] = sc.nextInt(); } String str = "YES"; // 两层循环暴力求解 for(int i = 0; i < length; i++){ int temp = nums[i]; for(int j = i+1; j < length; j++){ if(temp < nums[j]){ if((nums[j] / temp) %2 != 0){ str = "NO"; break; } }else if(temp > nums[j]){ if((temp / nums[j]) %2 != 0){ str = "NO"; break; } }else{ continue; } } if(str.equals("NO")){ break; } } System.out.println(str); } }
import java.util.*; public class Main { public static void main(String[] args) { boolean flag=true; Scanner scanner=new Scanner(System.in); int base = Integer.parseInt(scanner.nextLine()); String s = scanner.nextLine(); String[] s1 = s.split(" "); if(!("".equals(s))){ List<Integer> tem=new ArrayList<>(); for (String s2 : s1) { int anInt = Integer.parseInt(s2); tem.add(anInt); } //获取得数组中最小数的最小质数 int min = getMinData(Collections.min(tem)); for (String str : s1) { int i = Integer.parseInt(str); //排除自身 if(min==i) continue; //如果有一个最小质数不相等则改变falg为false if((getMinData(i))!=min){ flag=false; } } } System.out.println(flag?"YES":"NO"); } //递归获取每个数的最小质数 public static int getMinData(Integer para){ if(!((para/2)==0)&& ((para%2)==0)){ return getMinData(para/2); } return para; } }
#include <stdio.h> #include <stdlib.h> int array_len = 0; int *array_adr = NULL; int max_num = 0; int temp = 0; int main(void) { scanf("%d\r\n",&array_len); array_adr = (int *)malloc(array_len*sizeof(int)); if(array_adr == NULL){ return -1; }else for(int i = 0;i < array_len; i++){ scanf("%d\r\n",&array_adr[i]); } max_num = array_adr[array_len-1]; for(int i = 0; i < array_len-1; i ++){ temp = array_adr[i]; while(temp != max_num){ temp = 2*temp; if(temp > max_num){ printf("NO\r\n"); exit(0); } } } printf("YES\r\n"); return 0; }