算法练习之模拟

本文介绍了几个算法练习题,涉及数值比较、程序运行时间计算、数组元素循环右移、洗牌模拟和最短距离计算等。通过这些题目,旨在锻炼和提升编程能力,理解不同算法的应用场景。
摘要由CSDN通过智能技术生成

1011 A+B和C (15)(15 分)

给定区间[-2^31^, 2^31^]内的3个整数A、B和C,请判断A+B是否大于C。

输入格式:

输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。

输出格式:

对每组测试用例,在一行中输出“Case #X: true”如果A+B>C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)。

输入样例:

4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647

输出样例:

Case #1: false
Case #2: true
Case #3: true
Case #4: false
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int main() {
	int result = 0;
	char A[1000+10], B[1000+10];
	int DA, DB;
	scanf("%s %d %s %d", A, &DA, B, &DB);
	int lenA = strlen(A);
	int lenB = strlen(B);
	int r1 = 0, r2 = 0, cnt1 = 0, cnt2 = 0;
	for (int i = 0; i < lenA; i++) {
		if (A[i]-'0' == DA) cnt1++; 
	} 
	for (int i = 0; i < lenB; i++) {
		if (B[i]-'0' == DB) cnt2++; 
	}
	for (int i = 0; i < cnt1; i++) { r1 = r1*10 + DA; }
	for (int i = 0; i < cnt2; i++) { r2 = r2*10 + DB; } 
	result = r1 + r2;
	
	
	printf("%d\n", result);
 	
	return 0;
}

 

1026 程序运行时间(15)(15 分)

要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间。这个时间单位是clock tick,即“时钟打点”。同时还有一个常数CLK_TCK,给出了机器时钟每秒所走的时钟打点数。于是为了获得一个函数f的运行时间,我们只要在调用f之前先调用clock(),获得一个时钟打点数C1;在f执行完成后再调用clock(),获得另一个时钟打点数C2;两次获得的时钟打点数之差(C2-C1)就是f运行所消耗的时钟打点数,再除以常数CLK_TCK,就得到了以秒为单位的运行时间。

这里不妨简单假设常数CLK_TCK为100。现给定被测函数前后两次获得的时钟打点数,请你给出被测函数运行的时间。

输入格式:

输入在一行中顺序给出2个整数C1和C2。注意两次获得的时钟打点数肯定不相同,即C1 < C2,并且取值在[0, 10^7^]。

输出格式:

在一行中输出被测函数运行的时间。运行时间必须按照“hh:mm:ss”(即2位的“时:分:秒”)格式输出;不足1秒的时间四舍五入到秒。

输入样例:

123 4577973

输出样例:

12:42:59

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n1, n2;
int main() {
	scanf("%lld %lld", &n1, &n2);
	
	ll cnt = (ll)((double)(n2 - n1 + 50));//注意这里应该是50 
	cnt /= 100;
	ll hh = cnt/3600;
	ll mm = cnt%3600/60;
	ll ss = cnt%60;
	if (hh < 10) cout << "0";
	cout << hh << ":";
	if (mm < 10) cout << "0";
	cout << mm << ":";
	if (ss < 10) cout << "0";
	cout << ss << endl;
	return 0;
}

1008 数组元素循环右移问题 (20)(20 分)

一个数组A中存有N(N&gt0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A~0~ A~1~……A~N-1~)变换为(A~N-M~ …… A~N-1~ A~0~ A~1~……A~N-M-1~)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

输入格式:每个输入包含一个测试用例,第1行输入N ( 1<=N<=100)、M(M>=0);第2行输入N个整数,之间用空格分隔。

输出格式:在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。

输入样例:

6 2
1 2 3 4 5 6

输出样例:

5 6 1 2 3 4
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
int a[100 + 10],  temp[100 + 10];
void init() {
	memset(a, 0, sizeof(a));
}
//既然是循环右移,数组中的元素的相对位置是不变的,因此直接变换一下,下标就行; 
int main() {
	while (~scanf("%d %d", &n, &m)) {
		init();
		for (int i = 0; i < n; i++) {
			scanf("%d", &a[i]);
		}
		m = m%n;//注意这里,要取余,因为是循环位移 
		int first = 0;
		for (int i = n-m; i < n; i++) {
			if (first == 0) { printf("%d", a[i]); first = 1; }
			else printf(" %d", a[i]);
		}
		for (int i = 0; i < n-m; i++) {
			if (first == 0) { printf("%d", a[i]); first = 1; }
			else printf(" %d", a[i]);
		}
		printf("\n");
	}
	return 0;
}

 

 

