青少年软件编程(C语言)等级考试试卷(三级)
分数:100  题数:5
一、编程题(共5题,共100分)
1.
数对
 
数对
给定2到15个不同的正整数,你的任务是计算这些数里面有多少个数对满足:数对中一个数是另一个数的两倍。
比如给定1 4 3 2 9 7 18 22,得到的答案是3,因为2是1的两倍,4是2个两倍,18是9的两倍。
时间限制:1000
内存限制:65536
输入
一行,给出2到15个两两不同且小于100的正整数。最后用0表示输入结束。
输出
一个整数,即有多少个数对满足其中一个数是另一个数的两倍。
样例输入
1 4 3 2 9 7 18 22 0
样例输出
3
#include<stdio.h>
int main(){
    int i=0,l=0,k,a[255],sum=0;
        scanf("%d",&a[0]);
        for(i=0;a[i]!=0;)
        scanf("%d",&a[++i]);
        for(l=0;l<i;l++)
        for(k=0;k<i;k++)
        if(a[k]==2*a[l])
            sum++;
        printf("%d\n",sum);
       
    return 0;
}
试题编号:
试题类型:编程题
试题难度:一般
2.
井和绳子
 
井和绳子
有A, B, C, D, E五家人共用一口井,已知井深不超过k米。A, B, C, D, E的绳长各不相同,而且厘米表示的绳长一定是整数。
从井口放下绳索正好达到水面时:
(a)需要A家的绳n1条接上B家的绳1条
(b)需要B家的绳n2条接上C家的绳1条
(c)需要C家的绳n3条接上D家的绳1条
(d)需要D家的绳n4条接上E家的绳1条
(e)需要E家的绳n5条接上A家的绳1条
问井深和各家绳长。
时间限制:1000
内存限制:65536
输入
输入只有1行。包括空格分开的6个整数。 第一个整数k(1 <= k <= 20),代表井的最大深度(单位:米)。 接下来是5个正整数n1, n2, n3, n4, n5。这五个整数的含义见上面的题目描述。
输出
输出只有1行。 如果到了可行解,就输出6个整数,用空格分开,分别代表井的深度和A, B, C, D, E的绳长(单位都是厘米)。 如果有多组可行解,输出井的深度最小的那组解。 如果不存在可行解,就输出一行: not found
样例输入
10 2 3 4 5 6
样例输出
721 265 191 148 129 76
#include <bits/stdc++.h>
using namespace std;
int main(){
    int k,n1,n2,n3,n4,n5;
    int a,b,c,d,e,len;
    cin>>k>>n1>>n2>>n3>>n4>>n5;
    for(len=1;len<=k*100;len++){
        for(a=1;a<=len;a++){
            b=len-a*n1;
            c=len-b*n2;
            d=len-c*n3;
            e=len-d*n4;
            if(a==b||a==c||a==d||a==e||b==c||b==d||b==e||c==d||c==e||d==e)continue;
            if(e*n5+a==len){
                printf("%d %d %d %d %d %d",len,a,b,c,d,e);
                return 0;
            }
        }
    }
    printf("not found");
    return 0;
}
试题编号:
试题类型:编程题
c编程必背100题
3.
爬楼
 
爬楼
已知楼梯的数量,可以每次走2级或者3级,求不同的走法数
例如:楼梯一共有7级,一共3种方法:2 2 3或者 2 3 2 或者 3 2 2。
时间限制:1000
内存限制:65536
输入
输入包含若干行,每行包含一个正整数N,代表楼梯级数,1 <= N <= 50。 最后一行为0,表示测试结束。
输出
不同的走法数,每一行输入对应一行输出
样例输入
7
0
样例输出
3
试题编号:
试题类型:编程题
标准答案:
试题难度:一般
4.
表达式求值
 
表达式求值
输入一个布尔表达式,请你输出它的真假值。
比如:( V | V ) & F & ( F | V )
V表示true,F表示false,&表示与,|表示或,!表示非。
上式的结果是F
时间限制:1000
内存限制:65536
输入
输入包含多行,每行一个布尔表达式,表达式中可以有空格,总长度不超过1000
输出
对每行输入,如果表达式为真,输出"V",否则出来"F"
样例输入
( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))
样例输出
F
V
V
试题编号:
试题类型:编程题
标准答案:
#include<cstdio>
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<char,int>index;
char pri[7][7]={ {'>','<','<','<','>','>'},
                {'>','>','<','<','>','>'},
                {'>','>','=','<','>','>'},
                {'<','<','<','<','=','0'},
                {'>','>','>','0','>','>'},
                {'<','<','<','<','0','='}};
char get(char a,char b){
    int ida=index[a],idb=index[b];
    return pri[ida][idb];
}
void solve(string s){
    bool n[100];
    char o[100],x,op;
    int ntop=0,otop=0;
    int i=0;
    o[otop++]='#';
    for (i=0;i<s.length();i++)
    {
        if (s[i]=='V')    n[ntop++]=1;
        else if (s[i]=='F') n[ntop++]=0;
        else //op
        {
            x=get(o[otop-1],s[i]);
            while(x=='>')
            {
                op=o[otop-1];otop--;
                switch (op)
                {
                    case '|': n[ntop-2]=n[ntop-2]|n[ntop-1];ntop--;break;
                    case '&': n[ntop-2]=n[ntop-2]&n[ntop-1];ntop--;break;
                    case '!': n[ntop-1]=1-n[ntop-1];break;
                }
                x=get(o[otop-1],s[i]);
            }
            if (x=='=')
                otop--;
            else if (x=='<')
                o[otop++]=s[i];
        }
    }
    if (n[0]==1)cout<<"V"<<endl;
    else cout<<"F"<<endl;
}
int main(){
    index['|']=0;index['&']=1;index['!']=2;index['(']=3;index[')']=4;index['#']=5;//初始化索引
    int i;
    string s,temp;
    while(getline(cin,s)){
        i=0;temp="";
        while(i<s.length()){
            while(s[i]==' ')i++;
            temp+=s[i]; i++;
        }
        temp+="#";
        solve(temp);
    }
    return 0;
}