|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<bits/stdc++.h>
- using namespace std;
- #define endl '\n'
- #define ll long long
- #define int long long
- signed main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int t;
- cin>>t;
- int shang=2e5+7;
- int arr[shang+1];
- for(int i=1;i<=shang-1;i++){
- int he=0;
- int zhi=i;
- while(zhi){
- he+=zhi%10;
- zhi/=10;
- }
- arr[i]=he;
- }
- arr[0]=0;
- for(int i=1;i<=shang-1;i++){
- arr[i]+=arr[i-1];
- }
- while(t--){
- int n;
- cin>>n;
- cout<<arr[n]<<endl;
- }
- return 0;
- }
复制代码
与第二份代码相比为什么会快这么多,第二份代码为什么运行这么慢
- #include<bits/stdc++.h>
- using namespace std;
- #define endl '\n'
- #define ll long long
- #define int long long
- const int N=1e6+10;
- int pre[N];
- void solve()
- {
-
- int n;
- cin>>n;
- for(int i=1;i<=n;i++)
- {
- int he=0;
- int x=i;
- while(x>0)
- {
- he+=x%10;
- x/=10;
- }
- pre[i]=he;
- }
- for(int i=1;i<=n;i++)
- {
- pre[i]+=pre[i-1];
- }
- cout<<pre[n]<<endl;
-
- }
- signed main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int t;
- cin>>t;
- while(t--)
- {
- solve();
- }
- }
复制代码 |
|