查找给定整数的邻居之和

时间:2019-11-21 14:26:35

标签: c++

给出五个整数的序列。将这些数字(第一个和最后一个除外)替换为其邻居的总和。打印结果编号。

好了,这样编码并测试了代码,它给了我10%的代码。我在哪里弄错了?代码看起来还可以,可以正常工作,但是我不能100%。这是家庭作业,只是为了让你认识你们))你们能帮我找出我在哪里犯错吗?

#include <bits/stdc++.h>
using namespace std;
int main()
{
  int a,b,c,d,f;
  cin>>a>>b>>c>>d>>f;
  int b_1=(b-1)+(b+1);
  int c_1=(c-1)+(c+1);
  int d_1=(d-1)+(d+1);
  cout<<a<<b_1<<c_1<<d_1<<f;
}

//input 1 2 3 4 5
//ouput 1 4 6 8 5

2 个答案:

答案 0 :(得分:4)

我认为您误解了这个问题。邻居之和是序列中给定数字之前和之后的数字之和。从不应该计算第一个和最后一个数字(每个数字只有一个邻居)这一事实看来,这很明显。

int a, b, c, d, e;
if (cin >> a >> b >> c >> d >> e) {
  cout << a << (a + c) << (b + d) << (c + e) << e;
}

答案 1 :(得分:0)

首先请注意您自己的信息

  

给出五个整数的序列。 替换这些数字,但   第一个和最后一个,加上邻居的总数。打印   结果数字。

因此,您不仅需要输出新值。您需要替换变量的值。

您可以简单地使用标题std::exchange中声明的标准函数<utility>来完成任务。

例如

#include <iostream>
#include <utility>

int main() 
{
    int a, b, c, d, f;

    std::cin >> a >> b >> c >> d >> f;

    std::cout << "a = " << a
              << ", b = " << b
              << ", c = " << c
              << ", d = " << d
              << ", f = " << f
              << '\n';

    int tmp = std::exchange( b, a + c );
    tmp = std::exchange( c, tmp + d );
    std::exchange( d, tmp + f );

    std::cout << "a = " << a
              << ", b = " << b
              << ", c = " << c
              << ", d = " << d
              << ", f = " << f
              << '\n';

    return 0;
}

程序输出为

a = 1, b = 2, c = 3, d = 4, f = 5
a = 1, b = 4, c = 6, d = 8, f = 5

另一种方法是使用标准函数std :: swap。例如

#include <iostream>
#include <utility>

int main() 
{
    int a, b, c, d, f;

    std::cin >> a >> b >> c >> d >> f;

    std::cout << "a = " << a
              << ", b = " << b
              << ", c = " << c
              << ", d = " << d
              << ", f = " << f
              << '\n';

    int tmp = b;

    b = a + c;

    std::swap( tmp, c );

    c += d;

    std::swap( tmp, d );
    d += f;


    std::cout << "a = " << a
              << ", b = " << b
              << ", c = " << c
              << ", d = " << d
              << ", f = " << f
              << '\n';

    return 0;
}

再次显示程序输出是

a = 1, b = 2, c = 3, d = 4, f = 5
a = 1, b = 4, c = 6, d = 8, f = 5

您可以自己模拟功能std :: swap。例如

#include <iostream>

int main() 
{
    int a, b, c, d, f;

    std::cin >> a >> b >> c >> d >> f;

    std::cout << "a = " << a
              << ", b = " << b
              << ", c = " << c
              << ", d = " << d
              << ", f = " << f
              << '\n';

    int prev = a;

    int tmp = prev;
    prev = b;
    b = tmp;

    b += c;

    tmp = prev;
    prev = c;
    c = tmp;

    c += d;

    tmp = prev;
    prev = d;
    d = tmp;

    d += f;

    std::cout << "a = " << a
              << ", b = " << b
              << ", c = " << c
              << ", d = " << d
              << ", f = " << f
              << '\n';

    return 0;
}

程序输出为

a = 1, b = 2, c = 3, d = 4, f = 5
a = 1, b = 4, c = 6, d = 8, f = 5

最后是最简化的方法。

#include <iostream>

int main() 
{
    int a, b, c, d, f;

    std::cin >> a >> b >> c >> d >> f;

    std::cout << "a = " << a
              << ", b = " << b
              << ", c = " << c
              << ", d = " << d
              << ", f = " << f
              << '\n';

    int tmp = b;

    b = a + c;

    c = tmp + d;

    d = f + b - a;

    std::cout << "a = " << a
              << ", b = " << b
              << ", c = " << c
              << ", d = " << d
              << ", f = " << f
              << '\n';

    return 0;
}

程序输出为

a = 1, b = 2, c = 3, d = 4, f = 5
a = 1, b = 4, c = 6, d = 8, f = 5

使用最后一种方法。:)对于初学者来说很简单。