你可以建议......以下代码出了什么问题 [英] Can you please suggest...what is going wrong in the following code
本文介绍了你可以建议......以下代码出了什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定A,B打印对(a,b)的数量,使得GCD(a,b)= 1且1 <= a <= A且1 <= b <= B。
输入:
3,2
输出:5
1,1
1,2
2,1
3,1
3,2
我能得到正确的输出。
但是对于某些测试用例,我的回答是错误的。
你们可以帮忙找一下这个bug。
提前致谢
我的代码:
Given A,B print the number of pairs (a,b) such that GCD(a,b)=1 and 1<=a<=A and 1<=b<=B.
Input:
3,2
Output: 5
1,1
1,2
2,1
3,1
3,2
I anm getting the correct output.
But for some test cases, I am getting wrong answer.
Can you guys please help in finding the bug.
Thanks in advance
My code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GCD_3
{
class Program
{
public static int GCD(int a, int b)
{
while (b != 0)
{
int tmp = b;
b = a % b;
a = tmp;
}
if (a == 1)
return (1);
else return (0);
}
static void Main(string[] args)
{
int t = Convert.ToInt32(Console.ReadLine());
string[] num = new string[t * 2];
int[] a = new int[t*2];
int k = 0;
int count = 0;
for (int i = 0; i < t; i++)
{
num= Console.ReadLine().Split(' ');
for (int j = 0; j < 2; j++)
{
a[k++] = Convert.ToInt32(num[j]);
}
}
for (int i = 0; i < t; i++)
{
int x = a[i * 2];
int y = a[i * 2 + 1];
for (int b = 1; b <= x; b++)
{
for (int c = 1; c <= y; c++)
{
int count1 = GCD(b, c);
count += count1;
}
}
}
Console.WriteLine(count);
Console.Read();
}
}
}
推荐答案
最大公约数
查看链接以获取更多信息:
http:/ /en.wikipedia.org/wiki/Greatest_common_divisor [ ^ ]
http:/ /www.math.wustl.edu/~victor/mfmm/compaa/gcd.c [ ^ ]
祝你好运!
Greatest common divisor
Check the links for more info:
http://en.wikipedia.org/wiki/Greatest_common_divisor[^]
http://www.math.wustl.edu/~victor/mfmm/compaa/gcd.c[^]
Good luck!
这篇关于你可以建议......以下代码出了什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文