[] Data structure and algorithm study notes - "algorithm notes" -6

Killed attractiveness of the (3n + 1) guess

#include "stdafx.h"
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	int n;
	int i = 0;
	scanf("%d", &n);
	while (n - 1)
	{
		if (fmod(n, 2))
		{
			n = 3*n + 1;
		}
		n = n / 2;
		i++;
	}
	printf("%d", i);
	return 0;
}

While in this condition may be changed while (n-=. 1!)
FMOD function can be changed to%: if (n% 2 == 0)

Excavator strong Which

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	int n,m=0,maxnum=0,maxscore=0,score;
	int a[1000] = { 0 };
	int b[1000] = { 0 };
	scanf("%d", &n);
	for(int i=0;i<n;i++)
	{
		scanf("%d %d", &a[i], &score);
		b[a[i]] += score;
		if (a[i] > m)	m = a[i];
	}
	for (int i = 0; i < m; i++)
	{
		if (b[i] > maxscore)
		{
			maxscore = b[i];
			maxnum = i;
		}
	}
	printf("%d %d", maxnum, maxscore);
	return 0;
}

Here the subject is required is N <100000, but this will cause data overflow, the normal operation of the program into 1000, obviously does not meet the meaning of the questions, put a [100000] initialization removed, leaving only initializing the array b.

**练习**

The rest of the tree

Description Title
has a length of integer L (1 <= L <= 10000) of the road, one can imagine the number of longitudinal axis L of a line segment, the starting point is the coordinate origin, a tree at every integer coordinate points, i.e. 0 , 1,2, ..., L total L + L + 1 has a position on the tree.
Now to remove some trees, tree removal section is represented by a pair of numbers, such as 100 200 represents all removed from the tree (inclusive) 100-200.
There may be M (1 <= M <= 100) intervals, there may be overlap between the intervals. The number of trees remaining after removal of the requirements of all sections of the tree now.

Input two integers L (1 <= L <= 10000) and M (1 <= M <= 100).
Then there is an integer M groups, each with a pair of numbers.

Output may be multiple sets of input data, each input data, outputs a number representing the number of trees remaining after removal of all sections of the tree.

#include <cstdio>
#include <cstring>
#include<cmath>

int main()
{
	int L, M,q,p;
	int a[10001] = { 0 };
	while (scanf("%d %d", &L, &M), L || M)
	{
		int sum = 0;
		//memset(a, 0, sizeof(a));
		for (int i = 0; i <= L; i++)
		{
			a[i] = 1;
		}
		while (M--)
		{
			scanf("%d %d", &q, &p);
			for (int j = q; j <= p; j++)
			{
				a[j] = 0;
			}
		}
		for (int i = 0; i <= L; i++)
		{
			sum += a[i];
		}
		printf("%d\n", sum);
	}
	return 0;
}

A+B

Title Description
Given two integers A and B, which is a representation: starting from the bits, three digits each with a comma "," separated. Please calculation of A + B, and outputs a normal form.
Input data comprises a plurality of sets, each set of data per line,
consisting of two integers A and B (-10 . 9 <A, B <10 . 9 ).
Calculate the output result of A + B, and outputs its normal form, each set of data per line.

Ah ah ah ah do not! ! !

Special multiplication

Title Description
write algorithm, the input to one billion two small, find results.
Special multiplication Example: 123 * 45 = 1 4 + 1 5 + 2 4 + 2 5 + 3 4 + 3 5
inputs
two numbers less than ten billion
output
input may have multiple sets of data, for each set of data, the output of Input two numbers result obtained after calculation method according to requirements of the subject.

// ConsoleApplication1.cpp: 定义控制台-应用程序的入口点。
//
#include "stdafx.h"
#include <cstdio>
#include <cstring>
#include <cmath>

int main()
{
	int a[10] = { 0 }, b[10] = { 0 };
	long long int num1, num2;
	long temp;
	while (scanf("%lld %lld", &num1, &num2) != EOF)
	{
		for (int i = 0; i<10; i++)
		{
			temp = fmod(num1, pow(10, i + 1));
			a[i] = (temp - fmod(temp, pow(10, i))) / (pow(10, i));
			temp = fmod(num2, pow(10, i + 1));
			b[i] = (temp - fmod(temp, pow(10, i))) / (pow(10, i));
		}
		int result = 0;
		for (int i = 0; i<10; i++)
		{
			for (int j = 0; j<10; j++)
			{
				result += a[i] * b[j];
			}
		}
		printf("%d\n", result);
	}
	return 0;
}

