跳到正文

题解

题解:P10985 [蓝桥杯 2023 国 Python A] 整数变换

ethanho约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年8月25日
收录
2025年12月12日
更新
2026年9月24日
篇幅
约 2 分钟
题目
P10985

版本

  1. v1当前

看到标题,我就想用Python写了,然而:

(下面的Python代码首先定义了一个 digit⁡\operatorname{digit} 函数,用于计算一个整数的各位数字之和。然后定义了一个 min⁡\operatorname{min} 函数,返回这个数变回零所需的分钟数。最后,通过输入一个正整数并调用函数,我们可以得到结果)

Code1(60pts,原始写法数位和+函数统计):

Python
def digit(n):  
    s=0  
    while n>0:  
        k=n%10  
        s+=k  
        n=n//10  
    return s

def min(n):  
    m=0  
    while n>0:  
        n-=digit(n)  
        m+=1  
    return m

n=int(input())
print(min(n))

Code2(60pts,换了个字符串写法+函数统计):

Python
def digit(n):  
    str_n=str(n)    
    s=0  
    for c in str_n:   
        s+=int(c)    
    return s  

def min(n):  
    m=0  
    while n>0:  
        n-=digit(n)  
        m+=1  
    return m  
  
n=int(input())
print(min(n))

然后就把我逼疯了,却发现只是个 入门\mathbf{{\color{Red}入门 } } 题

最后的最后,还是用上了万能的C++

Code3(100pts,正常):

C++
#include<bits/stdc++.h>//万能头文件,不解释
using namespace std;
int n,cnt=0;

int f(){
	int x=n,s=0;
	while(x>0){
		s+=x%10;//加上末位
		x/=10;
	}
	return s;//返回
}

int main(){
	cin>>n;//输入
	while(n>0){
		n-=f();//减法操作
		cnt++;//统计
	}
	cout<<cnt<<endl;//输出
	return 0;//好习惯养成
}
原文