北航機試題2016(題目+代碼)

https://apriljia.com/2018/07/15/%E5%8C%97%E8%88%AA%E4%B8%8A%E6%9C%BA%E8%AF%95%E9%A2%982016%EF%BC%88%E9%A2%98%E7%9B%AE%E4%BB%A3%E7%A0%81%EF%BC%89/

16年只有兩道題。

1.給定一個數n,將這個數的各位順序顛倒,成爲逆序數m。

例如1234的逆序數是4321.

如果m是n的k倍(k爲整數),那麼輸出n*k=m

例如輸入1089

輸出1089*9=9801

如果m不是n的整數倍,那麼輸出n和m的逆序數

例如輸入1234

輸出1234 4321

例如輸入23200

輸出23200 00232

已知輸入開頭不包括多餘的零

#include "stdafx.h"
#include <iostream>
#include <string>
#include "math.h"
#include "stdio.h"
#include "string.h"
#include <vector>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;
int max(int x,int y){
	return x>y?x:y;
}


int main(int argc, char* argv[])
{
	int n1,n2;
	int tmp;
	char str1[100]={0};
	char str2[100]={0};
	scanf("%d",&n1);
	//cout<<n1<<endl;

	itoa(n1,str1,10);
	//cout<<str1<<endl;
	reverse(str1,str1+strlen(str1));
	//cout<<str1<<endl;
	n2=atoi(str1);
	//cout<<n2<<endl;
	if(n2%n1==0){
		printf("%d*%d=%s",n1,n2/n1,str1);
	}else{
		printf("%d %s",n1,str1);

	}

	return 0;
}

2.給一個c語言的enum定義語句,輸出enum中規定的各項及其對應的數值。

輸入 enum BOOL{true,false};

輸出

true 0

false 1

輸入 enum date{JAN=1,FEB,MAR,APR,MAY,JUN,JULY,AUG,SEP,OCT,NOV,DEC,MON=1,TUE,WED,THU,FRI,SAT,SUN,found=1949};

輸出

JAN 1

FEB 2

MAR 3

APR 4

MAY 5

JUN 6

JULY 7

AUG 8

SEP 9

OCT 10

NOV 11

DEC 12

MON 1

TUE 2

WED 3

THU 4

FRI 5

SAT 6

SUN 7

found=1949

 

#include "stdafx.h"
#include <iostream>
#include <string>
#include "math.h"
#include "stdio.h"
#include "string.h"
#include <vector>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
	char inputArr[100];
	string str,sub,newcountstr;
	int startindex,endindex,equalindex,count;
	bool stop=0;
	gets(inputArr);
	str=inputArr;

	startindex=0;
	startindex=str.find("{",startindex)+1;
	count=0;
	while(!stop){
		endindex=str.find(",",startindex);
		if(endindex!=string::npos){
			sub=str.substr(startindex,endindex-startindex);
			startindex=endindex+1;

			equalindex=sub.find('=');
			if(equalindex==string::npos){
				cout<<sub<<count<<endl;
				count++;
			}else{
				newcountstr=sub.substr(equalindex+1);
				count=atoi(newcountstr.c_str());
				sub.erase(sub.begin()+equalindex,sub.end());
				cout<<sub<<count<<endl;
			}
			

		}else{
			endindex=str.find("}",startindex);
			sub=str.substr(startindex,endindex-startindex);
			
			equalindex=sub.find('=');
			if(equalindex==string::npos){
				cout<<sub<<count<<endl;
				count++;
			}else{
				newcountstr=sub.substr(equalindex+1);
				count=atoi(newcountstr.c_str());
				sub.erase(sub.begin()+equalindex,sub.end());
				cout<<sub<<count<<endl;
			}

			stop=1;
		}

	

	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章