有关作用域的问题求解

这两张图片就是v数组和1数组定义的地方不一样,请问这有什么差别吗,为什么在函数外定义我的结果就对了

img

img

在主函数外边定义的变量是全局变量,整个CPP文件都可以引用得到。主函数里边定义的变量是局部变量,只有为主函数里边引用得到。不过,你这两种定义方式,效果应该是一样的。除非,还有别的代码对全局变量进行改变。

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7804526
  • 你也可以参考下这篇文章:在数组x的十个数中求平均值v,找出与v相差最小的数组元素
  • 除此之外, 这篇博客: 问题 V: 【字符串】十六进制转换中的 样例输入 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    复制样例数据

    123ABC
    

    样例输出

    4435274
    
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <vector>
    #include <ctime>
    #include <cctype>
    #include <bitset>
    #include <utility>
    #include <sstream>
    #include <complex>
    #include <iomanip>
    #define inf 0x3f3f3f3f
    typedef long long ll;
    using namespace std;
    string s;
    char T[1000010];
    int main()
    {
        cin>>s;
        int cd=s.size(),cd1=0,ct;
        for(int i=cd-1; i>=0; i-=3)
        {
            ct = 0;
            if(s[i] < 'A')
                ct+=s[i] - '0';
            else
                ct+=s[i] - 'A' + 10;
            if(i - 1 >= 0)
            {
                if(s[i - 1] < 'A')
                    ct+=(s[i - 1] - '0')*16;
                else
                    ct+=(s[i - 1] - 'A' + 10) * 16;
            }
            if(i - 2 >= 0)
            {
                if(s[i - 2] < 'A')
                    ct+=(s[i - 2] - '0')*256;
                else
                    ct+=(s[i - 2] - 'A' + 10) * 256;
            }
            for(int j = 0; j < 4; ++j)
            {
                T[++cd1] = ct % 8 + '0';
                ct /= 8;
            }
        }
        while(cd1>1&&T[cd1]=='0')
            cd1--;
        for(int i=cd1; i>0; i--)
            printf("%c",T[i]);
        return 0;
    }
    

     


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^