为什么这段代码不正确? [英] Why this code is not correct?

查看:96
本文介绍了为什么这段代码不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码用于查找两个数字是否为co_prime。

为什么此代码不正确?



我尝试过:



  #include   <   stdio.h  >  
void main(){

int a,b,min,n,k;

printf( 输入两个nums);
scanf( %d \ t%d,& a,&二);

if (a< b){min = a;}
else {min = b;}

for (n = 2 ; n< = min; n ++){
if (a%n == 0&& b%n == 0
{
k = 1 ;
break ;
}
else {
k = 0 ;
break ;
}
}
如果(k = 0 ){printf ( 这些是互质);} else if (k = 1 ){printf( 这些不是互质);}


}

解决方案

因为是sooooooooooooooo糟糕的缩进。



或者可能是因为

引用:

其他{k = 0;

break;}

显然是错误的。



尝试

  #include   <   stdio.h  >  


int main()
{

int a,b,min,n;

printf( 请输入两个数字(> 1) );
scanf( %d%d,& a,& b);


min = a< b? a:b;

if (min< 2
{
printf( 无效输入\ n);
返回 - 1 ;
}

for (n = 2 ; n< = min; n ++)
{
if (a%n == 0 & ;& b%n == 0
break ;
}

printf( 数字为);
if (n< = min)
printf( not);
printf( coprimes \ n);

return 0 ;
}


这里没有解决方案,只是建议:

- 缩进是一个很好的帮助,揭示你的结构正确使用时的代码。任何误操作只会使你的代码更加混乱,不一致的编码风格是一种误用。

在你的小代码中,缩进不一致,你使用3种不同的编码风格。

您的代码应如下所示:

  #include   <   stdio.h  >  
void main(){

int a ,b,分钟,N,K;

printf( 输入两个nums);
scanf( %d \ t%d,& a,&二);

if (a< b){
min = a;
}
其他 {
min = b;
}

for (n = 2 ; n< = min; n ++){
if (a%n == 0&& b%n == 0
{
k = 1 ;
break ;
}
else {
k = 0 ;
break ;
}
}
如果(k = 0 ){
printf( 这些是互质的);
}
else if (k = 1 ){
printf( 这些不是互质) ;
}
}





- 当你不明白你的代码失败的原因时,是时候使用了调试器。



- 作为程序员,你的工作也是找到有效的算法来解决你遇到的问题。

2个数字是当他们的GCD为1并且有一个众所周知的GCD算法时共同素数。

-----

学会正确缩进你的代码,它显示它的结构,它有助于阅读和理解。它还有助于发现结构错误。

专业程序员的编辑器具有此功能以及其他功能,例如括号匹配和语法突出显示。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]

-----

有一个工具可以让你看到你的代码是什么正在做,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

-----

最新公约数 - 维基百科 [如果(k = 0 ){printf( 这些是互质);}



这意味着,将k设置为0,然后如果该表达式的值为0,则打印这些是互质。因此,无论在该陈述之前持有的k值如何,它总是保持为零。它应该是:

  if (k ==  0  //  将k与0进行比较 
{
printf( 这些是互质);
}
其他 // 不需要这个if子句:if(k = 1)
{
printf( 这些不是互质);
}



或者有点整洁:

 printf( 这些是); 
if (k> 0
{
printf( not);
}
printf( coprimes \ n);


this code is to find the whether two numbers are co_prime or not.
why this code is not correct?

What I have tried:

#include<stdio.h>
void main(){

int a,b,min,n,k;

printf("enter two nums");
scanf(" %d \t %d",&a,&b);

if(a<b){min=a;}
else{min=b;}

for(n=2;n<=min;n++){
  if(a%n==0&&b%n==0)
  {
     k=1;
     break;
  }
  else{
  k=0;
  break;
}
                    }
if(k=0){printf("these are coprimes");}else if(k=1){printf("these are not coprimes");}


}

解决方案

Because is sooooooooooooooo bad indented.

Or possibly because the

Quote:

else{k=0;
break;}

is plainly wrong.

Try

#include <stdio.h>


int main()
{

  int a, b, min, n;

  printf("please, enter two numbers (> 1) ");
  scanf(" %d  %d",&a,&b);


  min = a < b ? a : b;

  if ( min < 2 )
  {
    printf("invalid input\n");
    return -1;
  }

  for( n=2; n<=min; n++)
  {
    if( a % n == 0 && b % n == 0)
      break;
  }

  printf("the numbers are");
  if (n <= min)
    printf(" not");
  printf(" coprimes\n");

  return 0;
}


No solution here, just advices:
- Indentation is a great help to expose the structure of your code when used correctly. Any misuse only make your code more confuse, and inconsistent coding style is a misuse.
In your little piece of code, indentation is not consistent and you use 3 different coding styles.
Your code should look like:

#include<stdio.h>
void main(){

  int a,b,min,n,k;

  printf("enter two nums");
  scanf(" %d \t %d",&a,&b);

  if(a<b){
    min=a;
  }
  else{
    min=b;
  }

  for(n=2;n<=min;n++){
    if(a%n==0&&b%n==0)
    {
      k=1;
      break;
    }
    else{
      k=0;
      break;
    }
  }
  if(k=0){
    printf("these are coprimes");
  }
  else if(k=1){
    printf("these are not coprimes");
  }
}



- When you don't understand why your code fails, it is time to use the debugger.

- As a programmer, your job is also to find efficient algorithms to solve the problems you encounter.
2 numbers are co-primes when their GCD is 1 and there is a very well known algorithm for GCD.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
-----
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
-----
Greatest common divisor - Wikipedia[^]


if(k=0){printf("these are coprimes");}


Which means, set k equal to 0 and then if the value of that expression is 0 print "these are coprimes". So whatever value k held before that statement it will always hold zero after. It should be:

if (k == 0) // compare k with 0
{
    printf("these are coprimes");
}
else // no need for this if clause: if(k=1)
{
    printf("these are not coprimes");
}


Or somewhat neater:

printf("these are ");
if (k > 0)
{
    printf("not ");
}
printf("coprimes\n");


这篇关于为什么这段代码不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