2001年北理复试上机题目A

1. 编写程序,计算下列分段函数y=f(x)的值。

y= -x+2.5 0<= x <2

y=2-1.5(x-3)(x-3) 2<= x <4

y=x/2-1.5 4<= x <6 

#include<cstdio>
int main()
{
    int x, y;
    scanf("%d", &x);
    if (x >= 0 && x < 2)y = 2.5 - x;
    if (x >= 2 && x < 4)y = 2 - 1.5*(x - 3)*(x - 3);
    if (x >= 4 && x < 6)y = x / 2 - 1.5;
    printf("%d", y);
    return 0;
}

2.编写程序,读入一个整数 N。若 N 为非负数,则计算 N 到 2N 之间的整数和;若 N 为一个负数,则求 2N 到 N 之间的整数和。

#include<cstdio>
int main()
{
    int N, ans = 0;
    scanf("%d", &N);
    if (N >= 0)
    {
        for (int i = N; i < 2 * N; i++)ans += i;
    }
    if (N < 0)
    {
        for (int i = 2*N; i < N; i++)ans += i;
    }
    printf("%d", ans);
    return 0;
}

3.设N是一个四位数,它的 9 倍恰好是其反序数(例如:1234的反序数是4321),求N的值。

#include<cstdio>
int main()
{
    int i, j;
    for (i = 1000; i < 1200; i++)
    {
        j = 9 * i;
        if (j / 1000 == i % 10 && (j % 1000) / 100 == (i / 10) % 10
            && (j % 100) / 10 == (i / 100) % 10 && j % 10 == i / 1000)break;
    }
    printf("%d", i);
    return 0;
}

4.N个人围成一圈顺序编号,从1号开始按1、2、3顺序报数,报3者退出圈外,其余的人再从1、2、3开始报数,报3的人再退出圈外,依次类推。请按退出顺序输出每个退出人的原序号。要求使用环型链表编程。

#include <iostream>
using namespace std;
 
struct p
{
    int num;
    p *next;
};
 
int main()
{
    p *head,*h,*t;
    int i,n;
    cin>>n;
 
    h=new p;
    h->num=1;
 
    head=h;
    h->next=NULL;
    for(i=2; i<=n; i++)
    {
        t=new p;
        t->num=i;
        h->next=t;
        h=t;
        h->next=NULL;
    }
 
    h->next=head;
    h=head;
 
    i=2;
    h=h->next;
    cout<<"出队序列为:"<<endl;
 
    while(n!=0)
    {
        if(i%3==0)
        {
            cout<<h->num<<" ";
            head->next=h->next;
            h=head->next;
            n--;
            i=1;
        }
        i++;
        head=h;
        h=h->next;
    }
    cout<<endl;
 
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/yun-an/p/11331051.html