uva 11384 Help is needed for Dexter

点击打开链接uva 11384

思路:找规律
分析:
1 题目说给定一个小于10^9的数,现在有n个数要求经过最少的步骤使得这个序列的所有数都为0,求这个最少的步骤
2 很明显的找规律题,题目明确说明每一次可以选择任意个的数减去一个正整数,那么我们看以下这个例子
1 2 3 4 5 6 = (1 2 3 (4 5 6)-4) +1 = (1 2 3 0 1 2) +1
那么我们根据题目的意思我们可以知道每一次可以选择多个相同的数进行减去一个数,那么所有相同的数实际上是同时消去的,那么这个序列1 2 3 0 1 2等价于 1 2 3 (0可以不用考虑)
那么我们假设n = 6时候为f(6),那么f(6) = f(3)+1.
3 那么推广所有的n,那么f(n) = f(n/2)+1,n = 1的时候f(1) = 1

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int getAns(int n){
    return n == 1 ? 1 : getAns(n/2)+1;
}

int main(){
    int n;
    while(scanf("%d" , &n) != EOF)
        printf("%d\n" , getAns(n));
    return 0;
}
时间: 2024-09-15 17:19:24

uva 11384 Help is needed for Dexter的相关文章

11384 - Help is needed for Dexter 模拟 98

   分治法 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> usin

UVa 12208 How Many Ones Needed? (组合数学)

12208 - How Many Ones Needed? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=onlinejudge&Itemid=99999999&category=244&page=show_problem&problem=3360 水. 完整代码: /*0.035s*/ #include<bits/stdc++.h> using namespace s

UVa 10714:Ants

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1655 原题: An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant rea

UVa 10382:Watering Grass

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1323 原题: n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed

UVa 11054:Wine trading in Gergovia

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1995 [原题] As you may know from the comic "Asterix and the Chieftain's Shield", Gergovia consists of one street, and

UVa 216:Getting in Line, 暴力与回溯

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=152 题目类型: 暴力,回溯法 题目: Computer networking requires that the computers in the network be linked. This problem considers a ``lin

UVa 10603:Fill,经典倒水问题+隐式图搜索+dfs

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_problem&problem=1544 类型: 隐式图搜索 原题: There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater th

UVa 11198:Dancing Digits,Rujia Liu的神题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2139 类型: 隐式图搜索,  BFS, 哈希判重,模拟 原题: Digits like to dance. One day, 1, 2, 3, 4, 5, 6, 7 and 8 stand in a line to have a wonderful

UVa 344 Roman Digititis:罗马数字统计

344 - Roman Digititis Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=280 Many persons are familiar with the Roman numerals for relatively small numbers.