1042 Shuffling Machine (20)(20 分)

Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid "inside jobs" where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.

The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

S1, S2, ..., S13, H1, H2, ..., H13, C1, C2, ..., C13, D1, D2, ..., D13, J1, J2

where "S" stands for "Spade", "H" for "Heart", "C" for "Club", "D" for "Diamond", and "J" for "Joker". A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer K (<= 20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.

Sample Input:

2
36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47

Sample Output:

S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5
#include <bits/stdc++.h>
using namespace std;
//思路就是用另一个数组不断记录每次变换,然后再把变换后的赋给原来的 
int main()
{

	string val[54] = {"S1","S2","S3","S4","S5","S6","S7","S8","S9","S10","S11","S12","S13",
	"H1","H2","H3","H4","H5","H6","H7","H8","H9","H10","H11","H12","H13",
	"C1","C2","C3","C4","C5","C6","C7","C8","C9","C10","C11","C12","C13",
	"D1","D2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13",
	"J1","J2"};
	int n;
	scanf("%d", &n);
	int move[54];
	for (int i = 0; i < 54; i++) scanf("%d", &move[i]);
	
	string val2[54];
	while (n--) {
		for (int i = 0; i < 54; i++) {
			val2[move[i]-1] = val[i];
		}
		for (int i = 0; i < 54; i++) {
			val[i] = val2[i];
		}
	}
	for (int i = 0; i < 53; i++) {
		cout << val[i] << " ";
	}
	cout << val[53] << endl;
	
	return 0;
}

 

1042 Shuffling Machine (20)(20 分)

Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid "inside jobs" where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.

The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

S1, S2, ..., S13, H1, H2, ..., H13, C1, C2, ..., C13, D1, D2, ..., D13, J1, J2

where "S" stands for "Spade", "H" for "Heart", "C" for "Club", "D" for "Diamond", and "J" for "Joker". A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer K (<= 20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.

Sample Input:

2
36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47

Sample Output:

S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5
#include <bits/stdc++.h>
using namespace std; 
char val[5] = {'S', 'H', 'C', 'D', 'J'}; 
int start[100], tmp[100], nxt[100];//记录下标就行,然后变换的是下标就行 
int n;
int main()
{
	memset(start, 0, sizeof(start));
	memset(tmp, 0, sizeof(tmp));
	cin >> n;

	for (int i = 1; i <= 54; i++) start[i] = i;
	for (int i = 1; i <= 54; i++) scanf("%d", &nxt[i]);

	while (n--) {
		for (int i = 1; i <= 54; i++) {
			tmp[nxt[i]] = start[i];
		}
		for (int i = 1; i <= 54; i++) {
			start[i] = tmp[i]; 
		}
	}
	
	for (int i = 1; i <= 54; i++) {
		if (i != 1) printf(" ");
		start[i]--;
		printf("%c%d", val[start[i]/13], start[i]%13+1);
	}	
	printf("\n");
	return 0;
}

1046 Shortest Distance (20)(20 分)

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (in [3, 10^5^]), followed by N integer distances D~1~ D~2~ ... D~N~, where D~i~ is the distance between the i-th and the (i+1)-st exits, and D~N~ is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (<=10^4^), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 10^7^.

Output Specification:

For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

Sample Input:

5 1 2 4 14 9
3
1 3
2 5
4 1

Sample Output:

3
10
7
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100000 + 10;
int n, k, st, ed;
int a[maxn], dis[maxn], sum;
void init() {
	memset(a, 0, sizeof(a));
	memset(dis, 0, sizeof(dis));
	sum = 0;	
}
void swap(int &A, int &B) {
	int temp = 0;
	temp = A;
	A = B;
	B = temp;
}
int main() {
	init();
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		sum += a[i];
		dis[i] = sum;
	}
	scanf("%d", &k);
	while (k-- != 0) {
		scanf("%d %d", &st, &ed);
		if (st > ed) { swap(st, ed); }
		int shortdis = 0;
		if (ed == 1) {
			printf("0\n");
		}
		else {
			if (st == 1) {
				shortdis = min(dis[ed-1], dis[n]-dis[ed-1]);	
			}
			else {
				shortdis = min(dis[ed-1]-dis[st-1], dis[n]-dis[ed-1]+dis[st-1]);
			}
			printf("%d\n", shortdis);
		}
	}
	return 0;
}

 

1010 一元多项式求导 (25)(25 分)

设计函数求一元多项式的导数。(注:x^n^(n为整数)的一阶导数为n*x^n-1^。)

输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是0,但是表示为“0 0”。

输入样例:

3 4 -5 2 6 1 -2 0

输出样例:

12 3 -10 1 6 0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a, b;
char ch;
int main() {
    int first = 0;
    do {
        scanf("%d %d", &a, &b);
        if (first == 0) {
            if (b != 0) { printf("%d %d", a*b, b-1); }
            else { printf("0 0"); }//零多项式的定义就是只有一个数并且指数为0,求导后系数指数都为0
            first = 1;
        }
        else {
            if (b != 0) printf(" %d %d", a*b, b-1);
        }


        ch = getchar();
    } while (ch != '\n');
    printf("\n");
	return 0;
}

 

1002 A+B for Polynomials (25)(25 分)

This time, you are supposed to find A+B where A and B are two polynomials.

Input

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.

Output

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output

3 2 1.5 1 2.9 0 3.2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000 + 10;
int n, a;
double p[maxn], b;
void init() {
    memset(p, 0, sizeof(p));
}
//题意就是定义一种加法规则,比如2.4^2 + 2.5^2 等于 4.9^4;//不是常规的计算方法
int main()
{
    init();
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d %lf", &a, &b);
        p[a] += b;
    }
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d %lf", &a, &b);
        p[a] += b;
    }
    int cnt = 0;
    for (int i = maxn-1; i >= 0; i--) {
        if (p[i] != 0) cnt++;
    }
    printf("%d", cnt);
    for (int i = maxn-1; i >= 0; i--) {
        if (p[i] != 0) {
            printf(" %d %.1f", i, p[i]);
        }
    }
    printf("\n");
    return 0;
}

 

