GDUFE ACM-1093

题目:http://acm.gdufe.edu.cn/Problem/read/id/1093

 

互质数简单版

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

  For given integer N (1<=N<=10^5) find amount of positive numbers not greater than N that coprime with N. Let us call two positive integers (say, A and B, for example) coprime if (and only if) their greatest common divisor is 1. (i.e. A and B are coprime iff gcd(A,B) = 1). 

Input:

The input includes several cases. For each case,the input contains integer N. 

Output:

For each case,print answer in one line. 

Sample Input:

9

Sample Output:

6

思路:一个个的判断是不是互质数,如果是,总数加1,这个方法数字大的时候回超时==

难度:简单吧

代码:
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int n,i,c,sum;
 5     int gcd(int a,int b);
 6     while(~scanf("%d",&n))
 7     {
 8         sum=0;
 9         for(i=1;i<n;i++)
10         {
11             c=gcd(n,i);
12             if(c==1)
13                 sum++;
14         }
15         printf("%d\n",sum);
16     }
17     return 0;
18 }
19 
20 int gcd(int a,int b)
21 {
22     int c;
23     while(a%b!=0)
24     {
25         c=a%b;
26         a=b;
27         b=c;
28     }
29     return b;
30 }

 

posted @ 2016-10-28 19:59  ruoruoruoruo  阅读(126)  评论(0编辑  收藏  举报