• 【Luogu】【关卡2-11】简单数学问题(2017年10月)【还差三道题】


    火星人  

    麦森数  

    P1403 [AHOI2005]约数研究

    f(n)表示n的约数个数,现在给出n,要求求出f(1)到f(n)的总和。

    解答:有几个1做约数的个数 = n /1; 有几个2做约数的个数 = n /2; 有几个3做约数的个数 = n /3;

    所以直接 对 n / i 求和就是答案。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cmath>
     5 using namespace std;
     6 
     7 int n;
     8 int main () {
     9     cin >> n;
    10 
    11     int  s =0;
    12     for (int i = 1; i <= n; ++i) {
    13         s += n / i;
    14     }
    15     cout << s << endl;
    16 
    17     return 0;
    18 }
    View Code

    进制转换  

    P1147 连续自然数和

    对一个给定的自然数M,求出所有的连续的自然数段,这些连续的自然数段中的全部数之和为M。

    例子:1998+1999+2000+2001+2002 = 10000,所以从1998到2002的一个自然数段为M=10000的一个解。

    解答:直接枚举暴力能过...

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <vector>
     5 #include <cstring>
     6 #include <map>
     7 #include <climits>
     8 #include <algorithm>
     9 #include <cmath>
    10 #include <sstream>
    11 
    12 using namespace std;
    13 
    14 int main() {
    15 
    16     int M, ans = 0;
    17     cin >> M;
    18     for (int first = 0; first <= M / 2; ++first) {
    19         for (int second = first; second <= M; ++second) {
    20             ans += second;
    21             if (ans > M) {
    22                 ans = 0;
    23                 break;
    24             }
    25             if (ans == M) {
    26                 cout << first << " " << second << endl;
    27                 ans = 0;
    28                 break;
    29             }
    30         }
    31         ans = 0;
    32     }
    33     return 0;
    34 }
    View Code

    P1029 最大公约数和最小公倍数问题

    给出两个正整数的最大公约数x和最小公倍数y,求满足这样条件的正整数的对数。

    解答:假设这两个正整数是p,q; 最大公约数,最小公倍数是x,y。那么有这么一条浅显的道理: p * q =  x * y

    枚举p,用p计算q, 然后计算gcd是不是x。 gcd怎么写---必会。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 int gcd(int small, int big) {
     5     if (small == 0) return big;
     6     return gcd(big % small, small);
     7 }
     8 
     9 int main() {
    10     int x, y;
    11     cin >> x >> y;
    12     int ans = 0;
    13     for (int i = x; i * i < x * y; ++i) {
    14         int p, q;
    15         if (y % i != 0) {
    16             continue;
    17         }
    18         p = i, q = x * y / i;
    19         //printf("p = %d, q = %d gcd[%d]
    ", p ,q, gcd(p, q));
    20         if (gcd(p, q) == x) {
    21             ++ans;
    22         }
    23     }
    24     cout << ans * 2 << endl;
    25     return 0;
    26 }
    View Code
  • 相关阅读:
    禁用网络连接后无法访问本机数据库的问题
    DevExpress使用笔记
    DEV控件 皮肤问题
    SQLServer2008导入Excel遇到的问题
    InnoSetup使用笔记
    SQLServer清空数据库中所有表的数据
    MS SQL Server2000转换成MySQL
    由MySQL登录不了引发的一些问题
    onerror事件
    DIV+CSS一些小小的技巧
  • 原文地址:https://www.cnblogs.com/zhangwanying/p/7637018.html
Copyright © 2020-2023  润新知