1009 Product of Polynomials (25)(25 分)

This time, you are supposed to find A*B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < ... < N2 < N1 <=1000.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output

3 3 3.6 2 6.0 1 1.6
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
struct poly {
    int x;
    double y;
}from[maxn], p[maxn*2+10];//注意2*maxn
int n, m, cnt;
void init() {
    cnt = 0;
}
int main(){
    init();
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d %lf", &from[i].x, &from[i].y);
    }
    scanf("%d", &m);

    for (int i = 1; i <= m; i++) {
        int x; double y;
        scanf("%d %lf", &x, &y);
        for (int j = 1; j <= n; j++) {
            double z = y*from[j].y;
            int c = x + from[j].x;
            p[c].x = c;
            p[c].y += z;
        }
    }
    for (int i = maxn*2-1; i >= 0; i--) {//注意2*maxn
        if (p[i].y != 0) {
            cnt++;
        }
    }
    printf("%d", cnt);
    for (int i = maxn*2-1; i >= 0; i--) {//注意2*maxn
        if (p[i].y != 0) {
            printf(" %d %.1f", p[i].x, p[i].y);
        }
    }
    return 0;
}

 

1028 人口普查(20)(20 分)

某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。

这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月6日,所以超过200岁的生日和未出生的生日都是不合理的,应该被过滤掉。

输入格式:

输入在第一行给出正整数N,取值在(0, 10^5^];随后N行,每行给出1个人的姓名(由不超过5个英文字母组成的字符串)、以及按“yyyy/mm/dd”(即年/月/日)格式给出的生日。题目保证最年长和最年轻的人没有并列。

输出格式:

在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。

输入样例:

5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20

输出样例:

3 Tom John

 

#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&-(x))
const int maxn = 100000 + 10;
typedef long long ll;
int n;
struct people {
    char name[100];
    int yy, mm, dd;
}A[maxn];
int cmp(people &a, people &b) {
    if (a.yy != b.yy) return a.yy < b.yy;
    else {
        if (a.mm != b.mm) return a.mm < b.mm;
        else return a.dd < b.dd;
    }
}
bool check(int yy, int mm, int dd) {
    //1814/09/06 - 2014/09/06
    if (yy >= 1814 && yy <= 2014) {
        if (yy == 1814 || yy == 2014) {
            if (yy == 1814) {
                if (mm < 9 || (mm == 9 && dd < 6)) return false;
                else return true;
            }
            else {
                if (mm > 9 || (mm == 9 && dd > 6)) return false;
                else return true;
            }
        }
        else {
            return true;
        }
    }
    else return false;
}
char name[100];
int yy, mm, dd;
int main() {
    scanf("%d", &n);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
        scanf("%s %d/%d/%d", name, &yy, &mm, &dd);
        if (check(yy, mm, dd)) {
            strcpy(A[cnt].name, name);
            A[cnt].yy = yy;
            A[cnt].mm = mm;
            A[cnt].dd = dd;
            cnt++;
        }
    }
    sort(A, A+cnt, cmp);
    if (cnt != 0) printf("%d %s %s\n", cnt, A[0].name, A[cnt-1].name);
    else printf("0\n");
    return 0;
}

