洛谷刷题C++语言 | P4956 Davor

学习C++从娃娃抓起!记录下洛谷C++学习和备考过程中的题目,记录每一个瞬间。

附上汇总贴:洛谷刷题C++语言 | 汇总_热爱编程的通信人的博客-CSDN博客


【题目描述】

在征服南极之后,Davor 开始了一项新的挑战。下一步是在西伯利亚、格林兰、挪威的北极圈远征。他将在 2018 年 12 月 31 日开始出发,在这之前需要一共筹集 n 元钱。他打算在每个星期一筹集 x 元,星期二筹集 x+k 元,……,星期日筹集 x+6k 元,并连续筹集 52 个星期。其中 x,k 为正整数,并且满足 1≤x≤100。

现在请你帮忙计算 x,k 为多少时,能刚好筹集 n 元。

如果有多个答案,输出 x 尽可能大,k 尽可能小的。注意 k 必须大于 0。

【输入】

The first line of input contains the integer N (1456 ≤ N ≤ 145600), the number from the task.

【输出】

The first line of output must contain the value of X (0 < X ≤ 100 ), and the second the value of K (K > 0 ).

【输入样例】

1456

【输出样例】

1

1

【代码详解】

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,x,k,ans;
    cin >> n;
    // //方法一:数学方法计算
    // n /= 52;
    // n /= 7;  //n = x + 3k
    // x = n - 3;
    // if (x>100) x = 100;
    // for (int i=x; i>=0; i--) {
    //     if ((n-i)%3==0) {
    //         cout << i << endl << (n-i)/3;
    //         break;
    //     }
    // }
    //方法二
    n /= 52;
    for (x=100; x>=0; x--) {
        if ((n-x*7)%21==0 && (n-x*7)>0) {
            cout << x << endl << (n-x*7)/21;
            break;
        }
    }
    return 0;
}

【运行结果】

1456
1
1

猜你喜欢

转载自blog.csdn.net/guolianggsta/article/details/132642279