鱼C论坛

 找回密码
 立即注册
查看: 170|回复: 1

代码比较

[复制链接]
发表于 2024-2-20 10:33:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. #define endl '\n'
  4. #define ll long long
  5. #define int long long

  6. signed main(){
  7.     ios::sync_with_stdio(0);
  8.     cin.tie(0);
  9.     cout.tie(0);
  10.     int t;
  11.     cin>>t;
  12.     int shang=2e5+7;
  13.     int arr[shang+1];
  14.     for(int i=1;i<=shang-1;i++){
  15.         int he=0;
  16.         int zhi=i;
  17.         while(zhi){
  18.             he+=zhi%10;
  19.             zhi/=10;
  20.         }
  21.         arr[i]=he;
  22.     }
  23.     arr[0]=0;
  24.     for(int i=1;i<=shang-1;i++){
  25.         arr[i]+=arr[i-1];
  26.     }
  27.     while(t--){
  28.         int n;
  29.         cin>>n;
  30.         cout<<arr[n]<<endl;
  31.     }
  32.     return 0;
  33. }
复制代码

与第二份代码相比为什么会快这么多,第二份代码为什么运行这么慢
  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. #define endl '\n'
  4. #define ll long long
  5. #define int long long
  6. const int N=1e6+10;
  7. int pre[N];
  8. void solve()
  9. {
  10.    
  11.    int n;
  12.    cin>>n;
  13.    for(int i=1;i<=n;i++)
  14.    {
  15.     int he=0;
  16.     int x=i;
  17.     while(x>0)
  18.     {
  19.         he+=x%10;
  20.         x/=10;
  21.     }
  22.     pre[i]=he;
  23.    }
  24.    for(int i=1;i<=n;i++)
  25.    {
  26.     pre[i]+=pre[i-1];
  27.    }
  28.    cout<<pre[n]<<endl;
  29.    


  30. }
  31. signed main()
  32. {
  33.     ios::sync_with_stdio(0);
  34.     cin.tie(0);
  35.     cout.tie(0);
  36.     int t;
  37.     cin>>t;
  38.     while(t--)
  39.     {
  40.         solve();
  41.     }
  42. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-2-20 10:33:59 | 显示全部楼层
这段代码的作用是读入一个整数t,然后循环t次。每次循环中,程序将输入的整数存储在变量arr中,并输出arr。

在这段代码中,使用了一些预处理指令和宏定义来简化代码编写和提高效率。其中,`#include`是C++中引入头文件的指令,`using namespace std;`表示使用标准命名空间std,`#define endl '\n'`定义了换行符为'\n',`#define ll long long`和`#define int long long`定义了ll和int为long long型。

程序开始时通过`ios::sync_with_stdio(0)`来关闭C++标准流与C标准流之间的同步,以提高输入输出效率。然后通过`cin.tie(0)`和`cout.tie(0)`将cin和cout与其他输入输出关联起来,以提高效率。

接着定义了变量t和shang,并将2e5+7赋值给shang。然后进入for循环,从1循环到t。在循环体内,使用`cin>>arr`读入一个整数,并使用`cout`将该整数输出。

这段代码可以正常运行,但是在最后一行的输出部分似乎出现了错误,因为没有给出具体的输出内容,所以无法得知最终输出的内容是什么。如果需要进一步帮助,请提供更多的信息。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 04:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表