每日一九度之 题目1031:xxx定律
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:6870
解决:4302
- 题目描述:
-
对于一个数n,如果是偶数,就把n砍掉一半;如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止。
请计算需要经过几步才能将n变到1,具体可见样例。
- 输入:
-
测试包含多个用例,每个用例包含一个整数n,当n为0 时表示输入结束。(1<=n<=10000)
- 输出:
-
对于每组测试用例请输出一个数,表示需要经过的步数,每组输出占一行。
- 样例输入:
-
3
1
0
- 样例输出:
-
5
0
挺简单的一道模拟题,但是也可以用数学方法计算出n与次数的关系式(可以自己尝试下)。
//Asimple
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <map>
#include <string>
#include <queue>
#define INF 100000
using namespace std;
const int maxn = ;
typedef long long ll;
int n; int fun(int n, int cnt){
if( n == ) return cnt;
else if( n & ){
n = * n + ;
n /= ;
fun(n,cnt+);
} else {
n /= ;
fun(n,cnt+);
}
} int main(){
while( scanf("%d",&n) && n ){
int cnt = ;
cout << fun(n,cnt) << endl ;
}
return ;
}
每日一九度之 题目1031:xxx定律的更多相关文章
- 每日一九度之 题目1033:继续xxx定律
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5502 解决:1351 题目描述: 当n为3时,我们在验证xxx定律的过程中会得到一个序列,3,5,8,4,2,1,将3称为关键数, ...
- 九度oj 题目1031:xxx定律 题目1033:继续xxx定律
题目描述: 对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止. 请计算需要经过几步才能将n变到1,具体可见样例. 输入: ...
- 每日一九度之 题目1076:N的阶乘
时间限制:3 秒 内存限制:128 兆 特殊判题:否 提交:7601 解决:2749 题目描述: 输入一个正整数N,输出N的阶乘. 输入: 正整数N(0<=N<=1000) 输出: 输入可 ...
- 每日一九度之 题目1043:Day of Week
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7336 解决:2563 题目描述: We now use the Gregorian style of dating in Russia. ...
- 每日一九度之 题目1042:Coincidence
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3007 解决:1638 题目描述: Find a longest common subsequence of two strings. 输入 ...
- 每日一九度之 题目1041:Simple Sorting
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4883 解决:1860 题目描述: You are given an unsorted array of integer numbers. ...
- 每日一九度之 题目1040:Prime Number
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...
- 每日一九度之 题目1038:Sum of Factorials
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...
- 每日一九度之 题目1039:Zero-complexity Transposition
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3372 解决:1392 题目描述: You are given a sequence of integer numbers. Zero-co ...
随机推荐
- ios-完成任务状态栏弹出提示view的小框架设计
设计思路: 创建单例,当设置提示view的属性时,可以随时访问到,并且只有一份. 创建对应的类方法.提供设置提示内容content,提示内容对应的图片image,提示view背景色以及背景图片的设置( ...
- Leetcode: Longest Substring with At Least K Repeating Characters
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- struts2 标签字体大小
<style type="text/css"> label{ font-size: 20px; } </style> <s:textfield nam ...
- m球求n盒子问题
球同盒同可空盒问题 #include <bits/stdc++.h> using namespace std; const int N = 25; int dp[N][N]; int ma ...
- UVA 10891 Game of Sum(DP)
This is a two player game. Initially there are n integer numbers in an array and players A and B get ...
- Codeforces Beta Round #93 (Div. 1 Only) D. Fibonacci Sums
先考虑一个斐波那契数能分成其他斐波那契数的方案,假如f[i]表示第i个斐波那契数,那么只要对他进行拆分,f[i-1]这个数字必定会存在.知道这一点就可以进行递推了.先将数字分成最少项的斐波那契数之和, ...
- firefox中 checkbox属性checked="checked"已有,但复选框却不显示打钩的原因
最近在调试复选框的应用,在ie没有问题,考虑到兼容性,试试了firefox,遇到了问题. 复选框绑定了click事件,点一次选中,再点击取消选中,依次类推.这个功能在ie中没问题,但是在firefox ...
- demo03linearlayoutdemo;
package com.example.demo03linearlayoutdemo; import android.os.Bundle; import android.app.Activity; i ...
- YbRapidSolution.MVC项目首页缓存没有起作用
Response.Cache.SetOmitVaryStar(true); 文件方面的内容,增加了这个语句 没有的话缓存没起作用,增加这个语句可提高系统性能. HomeController: usin ...
- oracle表分区
注:新建分区表前要先准备好要用的表空间 一. oracle分区类型: 范围分区(Range分区) 列表分区(List分区) 散列分区(Hash分区) 组合分区(Composite Partitioni ...