模擬考試參考程式碼

zJx-Lm發表於2024-05-30

模擬4 :

1.
Code
#include <iostream>
#include <iomanip>
using namespace std;

double averageAtomicWeight(int O, int C, int N, int S, int H)
{
    /********Program********/
	return (O*15.9994+C*12.011+N*14.0067+S*32.066+H*1.0079)/(double)(O+C+N+S+H);
    
	
    /********  End  ********/
}

int main()
{   
	int O, C, N, S, H;
	cin >> O >> C >> N >> S >> H;
	double ret = averageAtomicWeight(O, C, N, S, H);
	cout << setprecision(4) << fixed << ret << endl;
	return 0;
}

2.
Code
#include <iostream>
using namespace std;

//計算a和b的最大公約數
int gcd(int a, int b)
{
    /********Program********/
	while(b!=0){
        int temp=b;
        b=a%b;
        a=temp;
    }
	return a;
    /********  End  ********/
}

//計算a和b的最小公倍數
int lcm(int a, int b)
{
    /********Program********/
	return a * b / gcd(a, b);
	
    /********  End  ********/
}

int main()
{
    int a, b;
    cin >> a >> b; 
    cout << gcd(a, b) << " " << lcm(a, b) << endl; 
    return 0;
}
3.
Code
#include <iostream>
using namespace std;

double hamonic(int n) 
{
/**********Program**********/
    double ans=0; for(int i=1;i<=n;i++) ans+=1.0/(double)i;
	return ans;
/**********  End  **********/
}


int main()
{
    int n;
	cin >> n;
    cout << hamonic(n) << endl;
    return 0;
}
4.
Code
#include <iostream>
using namespace std;

int antiColor(int type, int color) {

/********Program********/
    if(type==0) return color^1;
    else if(type==1) return 255-color;
    return -1;
/********  End  ********/
}
int main()
{
    int type, color;
    cin >> type >> color;
    cout << antiColor(type, color) << endl;
    return 0;
}
5.
Code
#include <iostream>
using namespace std;

void convert(ostream &out, unsigned long x)
{ 
/*********Program*********/
    if(x<10){
        out<<x;
    }else{
        convert(out,x/10);
        out<<'-'<<x%10;
    }
	
	
/*********  End  *********/
}

int main()
{
    unsigned long A;
	cin >> A;
    convert(cout, A);
	cout << endl;
    return 0;
}
6.
Code
#include <iostream>
#include <cmath>
using namespace std;

#define N 10

int closed_index(double A[], int len) 
{
/**********Program**********/
    double sum = 0.0;
    for (int i = 0; i < len; i++) {
        sum += A[i];
    }
    double average = sum / len;

    double min_diff = std::fabs(A[0] - average);
    int min_index = 0;
    for (int i = 1; i < len; i++) {
        double diff = fabs(A[i] - average);
        if (diff < min_diff) {
            min_diff = diff;
            min_index = i;
        }
    }

    return min_index;
	
	
/**********  End  **********/
}

int main()
{
	double A[N];
	for (int i = 0; i < N; i++)
		cin >> A[i];
    cout << closed_index(A, N) << endl;
	return 0;
} 
7.
Code
#include <iostream>
#include <cmath>
using namespace std;

#define N 10

int rev_num(int A[], int len) 
{

/**********Program**********/
	int result=0;
    for(int n=0;n<len;n++){
		int num=0;
		for(int j=n+1;j<len;j++){
			if(A[j]>A[n]) num++;
		}
		result+=num;
	}
	return result;

/**********  End  **********/
}

int main()
{
	int A[N];
	for (int i = 0; i < N; i++)
		cin >> A[i];
    cout << rev_num(A, N) << endl;
	return 0;
}

8.
Code

#include <iostream>
using namespace std;
#include<cstring>
int find_last(char *str, char ch)
{
/**********Program**********/
    char *ma=strrchr(str,ch);
	int result=ma-str;
	return result;
/**********  End  **********/
}

int main()
{
	char str[200];
	char ch;
	cin >> str;
	cin >> ch;
    cout << find_last(str, ch) << endl;
	return 0;
}
9.
Code

#include <iostream>
using namespace std;
#define SIZE 5

struct partinfo {
    int  id;         // 參賽編號
    char name[10];   // 姓名
    char type;       // 組別:'s'、'q'分別代表少年組和青年組
    float s1;        // 初賽成績
    float s2;        // 複賽成績
    float s3;        // 決賽成績
};

