试图更有效地解决这个问题

发布于 2025-01-26 21:18:30 字数 782 浏览 1 评论 0原文

我读了一个自然数字,最多有9位数字。我必须形成一个以该数字的偶数数字的向量,每位数字一次出现一次(示例:987622222将输出:2 6 8)。我想知道我可以在此代码中添加哪些优化,以提高其效率。

#include <iostream>

using namespace std;

int main()
{   int x,i,k=0,copy,v[10]={0},j;
    cout<<"x=";
    cin>>x;
    copy=x;
    if (x==0)
        cout<<0;
    while (x>0)
    {
        v[x%10]++;
        x/=10;
    }
    for (i=0; i<10; i=i+2)
    {
        if(v[i]>0)
        {
          k++;
        }
    }
    int w[k]={0};
    if (k>0)
     {  j=0;
        for (i=0; i<10; i=i+2)
          if(v[i]>0)
           {   w[j]=i;
               j++;}
     }

    for (j=0; j<k; j++)
        cout<<w[j]<<" ";
    if (k==0 && copy!=0)
     cout<<"No even digits";
    return 0;
}

I read a natural number with at most 9 digits. I have to form a vector with the even digits of that number, in ascending order, with each digit appearing once (example : 98762222 is going to output: 2 6 8). I want to know what optimisations can I add to this code to make it more efficient.

#include <iostream>

using namespace std;

int main()
{   int x,i,k=0,copy,v[10]={0},j;
    cout<<"x=";
    cin>>x;
    copy=x;
    if (x==0)
        cout<<0;
    while (x>0)
    {
        v[x%10]++;
        x/=10;
    }
    for (i=0; i<10; i=i+2)
    {
        if(v[i]>0)
        {
          k++;
        }
    }
    int w[k]={0};
    if (k>0)
     {  j=0;
        for (i=0; i<10; i=i+2)
          if(v[i]>0)
           {   w[j]=i;
               j++;}
     }

    for (j=0; j<k; j++)
        cout<<w[j]<<" ";
    if (k==0 && copy!=0)
     cout<<"No even digits";
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫烠⑼条掵仅有一顆心 2025-02-02 21:18:30
#include <iostream>
#include <set>

int main()
{
    int64_t x; // You should use int64_t to cover ALL 9-digit numbers
    std::cout << "x=";
    std::cin >> x;
    std::set<int> s; // But each digit will be OK as a plain "int"
    while (x > 0)
    {
        int digit = x % 10;
        if (digit % 2 == 0) s.insert(digit);
        x /= 10;
    }
    if (s.size() == 0) std::cout << "No even digits.";
    else for (auto d : s) std::cout << d << " ";
    std::cout << "\n";
    return 0;
}

示例输入/输出:

x=98762222
2 6 8

The std::set container is your friend, here. It automatically sorts its elements and only keeps one (unique) element for each value:

#include <iostream>
#include <set>

int main()
{
    int64_t x; // You should use int64_t to cover ALL 9-digit numbers
    std::cout << "x=";
    std::cin >> x;
    std::set<int> s; // But each digit will be OK as a plain "int"
    while (x > 0)
    {
        int digit = x % 10;
        if (digit % 2 == 0) s.insert(digit);
        x /= 10;
    }
    if (s.size() == 0) std::cout << "No even digits.";
    else for (auto d : s) std::cout << d << " ";
    std::cout << "\n";
    return 0;
}

Example input/output:

x=98762222
2 6 8
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文