1032 挖掘机技术哪家强(20)(20 分)

为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。

输入格式:

输入在第1行给出不超过10^5^的正整数N,即参赛人数。随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从1开始连续编号)、及其比赛成绩(百分制),中间以空格分隔。

输出格式:

在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。

输入样例:

6
3 65
2 80
1 100
2 70
3 40
3 0

输出样例:

2 150

 

#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&-(x))
const int maxn = 100000 + 10;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int score[maxn];
void init() {
	memset(score, 0, sizeof(score));
}
int n;
int main() {
	init();
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		int id, sc;
		cin >> id >> sc;
		score[id] += sc;
	}
	int maxid = -1, maxsc = -1*INF;
	for (int i = 1; i < maxn; i++) {
		if (maxsc < score[i]) {
			maxid = i;
			maxsc = score[i];
		}
	}
	printf("%d %d\n", maxid, maxsc);
	return 0;
}

1011 World Cup Betting (20)(20 分)

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their money where their mouths were, by laying all manner of World Cup bets.

Chinese Football Lottery provided a "Triple Winning" game. The rule of winning was simple: first select any three of the games. Then for each selected game, bet on one of the three possible results -- namely W for win, T for tie, and L for lose. There was an odd assigned to each result. The winner's odd would be the product of the three odds times 65%.

For example, 3 games' odds are given as the following:

 W    T    L
1.1  2.5  1.7
1.2  3.0  1.6
4.1  1.2  1.1

To obtain the maximum profit, one must buy W for the 3rd game, T for the 2nd game, and T for the 1st game. If each bet takes 2 yuans, then the maximum profit would be (4.1*3.0*2.5*65%-1)*2 = 37.98 yuans (accurate up to 2 decimal places).

Input

Each input file contains one test case. Each case contains the betting information of 3 games. Each game occupies a line with three distinct odds corresponding to W, T and L.

Output

For each test case, print in one line the best bet of each game, and the maximum profit accurate up to 2 decimal places. The characters and the number must be separated by one space.

Sample Input

1.1 2.5 1.7
1.2 3.0 1.6
4.1 1.2 1.1

Sample Output

T T W 37.98

 

#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&-(x))
const int maxn = 100000 + 10;
const int INF = 0x3f3f3f3f;
typedef long long ll;
char mp[3] = {'W', 'T', 'L'};
int n;
int main() {
	double ans = 1.0;

	for (int i = 0; i < 3; i++) {
		int id = -1;
		double a, tmp = 0.0;
		for (int j = 0; j < 3; j++) {
			cin >> a;
			if (a > tmp) {
				tmp = a;
				id = j;
			}
		}
		printf("%c ", mp[id]);
		ans *= tmp;
	}
	printf("%.2f\n", (ans*0.65*1.0-1)*2);
	return 0;
}

1022 D进制的A+B (20)(20 分)

输入两个非负10进制整数A和B(<=2^30^-1),输出A+B的D (1 < D <= 10)进制数。

输入格式:

输入在一行中依次给出3个整数A、B和D。

输出格式:

输出A+B的D进制数。

输入样例:

123 456 8

输出样例:

1103
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&-(x))
const int maxn = 1000 + 10;
int ans[maxn];
int A, B, D;
int main() {
	while (cin >> A >> B >> D) {
		int C = A + B;
		int cnt = 0;
		do {
			int k = C%D;
			ans[cnt++] = k;
			C /= D;
		} while (C != 0);//do-while 判断C等于0的情况
		for (int i = cnt-1; i >= 0; i--) {
			printf("%d", ans[i]);
		}
	}

	return 0;
}

1061 Dating (20)(20 分)

Sherlock Holmes received a note with some strange strings: "Let's date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm". It took him only a minute to figure out that those strange strings are actually referring to the coded time "Thursday 14:04" -- since the first common capital English letter (case sensitive) shared by the first two strings is the 4th capital letter 'D', representing the 4th day in a week; the second common character is the 5th capital letter 'E', representing the 14th hour (hence the hours from 0 to 23 in a day are represented by the numbers from 0 to 9 and the capital letters from A to N, respectively); and the English letter shared by the last two strings is 's' at the 4th position, representing the 4th minute. Now given two pairs of strings, you are supposed to help Sherlock decode the dating time.

