九度論壇-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;
    }
}
相關文章
相關標籤/搜索