REVISE DAY 1 C Circulate (algorithm portal classic cycle race)

Today there is a big brother to me, like me, there is no solid knowledge base algorithm, can not even get some idea of ​​the extent of c language even have to, learn quickly, forget faster, not just to race to learn. So I decided to go over the new basic c language, and basic knowledge of algorithms and data structures, not only in preparation for the game, but also for future learning

Circulate(while,do while,for)


.1 daffodil daffodils number

All output daffodils number 100 to 999. If the 3-digit ABC meet ABC = A3 + B3 + C3, claimed it as the number of daffodils. For example, 153 + 53 + 33 = 13, so 153 is the number of daffodils.

code:

#include<stdio.h>
int main()
{
	int i;
	int a,b,c;
	for(i=100;i<1000;i++)
	{
		a=i/100;
		b=i%100/10;
		c=i%100%10;
		if(a*a*a+b*b*b+c*c*c==i)
			printf("%d ",i);
	}
	return 0;
}

.2 hanxin Han soldiers

According to legend, Han intellect, and never directly counting the number of its own troops, as long as soldiers have to make three in a row, five in a row, seven in a row to transform formation, but every time he swept Only one team of Pai Mei to know the total number. Input data comprises a plurality of sets, each set of data contains three non-negative integers a, b, c, represents the number of each row of tail formation (a <3, b 5, c <<7), the minimum value of the total number of output (or report no solution). The total number of not less than 10 are known, no more than 100. Input file to the end.
Sample input:
2. 6. 1
2. 1. 3
Sample Output:
Case. 1: 41 is
Case 2: No answer

code:

#include<stdio.h>
int main()
{
	int a,b,c;
	scanf("%d%d%d", &a, &b, &c)
		for (int i = 10; i < 100; i++)
		{
			if (i % 3 == a && i % 5 == b && i % 7 == c)
			{
				printf("%d", i);
				return 0;
			}
		}
		if(i==101)
		printf("No answer");
	return 0;
}

.3 triangle inverted triangle

Enter a positive integer n≤20, inverted triangle output a n-layer. For example, n = 5 the following output:
Here Insert Picture Description

code:

#include<stdio.h>
int main()
{
	int n;
	scanf("%d",&n);
	int i, j;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < i; j++)
			printf(" ");
		for (j = 0; j < 2 * (n - i) - 1; j++)
			printf("#");
		printf("\n");
	}
	return 0;
}

.4 subsequence and a subsequence

Enter two positive integers n <m <106, outputs Here Insert Picture Description , to five decimal places. Comprising a plurality of sets of input data end tag for n = m = 0. NOTE: This problem has pitfalls.
Sample input:
2. 4
65536 655360
0 0
Sample Output:
Case. 1: 0.42361
Case 2: 0.00001

code:

#include<stdio.h>
int main()
{
	int n, m,cnt=0;
	while (scanf("%d%d", &n, &m))
	{
		if (n == 0 && m == 0)
			break;
		else
		{
			cnt++;
			double i;
			double sum = 0;
			for (i = n; i <= m; i++)
				sum += 1 / i / i;//注意,n过大的话相乘会溢出
			printf("case%d:%.5f", cnt,sum);
		}
	}
	return 0;
}

.5 decimal fractions of decimals

Input A positive integer, b, c, the output of a / b, in decimal form, c 'after the decimal point. a, b≤106, c≤100. Comprising a plurality of sets of input data end tag for a = b = c = 0.
Sample input:
. 1. 6. 4
0 0 0
Sample Output:
Case. 1: 0.1667

This question is more interesting, and had the mentality of holding the c-based review exercises to do, and found the one simulation questions. Note that this question c range is equal to one hundred less than the maximum range of double is 16, so we have to come up when the primary method of division to do, and then take more than offset by 10. (Remember to count one, to rounding)

code:

#include<stdio.h>
int main()
{
	int a, b, c,cnt=0,sd[1000],d,h;
	while (scanf("%d%d%d", &a, &b, &c))
	{
		if (a == 0 && b == 0 && c == 0)
			break;
		else
		{
			cnt++;
			int n = a / b;
			printf("case %d:%d.",cnt,n);
			for (int i = 0; i <= c; i++)//多算一位
			{
				a *= 10;
				sd[i] = a / b;
				a %= b;
			}
			if (sd[c] >= 5)
				sd[c - 1] += 1;
			for (int i = 0; i < c; i++)
				printf("%d",sd[i]);
		}
	}
	return 0;
}

.6 permutation (arrangement)

With 1,2,3, ..., 9 consisting of 3 three-digit abc, def and GHI, each number is used just once, it requires abc: def: ghi = 1: 2: 3. Output "abc def ghi" according to the format of all solutions, a solution of each row. Tip: do not have too much brains.

Since neither the provisions of space and time complexity of the subject, perhaps it is not mindless, so I had to respectful than from life, with nine large circulation + N even the judge to get it! ! !

Death Code:

#include <stdio.h>
int main()
{
	int a, b, c, d, e, f, g, h, i;
	for (a = 1; a <= 9; a++)
	{
		for (b = 1; b <= 9; b++)
		{
			for (c = 1; c <= 9; c++)
			{
				for (d = 1; d <= 9; d++)
				{
					for (e = 1; e <= 9; e++)
					{
						for (f = 1; f <= 9; f++)
						{
							for (g = 1; g <= 9; g++)
							{
								for (h = 1; h <= 9; h++)
								{
									for (i = 1; i <= 9; i++)
									{
										if ((2 * (a * 100 + b * 10 + c) == 1 * (d * 100 + e * 10 + f)) && (3 * (a * 100 + b * 10 + c) == 1 * (g * 100 + h * 10 + i)) && (a != b) && (a != c) && (a != d) && (a != e) && (a != f) && (a != g) && (a != h) && (a != i) && (b != c) && (b != d) && (b != e) && (b != f) && (b != g) && (b != h) && (b != i) && (c != d) && (c != e) && (c != f) && (c != g) && (c != h) && (c != i) && (d != e) && (d != f) && (d != g) && (d != h) && (d != i) && (e != f) && (e != g) && (e != h) && (e != i) && (f != g) && (f != h) && (f != i) && (g != h) && (g != i) && (h != i))
										{
											printf("%d %d %d\n", a * 100 + b * 10 + c, d * 100 + e * 10 + f, g * 100 + h * 10 + i);
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return 0;
}

One day, step by step.

Published 22 original articles · won praise 8 · views 1474

Guess you like

Origin blog.csdn.net/OWCYKH/article/details/104356319