char* get(partinfo list[], int n)
{
	/**********Program**********/
    double ansl=100.0; int fal=-1;
    for(int i=0;i<n;i++) if(list[i].type=='s') if(list[i].s1*0.3+list[i].s2*0.3+list[i].s3*0.4<ansl) { ansl=list[i].s1*0.3+list[i].s2*0.3+list[i].s3*0.4; fal=i; }
    return list[fal].name;
	/**********  End  **********/
}

int main()
{
   
	partinfo list[SIZE];
	for (int i = 0; i < SIZE; i++)
        cin >> list[i].id >> list[i].name >> list[i].type >> list[i].s1 >> list[i].s2 >> list[i].s3;
	
    cout<< get(list, SIZE)<<endl;
	return 0;
}
10.
Code
#include <iostream>
using namespace std;

struct node
{
    int data;
    node *next;
};

node *deletesushu(node *head)
{
/**********Program**********/
    node dummy;
	dummy.next=head;
	node *prev=&dummy;
	node *curr=head;
	while(curr!=NULL){
		int num =curr->data;
		bool isPrime=true;
		if(num<2){
			isPrime=false;

		}else{
			for(int i=2;i*i<=num;++i){
				if(num%i ==0){
					isPrime=false;
					break;
				}
			}
		}
		if(isPrime){
			prev->next=curr->next;
			delete curr;
			curr=curr->next;
		}else{
			prev=curr;
			curr =curr->next;
		}
	}
	return dummy.next;
	
/**********  End  **********/
}

void output(node* head);

int main()
{
	node* head = NULL, * cur = NULL, * tmp;
	for (int i = 0; i < 10; i++)
	{
		tmp = new node;
		cin >> tmp->data;
		tmp->next = NULL;
		if (head == NULL) {
			head = tmp;
			cur = head;
		}
		else {
			cur->next = tmp;
			cur = cur->next;
		}
	}

	head = deletesushu(head);
	output(head);
    return 0;
}

void output(node* head) {
	while (head)
	{
		cout << head->data;
		head = head->next;
		if (head) cout << "->";
	}
	cout << endl;
} 

模擬5 :

1.
Code
#include <iostream>
#include <iomanip>

using namespace std;

int bmiIndex(double weight, double height)
{
/**********Program**********/
    double bim=weight/height/height;
    if(bim<18.5) return 1;
    else if(bim<24) return 2;
    else if(bim<28) return 3;
    return 4;
/**********  End  **********/
}


int main()
{
    double w,h;
    cin >> w >> h;

    cout << bmiIndex(w,h) << endl;
    
    return 0;
}
2.
Code
#include <iostream>
using namespace std;

int lottery(int a, int b)
{
/**********Program**********/
    if(a==b) return 1;
    else if(a/1000==b/1000&&a%10==b%10) return 2;
    else if(a/1000==b/1000) return 3;
    return -1;
/**********  End  **********/ 
}


int main()
{
    int num, lucky;
    cin >> num;
    cin >> lucky;

    cout << lottery(num, lucky) << endl;
    
    return 0;
}
3.
Code
#include <iostream>

using namespace std;

int deficientNumber(int num)
{
/**********Program**********/
    int sum=0,s=num;
    for(int i=1;i<num;i++) if(s%i==0) { sum+=i; }
    return max(0,num-sum);
/**********  End  **********/
}

int main()
{
    int n;
    cin >> n;
    
    cout << deficientNumber(n) << endl;

    return 0;
}
4.
Code
#include <iostream>
using namespace std;

int digitTrim(int n)
{
/**********Program**********/
    if(n==0) return 0;
    if(n%10!=0) return digitTrim(n/10)*10+n%10;
    else return digitTrim(n/10);
 
 
/**********  End  **********/
}

int main()
{
    int n;
    cin >> n;

    cout << digitTrim(n) << endl;
    
    return 0;
}
5.
Code
#include <iostream>
#include <cmath>
using namespace std;

void unitVector(double a[], double b[]) 
{
/**********Program**********/
    double ans=0;
    for(int i=0;i<5;i++) ans+=a[i]*a[i];
    ans=sqrt(ans);
    for(int i=0;i<5;i++) b[i]=a[i]/ans;
/**********  End  **********/
}


int main()
{
    double a[5];
    double b[5];
    for(int i = 0; i < 5; i++)
    {
        cin >> a[i];
    }

    unitVector(a, b);

    for (int i = 0; i < 5; i++)
        cout << b[i] << "\t";
    cout << endl;

    return 0;
}
6.
Code
#include <iostream>
using namespace std;

