首页 > 试题广场 >

扭蛋机

[编程题]扭蛋机
  • 热度指数:11764 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
22娘和33娘接到了小电视君的扭蛋任务:
一共有两台扭蛋机,编号分别为扭蛋机2号和扭蛋机3号,22娘使用扭蛋机2号,33娘使用扭蛋机3号。
扭蛋机都不需要投币,但有一项特殊能力:
扭蛋机2号:如果塞x(x范围为>=0整数)个扭蛋进去,然后就可以扭到2x+1个
扭蛋机3号:如果塞x(x范围为>=0整数)个扭蛋进去,然后就可以扭到2x+2个
22娘和33娘手中没有扭蛋,需要你帮她们设计一个方案,两人“轮流扭”(谁先开始不限,扭到的蛋可以交给对方使用),用“最少”的次数,使她们能够最后恰好扭到N个交给小电视君。

输入描述:
输入一个正整数,表示小电视君需要的N个扭蛋。


输出描述:
输出一个字符串,每个字符表示扭蛋机,字符只能包含"2"和"3"。
示例1

输入

10

输出

233

备注:
1<=N<=1e9
// golang
package main
 
import (
    "fmt"
)
func main()  {
    var niudan int
    fmt.Scanln(&niudan)
    var fanan  []int
    ifniudan == 1 {
        fmt.Printf("%d", 2)
    }elseifniudan == 2 {
        fmt.Printf("%d", 3)
    }else{
        for{
            ifniudan % 2 == 0 {
                niudan = (niudan - 2) / 2
                fanan = append(fanan, 3)
 
            } else{
                niudan = (niudan - 1) / 2
                fanan = append(fanan, 2)
            }
            ifniudan == 0 {
                break
            }
        }
        fori := len(fanan) - 1; i >= 0; i -- {
            fmt.Printf("%d", fanan[i])
        }
    }
}
发表于 2019-09-10 12:36:04 回复(1)
#include <iostream>
#include<string>
usingnamespacestd;
intmain() {
    intresult;
    string s = "";
    cin>>result;
    while(result!=0)
    {
        if(result%2==0)
        {
            result=(result-2)/2;
            s="3"+s;
        }
        else
        {
            result=(result-1)/2;
            s="2"+s;
        }
    }
    cout<<s<<endl;
    return0;
}
发表于 2019-08-31 16:00:21 回复(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();
                StringBuilder res = new StringBuilder();
                while(num != 0){
                    if(num % 2 == 0){
                        num = (num - 2) / 2;
                        res.insert(0, "3");
                    }else{
                        num = (num - 1) / 2;
                        res.insert(0, "2");
                    }
                }
                System.out.println(res);
            }
    }
}
发表于 2019-06-23 12:32:36 回复(1)
我怎么感觉题目有问题,还是我理解能力差,轮流扭的意思难道不是232323交替吗?为什么还有233
发表于 2022-03-09 10:28:34 回复(1)
首先,这个题目有很大的问题:
1. 题目中有说“轮流扭”, 但我们看答案发现并不是每个人扭一次, 可以一个人连续多次扭。
2. 题目中没有规定每次扭蛋是不是必须把手头的蛋全部用完
这很容易让人会错题意
发表于 2022-09-21 19:10:43 回复(1)
🙃轮流不是你一次我一次的意思嘛 题都没看懂。。
发表于 2022-02-26 20:59:15 回复(0)
n = int(input())
o = []
while n:
    o.append('3' if n%2 == 0 else '2')
    n = (n-1)//2
print(''.join(o[::-1]))

发表于 2019-09-09 15:52:50 回复(1)
def helper(num):
    res = ""
    while num>0:
        if ((num-2)/2)%1==0:
            res = '3'+res
            num = (num-2)/2
        if ((num-1)/2)%1==0:
            res =  '2'+res
            num = (num-1)/2
    return res
if __name__ == "__main__":
    obj = int(input())
    ans = helper(obj)
    print(ans)

