TOJ1100

http://acm.tju.edu.cn/toj/showp1100.html

个人问题在于:对题中

Answers must be rounded to six digits after the decimal point.

这句话的要求理解不到位,测试了几个结果都是正确的,但是总是WA,后来上网参考其他代码才发现是自己在输出时对格式的要求不到位。

else printf("%.6f\n", sqrt((total*6.0)/no));

以上应该是正确的输出语句,对输出格式(小数点后保留6位)做了要求。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int num[50];

int GCD(int a, int b){
    if(a>b) return GCD(a-b,b);
    if(a<b) return GCD(a,b-a);
    else return a;
}

int main(){
    int N;
    int yes, no, total;
    while(scanf("%d", &N) && N != 0){
        yes = 0;
        no = 0;
        total = 0;
        for(int i = 0; i < N; i++){
            scanf("%d", &num[i]);
        }
        for(int i = 0; i < N-1; i++){
            for(int j = i+1; j < N; j++){
                int judge = GCD(num[i], num[j]);
                if(judge == 1) no++;
                else yes++;
                total++;
            }
        }
        //printf("%d %d %d %f", total, yes, no, (total*6)/no);
        if(total == yes) printf("No estimate for this data set.\n");
        else printf("%.6f\n", sqrt((total*6.0)/no));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/rr572078051/article/details/82936676
今日推荐