九度论坛-1465

/**
 * 文件名:Main.java
 *
 * 版本信息:
 * 日期:2013-6-25
 * Copyright Corporation 2013
 * 版权所有
 *
 */
package 题目1465;

import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * 项目名称:arithmetic
 * 类名称:Main
 * 类描述:
 * 创建人:黄传聪
 * 创建时间:2013-6-25 下午5:09:57
 * 修改人:黄传聪
 * 修改时间:2013-6-25 下午5:09:57
 * 修改备注:
 * @version
 *
 */
public class Main {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		int num ;
		while(scanner.hasNext()){
			num = scanner.nextInt();
			if(num == 0){
				break;
			}
			
			int[] nums = new int[num];
			int i,j;
			for(i=0;i!=num;i++){
				nums[i] = scanner.nextInt();
			}
			
			Arrays.sort(nums);//对数组按照升序排序
			int count = 0;
			for(i=0;i<nums.length;i++){
				int a = nums[i];
				for(j=i+1;j<nums.length;j++){
					int b = nums[j];
					if(gcb(a,b) == false && a / b < 1){
						count++;
					}
				}
			}
			System.out.println(count);
		}
	}
	
	public static boolean gcb(int a, int b){
		int temp;
		if(a<b){
			temp = b;
			b = a;
			a = temp;
		}
		
		while(b != 0){
			temp = a % b;
			a = b;
			b = temp;
		}
		
		if(a == 1){
			return false;
		}else{
			return true;
		}
	}
	

}

c++ 代码:
#include <iostream>
#include <algorithm>
using namespace std;
int gcd(int , int );
bool isTrue(int );
int main ()
{
    int num;
    while (cin >> num)
    {
        if(num == 0)
        {
            break;
        }
        else
        {
            int i,j;
            int nums[num]; //用于存放输入的数据
            for(int i=0;i != num;i++)
            {
                cin >> nums[i];
            }
 
            sort(nums,nums+num);
 
            int count = 0;
 
            for(i=0;i != num;i++)
            {
                int a = nums[i];
                for(j=0;j != num;j++)
                {
                    int b = nums[j];
 
                    if(isTrue(gcd(a,b)) && a / b < 1)
                    {
                        count++;
                    }
                }
            }
            cout<<count<<endl;
        }
    }
        return 0;
}
 
int gcd(int a, int b)
{
    if(a % b == 0)
    {
        return b;
    }
    else
    {
        return gcd(b, a % b);
    }
}
 
bool isTrue(int a)
{
    if(a == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

转载于:https://my.oschina.net/u/1011659/blog/140442

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值