void extract(char str[], int s, int e, int n, char res[])
{
 /**********Program**********/
    int count=0;
    for(int a=s;a<e;a+=n){
        res[count]=str[a];
        count++;
    }
    res[count]='\0';
 /**********  End  **********/
}
int main()
{
    int n;
    char s[100];
    char e[100];

    for (int j = 0; j < 50; j++)
    {
        cin >> n;
        s[j] = n % 26 + 'a';
    }
    s[50] = 0;

    extract(s, 2, 40, 3, e);

    cout << e << endl;
    
    return 0;
}
7.
Code
#include <iostream>
#include <cstring>
using namespace std;

void run_length_decoding(char* src, char* dst)
{
/**********Program**********/
    char num,ch;
    while(true) {
        num=*src++; 
        if(num=='\0') break;
        ch =*src++;
        for(char i='1';i<=num;i++) {
            *dst++=ch;
        }
    }
    *dst='\0';
/**********  End  **********/
}

int main()
{
    char s[1000], t[1000];
    cin >> s;

    run_length_decoding(s, t);

    cout <<  t << endl;
    
    return 0;
}
8.
Code
#include <iostream>
#include<string>
using namespace std;

struct stuinfo {
    int  id;  // 學號
    string name; // 姓名
    char gender; // 性別
    int  score1; // 課程1成績
    int  score2; // 課程2成績
    int  score3; // 課程3成績
    int  score4; // 課程4成績
};

double statistics(stuinfo stus[], int n)
{
    /**********Program**********/
    int num1=0,num2=0; double sum1=0,sum2=0;
    for(int i=0;i<n;i++) { 
        if(stus[i].gender=='m') num1++,sum1+=stus[i].score1+stus[i].score2+stus[i].score3+stus[i].score4;
        else num2++,sum2+=stus[i].score1+stus[i].score2+stus[i].score3+stus[i].score4;
    }
    if(num1) sum1/=(double)num1; 
    if(num2) sum2/=(double)num2;
    return sum1-sum2;
    /**********  End  **********/
}

int main()
{
    int n;
    stuinfo ptr[11] = {
        {104, "Zhang San", 'f', 94, 97, 91, 94},
        {105, "Li Si", 'm', 90, 86, 90, 75},
        {106, "Wang Wu", 'm', 94, 88, 91, 86},
        {107, "Zhzo Liu", 'f', 92, 95, 81, 87},
        {108, "Qian, Jiu", 'f', 99, 90, 82, 91},
        {109, "Ding Yi", 'm', 98, 86, 80, 94},
        {110, "Zhou Er", 'f', 90, 90, 91, 93},
        {111, "Chen Ba", 'm', 92, 83, 92, 77},
        {112, "Zhu Qi", 'f', 90, 85, 88, 81},
        {113, "Wu Shi", 'm', 91, 82, 89, 86},
        {114, "Du Du", 'm', 93, 84, 82, 87} 
    };

    cin >> n;

    cout << statistics(ptr, n) << endl;

    return 0;
}
9.
Code
#include <iostream>
using namespace std;

struct node { //連結串列節點定義
    int data;
    node* next;
};

node* even(node* h)
{
    /**********Program**********/
    int i=1; node *hh=new node,*las=hh; hh->data=h->data; hh->next=NULL; h=h->next;
    while(h) {
        i^=1;
        if(i) { node * t=new node; t->data=h->data; t->next=NULL; las->next=t; las=t; }
        h=h->next;
    }
    return hh;
    /**********  End  **********/
}

int main()
{
    node* head = NULL, * t;
    int n, j;
    cin >> n;
    n = n % 47;

    head = new node;
    head->data = n;
    t = head;
    for (j = 0; j < 20; j++)
    {
        cin >> n;
        n = n % 47;
        t->next = new node;
        t = t->next;
        t->data = n;
    }
    t->next = NULL;

    t = even(head);

    while (t)
    {
        cout << t->data << "->";
        t = t->next;
    }
    cout << endl;

    return 0;
}
10.
Code
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
using namespace std;

int analysis(char *fileName, int len)
{
/**********Program**********/
	ifstream fil(fileName,ios::in);
	int n; string s; int ans=0;
	fil>>n;
	while(n!=-1) {
		for(int i=0;i<n;i++) { fil>>s; if(s.length()>len) ans++; }
		fil>>n;
	}
	return ans;
/**********  End  **********/
}

int main()
{   
	//忽略閱讀
	int c;
	cin >> c;
	cout << analysis("/data/workspace/myshixun/step1/words.txt", c) << endl;
	
	//忽略閱讀結束
	return 0;
}

相關文章