历年CSP-J初赛真题解析 | 2014年CSP-J初赛阅读程序(23-26)

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

附上汇总贴:历年CSP-J初赛真题解析 | 汇总_热爱编程的通信人的博客-CSDN博客


#include <iostream>
using namespace std;
int main() {
    int a, b, c, d, ans;
    cin >> a >> b >> c;
    d = a-b;
    a = d+c;
    ans = a*b;
    cout << "Ans=" << ans << endl;
    return 0;
}

第23题

输入:2 3 4

输出:( )

【答案】:Ans=9

【解析】

模拟代入运算,注意输出格式不含空格

#include <iostream>
using namespace std;
int fun(int n) {
    if (n==1) return 1;
    if (n==2) return 2;
    return fun(n-2) - fun(n-1);
}
int main() {
    int n;
    cin >> n;
    cout << fun(n) << endl;
    return 0;
}

第24题

输入:7

输出:( )

【答案】:-11

【解析】

fun(7)=fun(5)-fun(6),…,fun(3)=fun(1)-fun(2)

计算结果fun(7)=-11

#include <iostream>
#include <string>
using namespace std;
int main() {
    string st;
    int i, len;
    getline(cin, st);
    len = st.size();
    for (i=0; i<len; i++) {
        if (st[i]>='a' && st[i]<='z') 
            st[i]=st[i]-'a'+'A';
    }
    cout << st << endl;
    return 0;
}

第25题

输入:Hello, my name is Lostmonkey.

输出:( )

【答案】:HELLO, MY NAME IS LOSTMONKEY.

【解析】

第10到第11行,就是将小写字母转为大写字母

#include <iostream>
using namespace std;
const int SIZE=100;
int main() {
    int p[SIZE];
    int n, tot, i, cn;
    tot = 0;
    cin >> n;
    for (i=1; i<=n; i++) p[i] = 1;
    for (i=2; i<=n; i++) {
        if (p[i]==1) tot++;
        cn = i*2;
        while (cn<=n) {
            p[cn] = 0;
            cn += i;
        } 
    }
    cout << tot << endl;
    return 0;
}

第26题

输入:30

输出:( )

【答案】:10

【解析】

第13至第16行,是为了将i的倍数都置为0。只有质数不是任何数的倍数。30以内的质数有2,3,5,7,11,13,17,19,23,29,共10个

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值