In solving the problem of the time, encountered some problems, such as int range / long int it does not include the maximum number 999999999, so set the data type of the time should be set long long int.

Question: This question is there any other solution? For example, a string?

#include <cstdio>
#include <cstring>
#include <cmath>

int main()
{
	char a[10] = { 0 }, b[10] = { 0 };
	int result = 0;
	int m, n;
	while (scanf("%s %s", a, b) != EOF)
	{
		for (int i = 0; i<strlen(a); i++)
		{
			for (int j = 0; j<strlen(b); j++)
			{
				m = a[i] - '0';
				n = b[j] - '0';
				result +=( a[i]-'0') *( b[j]-'0');
			}
		}
		printf("%d\n", result);
	}
}

The method of drawing others to write the above code, where the point to note is

		for (int i = 0; i<strlen(a); i++)

This line of code, written directly if i <10; guess cause read '\ 0', so that the final result of the error.

Compare the number of odd-even

Description Title
Enter a number of the first line, is n, the number n of the second input line, in which the number n, the even more than if odd, outputting to NO, output YES.
Enter
input multiple sets of data.
Each input n, and the input n integers (1 <= n <= 1000 ).
Output
if more than the even odd output to NO, output YES.

#include <cstdio>
#include <cstring>
#include <cmath>

int main()
{
	int n,num;
	while (scanf("%d", &n) != EOF)
	{
		int n1 = 0, n2 = 0;
		while (n--)
		{
			scanf("%d", &num);
			if (fmod(num, 2))	n1++;
			else n2++;
		}
		if (n2 > n1)	printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}

Shortest Distance

题目描述
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.
输入
Each input file contains one test case. For each case, the first line contains an integer N (in [3, 105]), followed by N integer distances D1 D2 … DN, where Di is the distance between the i-th and the (i+1)-st exits, and DN 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 (<=104), 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 107.
输出
For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.
样例输入

#include <cstdio>
#include <cstring>
#include <cmath>
#define N 200000
#define M 10000
using namespace std;

int main()
{
	int n,num;
	int a[N] = { 0 }, p[M] = { 0 }, q[M] = { 0 };
	int lentha, lenthpq;
	while (scanf("%d", &n) != EOF)
	{
		int i = 0;
		while (n-i)
		{
			scanf("%d", &a[i]);
			i++;
		}
		int row;
		scanf("%d", &row);
		int j = 0;
		while (row-j)
		{
			scanf("%d %d", &p[j], &q[j]);
			if (p[j] > q[j])
			{
				int temp;
				temp = p[j];
				p[j] = q[j];
				q[j] = temp;
			}
			j++;
		}
		for (int k = 0; k < n; k++)
		{
			a[k + n] = a[k];
		}
		for (int k = 0; k < row; k++)
		{
			int dis1=0, dis2=0;
			for (int u = p[k]-1; u <=q[k]-2 ; u++)
			{
				dis1 += a[u];
			}
			for (int u = q[k] - 1; u <= p[k] + n - 2;u++)
			{
				dis2 += a[u];
			}
			if (dis1 < dis2)	printf("%d\n", dis1);
			else printf("%d\n", dis2);
		}
	}
	return 0;
}

A + B and C

Subject description
given interval [-231, 231] in the three integers A, B and C, A + B determines whether the request is greater than C.
Input
Input line 1 gives a positive integer T (<= 10), a number of test cases. T is then given set of test cases, each per line, the order is given A, B and C. Between integer separated by a space.
Output
for each test case, the output in line "Case #X: true" if A + B> C, and otherwise outputs "Case #X: false", where X is the test case number (starting from 1).

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	int n;
	long long a, b, c,result;
	scanf("%d", &n);
	for (int i = 0; i<n; i++)
	{
		scanf("%lld %lld %lld", &a, &b, &c);
		result = a + b - c;
		if (result > 0)	printf("Case #%d: true\n", i + 1);
		else
			printf("Case #%d: false\n", i + 1);
	}
	return 0;
}

Part A + B ** **

