Exam2001A

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  

C++

#include <iostream>
using namespace std;

int main()
{
	double x,y;
	cout<<"input the x:"<<endl;
	cin>>x;
	if(x<0||x>=6)
	{
		cout<<"x out of the bounds"<<endl;
		return 0;
	}
	if(x>=0&&x<2)
	{
		y=-x+2.5;
	}
	else if(x>=2&&x<4)
	{
		y=2-1.5*(x-3)*(x-3);
	}
	else if(x>=4&&x<6)
	{
		y=x/2-1.5;
	}
	cout<<"y="<<y<<endl;
	return 0;
}

Go

package exam2001a

import "fmt"

//Y is used to calculate y based on x
func Y(x float64) {
	if x < 0 || x >= 6 {
		fmt.Println("X out of bounds")
		return
	}
	var y float64
	if x >= 0 && x < 2 {
		y = -x + 2.5
	} else if x >= 2 && x < 4 {
		y = 2 - 1.5*(x-3)*(x-3)
	} else if x >= 4 && x < 6 {
		y = x/2 - 1.5
	}
	fmt.Printf("y=%.2f", y)

}

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

C++

#include <iostream>
using namespace std;

int main()
{
	int n,sum=0,flag=1;		//flag=1 表示 n 为正数,flag=-1 表示 n 为负数 
	cin>>n;
	if(n<0)
	{
		flag=-1;
		n=-n;
	}
	for (int i=n;i<=2*n;i++)
	{
		sum+=i;
	}
	if(flag==1)
	{
		cout<<"sum="<<sum<<endl;
	}
	else
	{
		cout<<"sum="<<-sum<<endl;
	}
	return 0; 
}

Go

package exam2001a

//Sum is used to compute the sum between n and 2*n
func Sum(n int) int {
	sum := 0
	symbol := 1
	if n < 0 {
		symbol = -1
		n = -n
	}
	for i := n; i <= 2*n; i++ {
		sum += i
	}
	if symbol == -1 {
		return -sum
	}

	return sum

}

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

C++

#include <iostream>
using namespace std;

int main()
{
	int a[4];
	int t;
	for(int i=1000;i<10000;i++)
	{
		t=i;
		a[0]=t%10;
		a[1]=t/10%10;
		a[2]=t/100%10;
		a[3]=t/1000%10;
		t=a[0]*1000+a[1]*100+a[2]*10+a[3];
		if(t==9*i)
		{
			cout<<"N="<<i<<endl;
		}
	}
	return 0;
}

Go

package exam2001a

import "fmt"

//Inverse is used to find N
func Inverse() {
	var a [4]int
	for i := 1000; i < 10000; i++ {
		t := i

		for k := 0; k < 4; k++ {
			a[k] = t % 10
			t /= 10
		}
		t = a[0]*1000 + a[1]*100 + a[2]*10 + a[3]
		if t == 9*i {
			fmt.Println(i)
		}

	}
}

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

C++

#include <iostream>
using namespace std;

typedef struct LNode
{
	int data;
	LNode *next;
	
}LNode,*Linklist;
	
int main()
{
	int n,k;
	LNode *pre;
	cin>>n;
	if(n<1)
	{
		return 0;
	}
	
	//建立链表 
	Linklist L,r;
	L=new LNode;
	L->data=1;
	L->next=NULL;
	r=L;
	for(int i=2;i<=n;i++)
	{
		LNode *p;
		p=new LNode; 
		p->data=i;
		r->next=p;
		r=p;
	}
	r->next=L;
	
	//输出相应序号并释放空间 
	r=L;
	while(n)
	{
		k=1;
		while(k<3)
		{
			pre=r;
			r=r->next;
			k++;
		
		}	
		cout<<r->data<<"  ";
		pre->next=r->next; 
		delete r;
		r=pre->next;
		n--;
	}
	cout<<endl;
	return 0; 
}

Go

package exam2001a

import "fmt"

//Node represents the node in a single linked list
type Node struct {
	data int
	next *Node
}

//Order is used to sort according to certain rule
func Order(n int) {
	//建立链表
	if n < 1 {
		fmt.Println("N is a wrong data")
		return
	}
	var L = new(Node)
	var pre, r *Node
	L.data = 1
	L.next = nil
	r = L
	for i := 2; i <= n; i++ {
		// var p = new(Node)
		// p.data = i
		// r.next = p
		// r = p
		//下方与注释部分实现功能一致
		p := Node{
			data: i,
			next: nil,
		}
		r.next = &p
		r = &p
	}
	r.next = L

	//输出序号并释放空间
	r = L
	for n > 0 {
		k := 1
		for ; k < 3; k++ {
			pre = r
			r = r.next
		}
		fmt.Printf("%d  ", r.data)
		pre.next = r.next
		r = pre.next
		n--
	}

}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy