zoukankan      html  css  js  c++  java
  • 杭电oj平台上的11页题目代码:acm编程题:hdu1012,hdu1017

    //1012
    /*思路:题目的意思就是将正整数(0~9)求阶乘倒数的和,需要设置一个函数用来求阶乘,在main函数中,0~9循环一次,调用阶乘函数求倒数并累加作为和,求出相应的e并输出*/
    #include<iostream>
    #include<iomanip>
    using namespace std;
    //求n的阶乘的函数
    int func(int n)
    {
    if (n==0||n==1)
    {
    return 1;
    }
    return n * func(n - 1);
    }
    int main()
    {
    int n, e;
    int i;
    double arr[10], sum = 0;//sum是和
    cout << 'n' << ' ' << 'e' << endl;
    cout << '-' << ' ' << "-----------"<< endl;
    for ( i = 0; i <= 9; i++)
    {
    arr[i] = 1.0 / func(i);
    sum += arr[i];
    //setprecision是一个计算机函数,功能是控制输出流显示浮点数的有效数字个数[1] ,如果和fixed合用的话,可以控制小数点后面有几位。
    if (i==0||i==1)
    {
    cout << i << ' '<<fixed << setprecision(0)<<sum << endl;
    }
    else if (i == 2)
    {
    cout << i << ' '<<fixed << setprecision(1) << sum << endl;
    }
    else
    {
    cout << i << ' ' <<fixed<< setprecision(9)<<sum << endl;
    }
    }
    return 0;
    }

    //1017
    //思路:遍历小于n的整数,用二重循环,i和j(分别代表题目中的a和b),看 (a^2+b^2 +m)/(ab) is an integer,可以看前者(a^2+b^2 +m)和后者(ab)的取模是否为0或者商乘以后者(ab)判断其是否等于前者(a^2+b^2 +m)
    #include<iostream>
    using namespace std;
    int main()
    {
    int n, m;
    int i, j;
    int temp;
    int count1 = 0;//用于记录case个数
    int count2 = 0;//用于记录pairs
    int t;
    while (cin >> t)
    {
    while (t--)
    {
    while (cin>>n>>m)
    {
    if (n==0&&m==0)
    {
    break;
    }
    count1++;
    temp = 0;
    count2++;
    for (i = 0; i < n-1; i++)
    {
    for ( j = i+1; j < n; j++)
    {
    temp = (i*i + j*j + m);
    //解一:看是否能够整除,可以看求余数是不是等于0
    //解二:求出他们的商,并看乘以除数,看是否等于被除数
    /* int x = i*i+j*j+m;
    int y = i*j;
    int temp = x/y;
    if(temp*y==x)num++; */
    if (temp%(i*j)==0)
    {
    count2++;
    }
    }
    }
    cout << "Case " << count1 << ":" << count2 << endl;
    }
    }
    }
    return 0;
    }

     参考博客:http://blog.csdn.net/runner__1/article/details/50193057,http://blog.csdn.net/qq_26919527/article/details/49007943

    一生有所追!
  • 相关阅读:
    378. Kth Smallest Element in a Sorted Matrix
    372. Super Pow
    357. Count Numbers with Unique Digits
    345. Reverse Vowels of a String
    343. Integer Break
    347. Top K Frequent Elements
    344. Reverse String
    338. Counting Bits
    326. Power of Three
    python练习
  • 原文地址:https://www.cnblogs.com/BlueBlue-Sky/p/8528508.html
Copyright © 2011-2022 走看看