Description Title
Title described
positive integers A, "DA (as an integer) part" is defined as an integer of PA A new all DA thereof. For example: Given A = 3862767, DA = 6, then "section 6" PA 66 A is, because there are 2 A 6.
Now given A, DA, B, DB, write a program to calculate PA + PB.
Input
Input given sequentially in a row A, DA, B, DB, separated by a space intermediate, where 0 <A, B <1010.
Output
output values PA + PB in a row.

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int num(int a, int da)
{
	int numa = 0;
	for (int i = 1; i < 10; i++)
	{
		int k, t;
		k = pow(10, i);
		t = pow(10, i - 1);
		int m;
		m = (fmod(a, k) - fmod(a, t)) / t;
		if (m == da) 
		{
			numa++;
		}
	}
	return numa;
}

int compute(int numa,int da)
{
	int pa = 0;
	for (int i = 0; i < numa; i++)
	{
		pa += da * pow(10, i);
	}
	return pa;
}

int main()
{
	int a, b;
	int pa=0, pb=0,da,db;
	int numa = 0, numb = 0;
	while (scanf("%d %d %d %d", &a, &da, &b, &db) != EOF)
	{
		numa=num(a, da);
		numb=num(b, db);
		pa = compute(numa, da);
		pb = compute(numb, db);
		printf("%d\n", pa + pb);
	}
	return 0;
}

** ** hammer scissors cloth

  • Encounter problems not know what went wrong == clearly answer the same!

Title Description
everyone should be able to play "hammer and scissors" game: they both give gesture, the outcome of the rules as shown:
Now given the two men clash records, statistics of both wins, flat, negative number, and the two sides were given what gesture greatest chance of winning.
Input
Input line 1 gives a positive integer N (<= 105), i.e., both the number of confrontation. Then N rows, each row gives information confrontation, i.e., the gesture and B sides at the same time is given. C stands for "hammer", the representative J "scissors", B stands for "cloth", the first letter represents a party, on behalf of the second party, there is an intermediate space.
Output
Output 1 and 2 are given line A, B wins, flat, negative number, separated by an inter-digital space. Line 3 shows two letters represent A, B wins the highest number of gestures, there is an intermediate space. If the solution is not unique, the minimum solution alphabetically output.

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	int n;
	char jia, yi;
	int a[2][6] = { 0 };
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		getchar();
		scanf("%c %c", &jia, &yi);
		if (jia == 'C')
		{
			if (yi == 'J')
			{
				a[0][0]++;
				a[0][3]++;
				a[1][2]++;
			}
			if (yi == 'B')
			{
				a[0][2]++;
				a[1][0]++;
				a[1][4]++;
			}
			if (yi == 'C')
			{
				a[0][1]++;
				a[1][1]++;
			}
		}
		if (jia == 'J')
		{
			if (yi == 'B')
			{
				a[0][0]++;
				a[0][5]++;
				a[1][2]++;
			}
			if (yi == 'C')
			{
				a[0][2]++;
				a[1][0]++;
				a[1][3]++;
			}
			if (yi == 'J')
			{
				a[0][1]++;
				a[1][1]++;
			}
		}
		if (jia == 'B')
		{
			if (yi == 'C')
			{
				a[0][0]++;
				a[0][4]++;
				a[1][2]++;
			}
			if (yi == 'J')
			{
				a[0][2]++;
				a[1][0]++;
				a[1][5]++;
			}
			if (yi == 'B')
			{
				a[0][1]++;
				a[1][1]++;
			}
		}
		int cha = 1;
	}
	printf("%d %d %d\n%d %d %d", a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2]);
	int output[2][2] = { 0 };
	for (int man = 0; man < 2; man++)
	{
		for (int q = 3; q <=5 ; q++)
		{
			if (a[man][q] > output[man][1])
			{
				output[man][1] = a[man][q];
				output[man][0] = q;
			}
		}
	}
	printf("\n");
	for (int k = 0;  k< 2; k++)
	{
		switch (output[k][0])
		{
		case 3: {
			printf("C ");
			break;
		}
		case 4: {
			printf("B ");
			break;
		}
		case 5: {
			printf("J");
			break;
		}
		default:
			break;
		}
		if (k == 1)
			printf("\n");
	}
	return 0;
}
Published 43 original articles · won praise 4 · Views 1221

Guess you like

Origin blog.csdn.net/weixin_42176221/article/details/100733833