发表于 2019-05-07 13:37:44 回复(3)
我怎么觉得题描述不对!
两人“轮流扭”, ababababab   或  babababa 这样换着来吗?
为什么是不能轮流来的,为什么要说两人“轮流扭” 呢?
发表于 2022-04-14 08:56:54 回复(0)
这道题可以简化成走方格
初始位置是x=0,然后每步有两种选择
1、第一种选择是走到编号为 2*x+1 的方格,代号为 ‘2’
2、第一种选择是走到编号为 2*x+2 的方格,代号为 ‘3’
求一种可以正好走到 编号为 n 的方式
# 逆推法:根据最后一步的奇偶,判断前一步的选择
n = int(input())
res = ''
while n > 0:
    if (n-2) % 2:
        n = (n-1) // 2
        res = '2' + res
    else:
        n = (n-2) // 2
        res = '3' + res
print(res)



发表于 2019-08-22 16:01:14 回复(0)
✭头像
x = eval(input())
ls = []
while(x):#从后往前逆推
    if(x%2==0):#若x为偶数,则最后一步肯定是扭蛋机3
        ls.append(3)
        x = (x-2)/2
    else:#若x为奇数,则最后一步肯定是扭蛋机2
        ls.append(2)
        x = (x-1)/2
for i in ls[::-1]:
    print(i, end='')

发表于 2019-08-26 21:52:08 回复(3)
#include <iostream>
#include <stack>
#include <string>

using namespace std;

int N;
stack<string> s_sta, t_sta;

bool dfs(int, int);

int main(int argc, char **argv) {
    cin >> N;
    if (dfs(0, 0)) {
        for (; !s_sta.empty(); t_sta.push(s_sta.top()), s_sta.pop());
        for (; !t_sta.empty(); cout << t_sta.top(), t_sta.pop());
    }
    cout << endl;
    return 0;
}

bool dfs(int i, int sum) {
    if (sum == N) return true;
    if (sum > N) return false;
    for (int j = sum; j >= 0; j--) {
        s_sta.push("3");
        if (dfs(i + 1, sum - j + (2 * j + 2))) return true;
        s_sta.pop();
    }
    for (int j = sum; j >= 0; j--) {
        s_sta.push("2");
        if (dfs(i + 1, sum - j + (2 * j + 1))) return true;
        s_sta.pop();
    }
    return false;
}
# 这题本身充满了问题
(1)“x范围为>=0正整数”,x到底tm能不能为0?题目后半部分印证了“x为 >= 0的整数”。
(2)同样次数最少的情况下,串到底是否唯一?从题目来看,并不要求唯一;从样例来看,要求唯一。
# 我的答案的问题
(1)我采用的dfs并不是一个好办法,大家的根据奇偶判断的方法更合适,但我认为不应该是错的。
发表于 2019-08-03 10:51:31 回复(2)
#include<iostream>
#include<string>
using namespace std;

int main(){
    int n,i;
    int a[2000];   //存放得出的扭蛋机号码
    while(scanf("%d",&n)!=-1){
        i=0;
        while(n>0){   //n>0时,都在while循环内运算
            if(n%2==0){
                //除以2余0,则知道是3号机扭的
                n=(n-2)/2;
                a[i]=3;  //最后的扭蛋机为3号
                i++; //i加1,a[2000]存放倒数第二个数据
            }
            else{
                n=(n-1)/2;
                a[i]=2;
                i++;
            }
        }
        i--; //正序输出扭蛋机顺序
        for(;i>=0;--i)
            cout<<("%d",a[i]);
    }
}

发表于 2021-07-16 10:26:36 回复(1)
这道题有没有可能每次得到的蛋能只取一部分再拿去投?感觉用倒推法默认每次得到的蛋都全部拿去产新蛋,能证明全部投得到的结果一定比部分投优吗?
发表于 2021-05-17 15:26:36 回复(0)
原来是这样的
import java.util.*;
public class bili23 {
	public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        StringBuffer str = new StringBuffer();
        while(n > 0) {
            if(n%2 == 0) {
            	n = (n - 2)/2;
            	str.append("3");
            }else {
            	n = (n - 1)/2;
            	str.append("2");
            }
        }
        str.reverse();
        System.out.println(str);
        sc.close();
    }
}

发表于 2019-08-10 15:51:36 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,x=0;
    cin>>n;
    string s="";
    while(n)
    {
        if(n%2==0)
        {
            n=(n-2)/2;
            s+='3';
        }
        else
        {
            n=(n-1)/2;
            s+='2';
        }
    }
    reverse(s.begin(),s.end());
    cout<<s<<endl;
    return 0;
}