Input Specification:

Each input file contains one test case. Each case gives 4 non-empty strings of no more than 60 characters without white space in 4 lines.

Output Specification:

For each test case, print the decoded time in one line, in the format "DAY HH:MM", where "DAY" is a 3-character abbreviation for the days in a week -- that is, "MON" for Monday, "TUE" for Tuesday, "WED" for Wednesday, "THU" for Thursday, "FRI" for Friday, "SAT" for Saturday, and "SUN" for Sunday. It is guaranteed that the result is unique for each case.

Sample Input:

3485djDkxh4hhGE 
2984akDfkkkkggEdsb 
s&hgsfdk 
d&Hyscvnm

Sample Output:

THU 14:04
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&-(x))
const int maxn = 100 + 10;
typedef long long ll;
int n;
char mp[10][10] = {"", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
char s1[maxn], s2[maxn], s3[maxn], s4[maxn];
/**
第一个字符串和第二个字符串:
    第一对相同位置的,A-G的大写字母
    第二对相同位置的,0-9或者A-N的字符
第三个字符串和第四个字符串:
    第一对相同位置的,A-Z和a-z
**/
void work(char ch1, char ch2) {
    int k = ch1 - 'A' + 1;
    printf("%s ", mp[k]);
    if (ch2 >= '0' && ch2 <= '9') printf("%02d", (ch2-'0'));
    else printf("%02d", (ch2-'A')+10);
}
int main() {
    scanf("%s", s1);
    scanf("%s", s2);
    scanf("%s", s3);
    scanf("%s", s4);

    int len1 = strlen(s1);
    int len2 = strlen(s2);
    int len3 = strlen(s3);
    int len4 = strlen(s4);
    char ch1, ch2;
    int i = 0;
    for (i = 0; i < min(len1, len2); i++) {
            //第一个相同的是大写字母
        if (s1[i] == s2[i] && s1[i] >= 'A' && s1[i] <= 'G') {
            ch1 = s1[i];
            break;
        }
    }
    i++;
    for (; i  < min(len1, len2); i++) {
        if (s1[i] == s2[i] && ((s1[i] >= '0' && s1[i] <= '9')||(s1[i] >= 'A' && s1[i] <= 'N'))) {
            ch2 = s2[i];
            break;
        }
    }
    work(ch1, ch2);
    for (i = 0; i < min(len3, len4); i++) {
        if (s3[i] == s4[i] && s3[i] >= 'A' && s3[i] <= 'z') {
            printf(":%02d\n", i);
            break;
        }
    }
    return 0;
}

1082 Read Number in Chinese (25)(25 分)

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output "Fu" first if it is negative. For example, -123456789 is read as "Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu". Note: zero ("ling") must be handled correctly according to the Chinese tradition. For example, 100800 is "yi Shi Wan ling ba Bai".

Input Specification:

Each input file contains one test case, which gives an integer with no more than 9 digits.

Output Specification:

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

Sample Input 1:

-123456789

Sample Output 1:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

Sample Input 2:

100800

Sample Output 2:

yi Shi Wan ling ba Bai

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000 + 10;
char str[20];
char num[10][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
char wei[10][5] = {"Shi", "Bai", "Qian", "Wan", "Yi"};
//思路仔细看,4位4位处理,
//比如-123456789,那就6789当做一个四位,2345再当做一个四位,-1再当做一个四位,
//然后输入的数不会超过9个数字,也就是最多是亿级的。
int main() {
    scanf("%s", str);
    int len = strlen(str);
    int left = 0, right = len - 1;
    if (str[0] == '-') {
        printf("Fu");
        left++;
    }
    while (left + 4 <= right) {
        right -= 4;
    }
    while (left < len) {
        bool flag = false;// == false表示没有累积的0
        bool isPrint = false;// == false表示改节没有输出过其中的位
        while (left <= right) {
            if (left > 0 && str[left] == '0') {
                flag = true;
            }
            else {
                if (flag == true) {//如果存在累积的0
                    printf(" ling");
                    flag = false;
                }
                //只要不是首位
                if (left > 0) printf(" ");
                printf("%s", num[str[left] - '0']);
                isPrint = true;//改节至少有一位被输出
                if (left != right) {
                    printf(" %s", wei[right - left - 1]);
                }
            }
            left++;
        }
        if (isPrint == true && right != len - 1) {
            printf(" %s", wei[(len - 1 - right)/4 + 2]);//最多三个四位的,所以要加2
        }
        right += 4;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值