首页 > 试题广场 >

扭蛋机

[编程题]扭蛋机
  • 热度指数:11915 时间限制: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
#include <stdio.h>
int fac(int n)
{
    if (n % 2 == 0 && n != 0)
    {
        fac((n - 2) / 2);
        printf("%d", 3);
    }
    else if (n % 2 != 0 && n != 0)
    {
        fac((n - 1) / 2);
        printf("%d", 2);
    }
    return 0;
}
int main()
{
    int N = 0;
    scanf("%d", &N);
    fac(N);
    return 0;
}

发表于 2024-04-27 19:18:24 回复(0)
#include <stdio.h>
#include <stdlib.h>

int eggmac1(int a)
{
    a = a - 1;
    a = a / 2;
    return a;
}                       //扭蛋机一号

int eggmac2(int a)
{
    a = a - 2;
    a = a / 2;
    return a;
}                      //扭蛋机二号

int main() {
    int n;
    scanf("%d" , &n);
    int A[100] = {0};
    int i = n , j = 0;         //备份
    while(n >= 0)
    {
        if(i == 2)
        {
            A[j] = 3;
            break;
        }
        else if(i == 1)
        {
            A[j] = 2;
            break;
        }
        if(i % 2 == 0) //能被2整除则进入扭蛋机2号
        {
            i = eggmac2(i);
            A[j] = 3;
            j++;
        }
        else{          //否则进一号
            i = eggmac1(i);
            A[j] = 2;
            j++;
        }
    }
    for(i = j ; i >= 0 ; i--)
    {
        printf("%d" , A[i]);
    }
    return 0;
}
编辑于 2024-04-17 14:48:08 回复(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)