发表于 2019-07-23 08:28:14 回复(1)
这是什么意思啊。。。
发表于 2019-05-11 18:35:08 回复(0)
不懂就问,
为什么想要3个扭蛋的时候不能是23
给0个2号扭蛋机(-0+1),给0个3号扭蛋机(-0+2)
结果不也是3吗?
发表于 2023-08-08 23:43:26 回复(1)
利用递归思想,结束条件为扭蛋数为1或2,递归条件:奇数个蛋就用2号机,偶数个蛋就用3号机。
#include <bits/stdc++.h>
using namespace std;

string fun(int i)
{
    if(i == 1)
    {
        return "2";
    }
    else if(i == 2)
    {
        return "3";
    }
    else if(i%2 == 0)
    {
        //是偶数,那么前一步一定是3号
        return fun((i-2)/2)+"3";
    }
    else if(i%2 == 1)
    {
        //是奇数,那么前一步一定是2号
        return fun((i-1)/2)+"2";
    }
    return "";
}

int main()
{
    int n;
    cin >> n;
    cout << fun(n);
    
    return 0;
}


发表于 2022-02-15 10:16:42 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
// 宏定义 -- 用来定义各种符号常量
#define not !
#define INIT_CAPACITY 8
#define InitStack(S) __InitStack(S, INIT_CAPACITY);

// 宏函数
#define SWAP(a, b) { typeof(a) t = a; a = b; b = t; }

typedef enum { OK = 1, ERROR = -1, OVERFLOW = -2 } Status;

// -------------------- 顺序栈的存储表示与实现 --------------------
typedef char SElemType;

typedef struct {
  SElemType* base;
  SElemType* top;
  size_t capacity;
} SqStack;

Status __InitStack(SqStack* S, int initialCapacity) {
  if (initialCapacity < 1) {
    fprintf(stdout,
            "InitStack ERROR: The initialCapacity %d Must be > 0!", initialCapacity);
    return ERROR;
  }
  if (!((*S).base = (SElemType*) malloc(initialCapacity * sizeof(SElemType)))) {
    fprintf(stdout, "InitStack Memory Overflow: %s\n", strerror(errno));
    exit(OVERFLOW); // abort
  }
  (*S).top = (*S).base;
  (*S).capacity = initialCapacity;
  return OK;
}

bool StackEmpty(SqStack* S) {
  return (*S).top == (*S).base;
}

bool StackFull(SqStack* S) {
  return (*S).top - (*S).base == (*S).capacity;
}

size_t StackLength(SqStack* S) {
  return (*S).top - (*S).base;
}

void __large_capacity(SqStack* S, float factor) { // factor == 倍增因子
  if (!((*S).base = (SElemType*)
        realloc((*S).base, (S->capacity * factor) * sizeof(SElemType)))) {
    fprintf(stdout, "__large_capacity Memory Overflow: %s\n", strerror(errno));
    exit(OVERFLOW);
  }  
  (*S).top = (*S).base + (*S).capacity;
  (*S).capacity *= factor;
}

Status Push(SqStack* S, SElemType e) {
  if (StackFull(S))
    __large_capacity(S, 1.5); // 弹性扩容
  
  *(*S).top++ = e;
  return OK;
}

Status Pop(SqStack* S, SElemType* e) {
  if (StackEmpty(S)) {
    fputs("InitStack ERROR: The stack is empty!\n", stdout);
    return ERROR;
  }
  *e = *--(*S).top;
  return OK;
}

Status DestroyStack(SqStack* S) {
  free((*S).base);
  (*S).top = NULL;
  return OK;
}
// -------------------- 顺序栈的存储表示与实现 --------------------

int main(const int argc, const char* const argv[]) {
  int n;
  fscanf(stdin, "%d", &n);
  
  SqStack S;
  InitStack(&S);
  
  while (n) {
    Push(&S, n & 1 ? '2' : '3');
    n = n & 1 ? (n - 1) >> 1 : (n - 2) >> 1;
  }
  
  char ch;
  while (not StackEmpty(&S)) {
    Pop(&S, &ch);
    fprintf(stdout, "%c", ch);
  }
  
  return DestroyStack(&S), 0;
}

发表于 2021-08-08 15:16:40 回复(0)

热门推荐

通过挑战的用户

查看代码