北航上机试题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;
}

猜你喜欢

转载自blog.csdn.net/mingzhiqing/article/details/81055641
今日推荐