Number

链接:https://ac.nowcoder.com/acm/contest/893/B
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

Bonnie得到了一个数字n。
现在她想对这个数字不断的做一种操作:

  • 如果n的最后一位数码是0,那么她就把n除以10;
  • 否则她把这个数加上1;
  • 直到n变为一个不大于1的数。

给定n,请问Bonnie需要做多少次操作?

输入描述:

第一行一个数字T(1≤T≤300000)T(1 \le T \le 300000)T(1≤T≤300000)--样例个数。

每个样例仅一行一个数字n(1≤n≤109)n(1 \le n \le 10^{9})n(1≤n≤109)。

输出描述:

每个样例输出一行一个数字—Bonnie需要做的操作次数。

示例1

输入

复制

6  
9  
99  
2  
11032  
1000000000  
62

输出

复制

2
3
9
44
9
13

说明

第一个样例: 9→10→19 \rightarrow 10 \rightarrow 19→10→1
第二个样例: 99→100→10→199 \rightarrow 100 \rightarrow 10\rightarrow 199→100→10→1
#include <iostream>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <set>
#include <list>
#include <queue>
#include <stack>

using namespace std;

int dfs(int n){
	
	if(n<=1){
		
		return 0;
	}
	
	if(n%10==0){
		
		return 1+dfs(n/10);
	}
	else{
		
		return 1+dfs(n+1);
	}
} 

int main(){
	
	ios::sync_with_stdio(false);
		
	int n;
	cin>>n;
	
	for(int i=0;i<n;i++){
		
		int m;
		cin>>m;
		
		cout<<dfs(m)<<endl;
	}
	
	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值