滴滴9.17测开笔试
60分选择题
2道编程题(40分):0.91 + 1
/*
定义函数f(x)表示 x 在二进制表示下 1 的个数。例如,9的二进制表示为1001,则f(9)=2。
一个特工获取了一个重要情报,这个情报为一个非负整数x。他在传递情报的时候对这个数进行了处理,
他将这个整数x乘上f(x) 后再发送出去。现在你得到了这份情报,你想知道处理前的数可能为多少?
*/
import java.util.*;
// 过了91%
class Main1
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
long y = cin.nextLong();
if(y==0){
System.out.println(0);
System.out.println(0);
return;
}
int res = 0;
List<Long> list = new ArrayList<>();
for(int i=1; i<=64; i++){
if(y%i != 0){
continue;
}
int count = helper(y/i);
if(count == i){
res ++;
list.add(y/i);
}
}
System.out.println(res);
for(int i=0; i<list.size(); i++){
System.out.print(list.get(list.size()-1-i));
if(i != list.size()-1){
System.out.print(" ");
}
}
}
static int helper(long x){
int res = 0;
while(x!=0){
if(x%2==1){
res ++;
}
x /= 2;
}
return res;
}
}
/*
小昱做了很久的实验得到了一个用正整数表示的实验数据,并记录在了纸上。但是由于做完实验太过激动,他一不小心把墨水打翻溅在了纸上,导致数据中一些位置上的数字看不清楚。他仍记得这个数据有以下三个特征:
1. 这个数是正整数,且没有前导零(即数的最高位不是0)
2. 这个数任意两个相邻数位的数字不同
3. 这个数可以被3整除
他现在很关心在满足以上特征的条件下,这个数字最小为多少。
*/
import java.util.*;
// 全过
class Main2
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
String str = cin.nextLine();
char[] chs = str.toCharArray();
int total = 0;
int count = 0;
int len = str.length();
for(int i=0; i<len; i++){
if(chs[i]=='?'){
count++;
}else{
total += (chs[i]-'0');
}
}
if(count==0){
System.out.println(str);
return;
}
for(int i=0; i<len; i++){
if(chs[i]!='?'){
continue;
}
if(count==1){
int left = -1, right = -1;
if(i>0){
left = chs[i-1]-'0';
}else{
left = 0;
}
if(i<len-1){
right = chs[i+1]-'0';
}
int x = total%3;
x = (3-x)%3;
while(left==x || right==x){
x += 3;
}
chs[i] = (char)('0'+x);
break;
}
count--;
if(i==0){
if(chs[i+1]=='1'){
chs[i] = '2';
total += 2;
}else{
chs[i] = '1';
total += 1;
}
}else{
if(chs[i-1]=='0' || chs[i+1]=='0'){
if(chs[i-1]=='1' || chs[i+1]=='1'){
chs[i] = '2';
total += 2;
}else{
chs[i] = '1';
total += 1;
}
}else{
chs[i] = '0';
}
}
}
for(int i=0; i<len; i++){
System.out.print(chs[i]);
}
}
}
传音控股公司福利 335人发布

