跳到正文

个人记录

题解 【整数拆分】

⚡小林子⚡约 1 分钟
原文
文章信息与版本

文章信息

发布
2020年8月6日
收录
2025年12月12日
更新
2026年11月22日
篇幅
约 1 分钟

版本

  1. v1当前

题目还要贴吗,自己看去


最基本的思路就是枚举子串,然后字符串转整数判断。

枚举子串 Θ(n2)\Theta(n^2),炸不了的。

然后… 结束了啊!

代码只要模拟过程就行了。

我写的过程是固定起点然后枚举到最后一个字符的。

说明一下为什么开 int 就行:题目中说了字符串最多 9 位数(炸不了),而且 x 最多也是 9 位(炸不了),所以 int 就行了。

Code:

C++
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
string s;int x,ans;
int main(){
	freopen("split.in","r",stdin);
	freopen("split.out","w",stdout);
	cin>>s>>x;
	for(int i=0;i<s.size();i++){
		if(s[i]=='0') continue;  //第一位不能是 0
		int tmp=s[i]-'0';  //开 tmp 存储当前数字大小
		if(tmp>x) ans++;
		for(int j=i+1;j<s.size();j++){  //枚举到最后一个字符
			tmp=tmp*10+s[j]-'0';
			if(tmp>x) ans++;
		}
	}
	cout<<ans;
	fclose(stdin);
	fclose(stdout);
	return 0;
}

The End.\sf{The\,End.}

原文