3位整数程序无法执行

  • 本文关键字:执行 程序 整数 3位 c
  • 更新时间 :
  • 英文 :


是的,这是一个基本的C编码家庭作业问题。不,我不仅在寻找某人为我做。考虑到这是我的第一个编程课,我并不感到惊讶的是我无法正常工作,而且我敢肯定它有很多错误。我只需要一些帮助,指出我的代码和缺少的问题,以便我可以自己修复它们。

家庭作业问题:

编写一个程序仅读取一个整数编号(您的输入必须为 一个3位数字从100到999),并认为一个数字是 是ABC(其中A,B和C是一个数字的3位数字)。现在, 形成成为ABC,BCA和CAB的数字,然后找出 这三个数字除以11时的其余部分。 假设其余部分分别为X,Y和Z,并添加它们 向上如x y,y z和z x。现在,如果这些总结中的任何一个很奇怪 数字,如果总和11少于20,则将其增加11个, 否则,将总和减少11(此求和操作 必须是正数,但小于20)。最后,每个 一半的总和。现在,打印出所有结果数字。

我的代码:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
    //Declare all variables
    int OrigNumber;
    int x, y, z;
    int number;
    number = x, y, z;
    int sum;
    //
    printf("Input a three digit number");
    //
    int c;
    c = OrigNumber %10;
    //
    int b;
    b=((OrigNumber - c) % 100)/10;
    //
    int a;
    a = (OrigNumber - (b + c))/100;
    //
    int abc, bca, cab;
    abc = (a*100) + (10*b) + c;
    bca = (10*b) + c + (a*100);
    cab = c + (a*100) + (10*b);
    //
    if((number % 2) == 1)
        {
            if(number + 11 < 20)
                number += 11;
            else if((100 - 11 > 0) && (100 - 11 < 20))
                number -= 11;
        }
    //
    x = abc/11;
    y = bca/11;
    z = cab/11;
    //
    sum = (x + y),
          (y + z),
          (z + x);
}

要开始,您需要读取输入。首先要提示,该提示包括马车返回:

printf("Input a three digit number: n");

由于它是三位数的编号,因此您可以添加以下行以读取输入:

scanf("%3d", &OrigNumber);

下一个代码效果很好,直到您到达if (number % 2),这是毫无意义的,因为您并没有真正定义number-嗯,您做到了,但是行

number = x, y, z;

不做您认为做的事情。如果添加

printf("So far I have abc=%d, bca=%d, cab=%dn", abc, bca, cab);

第一次阅读数字并计算了这三个时,您会看到自己在路上。

请注意

number = x, y, z;

使用一种称为"逗号运算符"的东西。所有的东西(A,B,C)都是"评估"的,但它们的价值未返回。无论如何,在您拥有该行的地方,您尚未将值分配给X,Y和Z。

这足以使您开始?

Update 现在您已经有几个小时来考虑了这一点,这里有几个指针。

您的abc, cab, bca计算没有任何意义。我将向您展示其中之一:

cab = c*100 + a*10 + b;

接下来,您需要计算x,y和z的每个。同样,这是三个:

y = bca%11;

现在您必须赚取总和 - 我称它们为xyyzzx。其中之一:

zx = z + x;

接下来要处理以下指令:"现在,如果这些总和中的任何一个是奇数,则如果总和11少于20,则将其增加11个,否则将总和减少11:

if(xy % 2 == 1) {
  if(xy + 11 < 20) xy += 11; else xy -= 11;
}

对所有三个总和都使用类似的代码。然后"除以2":

xy /= 2;

根据需要重复。

最后,打印出结果:

printf("xy: %d, yz: %d, zx: %dn", xy, yz, zx);

令人惊讶的是,如果您做到了这一点,您会收回原始数字...

您可以通过使用一系列值并循环遍历它来使代码更加紧凑 - 而不是重复我上面编写的代码片段,而不是使用不同的变量。但是我怀疑这远远超出了您此时所知道的。

您可以从这里拿走吗?

#include <stdio.h>
int main()
{
    //Declare all variables
    int OrigNumber;
    int a, b, c;
    int abc, bca, cab;
    int x, y, z;
    int xplusy , yplusz, xplusz;
    printf(" A program to read ONLY one integer number.n Input must be one 3 digit number from 100 to 999 : ");    
    scanf("%d", &OrigNumber);   // Get input from console
    if(OrigNumber > 999 || OrigNumber < 100) {
        printf("Invalid number. Quiting program. This is error handling. Important while learning programming.");   
    return 0;
    }
    c = OrigNumber %10; // digit at unit's place
    b=((OrigNumber) % 100)/10; //digit at the ten's place
    a = (OrigNumber)/100; //digit at the 100's place. Note: 734/100 = 7. NOT 7.34.
    printf("n Three numbers say A,B, C : %d, %d , %d ", a, b, c);  
    abc = a*100 + 10*b + c;
    bca = 100*b + 10*c + a;
    cab = c*100 + a*10 + b;
    printf("n Three numbers say ABC, BCA, CAB : %d, %d , %d ", abc, bca, cab);
    x = abc % 11;   // Reminder when divided by 11.
    y = bca % 11;
    z = cab % 11;
    printf("n Three numbers say X, Y, Z : %d, %d , %d ", x, y, z);
    xplusy = x + y; // Adding reminders two at a time.
    yplusz = y + z;
    xplusz = x + z;
    printf("n Three numbers  X+Y, Y+Z, X+Z : %d, %d , %d ", xplusy, yplusz, xplusz);
    if((xplusy % 2) == 1) {
        if(xplusy + 11 < 20)
            xplusy += 11;
        else
        xplusy -= 11;
    }
    if((yplusz % 2) == 1) {
        if(yplusz + 11 < 20)
            yplusz += 11;
        else
            yplusz -= 11;
    }
    if((xplusz % 2) == 1) {
        if(xplusz + 11 < 20)
            xplusz += 11;
        else
            xplusz -= 11;
    }
    xplusy /= 2; // Finally, divide each of the sum in half.
    yplusz /= 2;
    xplusz /= 2;
    printf("n Now print out all the resulting digits :  %d, %d , %d n", xplusy, yplusz, xplusz);
    return 0;
}
int abc, bca, cab;
abc = (a*100) + (10*b) + c;
bca = (10*b) + c + (a*100);
cab = c + (a*100) + (10*b);

我建议此时在代码中打印出数字。

printf( "%d %d %d", abc, bca, cab );

我认为您会看到您解决的问题之一。

#include <iostream>
#include <cstdio>
using namespace std;
int main() {
    int n, a, b, c, abc, bca, cab, x, y, z, p, q, r;
    scanf("%d", &n);
    c=n%10;
    b=(n/10)%10;
    a=n/100;
    abc=a*100+b*10+c;
    bca=b*100+c*10+a;
    cab=c*100+a*10+b;
    x=abc%11;
    y=bca%11;
    z=cab%11;
    p=x+y;
    q=y+z;
    r=z+x;
    return 0;
}

现在,如果这些总和是奇数,请增加11 求和11少于20,否则将总和减少 11(此求和操作必须为正数,但小于 20)。最后,将每个总和分为一半。现在,打印出所有的 结果数字。

我没有得到最后一部分,您能更清楚地解释它吗?

最新更新