ACM:Virgo's Trial

An elephant decided to visit his friend. It turned out that the elephant’s house is located at point 0 and his friend’s house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend’s house.

Input
The first line of the input contains an integer x (1 ≤ x ≤ 1 000 000) — The coordinate of the friend’s house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.
Examples

Input
5
Output
1
Input
12
Output
3
Note
In the first sample the elephant needs to make one step of length 5 to reach the point x.
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.

AC:

#include<stdio.h>
 int main()
 {
     int total = 0,temp,step=0,i = 0;
	 int s[5]={5,4,3,2,1};
   
	 scanf("%d",&total);
     
	for(i=0;i<5;++i)
	{
		if(total>=s[i])
		{
			temp=total/s[i];
			step+=temp;
			total-=temp*s[i];
		}
	}
	
	printf("%d",step);
	return 0;
 }

Shapur was an extremely gifted student. He was great at everything including Combinatorics, Algebra, Number Theory, Geometry, Calculus, etc. He was not only smart but extraordinarily fast! He could manage to sum 1018 numbers in a single second.

One day in 230 AD Shapur was trying to find out if any one can possibly do calculations faster than him. As a result he made a very great contest and asked every one to come and take part.

In his contest he gave the contestants many different pairs of numbers. Each number is made from digits 0 or 1. The contestants should write a new number corresponding to the given pair of numbers. The rule is simple: The i-th digit of the answer is 1 if and only if the i-th digit of the two given numbers differ. In the other case the i-th digit of the answer is 0.

Shapur made many numbers and first tried his own speed. He saw that he can perform these operations on numbers of length ∞ (length of a number is number of digits in it) in a glance! He always gives correct answers so he expects the contestants to give correct answers, too. He is a good fellow so he won’t give anyone very big numbers and he always gives one person numbers of same length.

Now you are going to take part in Shapur’s contest. See if you are faster and more accurate.

Input
There are two lines in each input. Each of them contains a single number. It is guaranteed that the numbers are made from 0 and 1 only and that their length is same. The numbers may start with 0. The length of each number doesn’t exceed 100.
Output
Write one line — the corresponding answer. Do not omit the leading 0s.
Examples
Input
1010100
0100101
Output
1110001
Input
000
111
Output
111
Input
1110
1010
Output
0100
Input
01110
01100
Output
00010

AC:

#include<stdio.h>
#include<string.h>
int main()
{
	char a[100010] ={0},b[10010] = {0};
	int lena = 0,lenb = 0,i = 0;
	scanf("%s",&a);
	scanf("%s",&b);
	lena = strlen(a);lenb = strlen(b);
	
	for (i = 0; i < lena; i++)
	{
		if (a[i] == b[i])
			a[i] = '0';
		else 
			a[i] = '1';
	}
	printf("%s",a);
	return 0;
}

Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn’t need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a “plus”) and negative (a “minus”). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other.

Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own.

Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed.

Input
The first line of the input contains an integer n (1 ≤ n ≤ 100000) — the number of magnets. Then n lines follow. The i-th line (1 ≤ i ≤ n) contains either characters “01”, if Mike put the i-th magnet in the “plus-minus” position, or characters “10”, if Mike put the magnet in the “minus-plus” position.
Output
On the single line of the output print the number of groups of magnets.
Examples
Input
6
10
10
10
01
10
10
Output
3
Input
4
01
01
10
10
Output
2

Note
The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets.
The second testcase has two groups, each consisting of two magnets.

AC:

#include <stdio.h>
long long a[1000010];

int main()
{
	int total = 0, ans = 0,i = 0;
	scanf("%d", &total);
	for(i = 1; i <= total; i++)
	{
		scanf("%d", &a[i]);
		ans += (a[i] == a[i - 1] ? 0 : 1);
	}
	printf("%d\n", ans);
	return 0;
}

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, …, xm> another sequence Z = <z1, z2, …, zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, …, ik> of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.

Input
abcfbc abfcab
programming contest
abcd mnp
Output
4
2
0

Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0

AC:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#include<algorithm>
char a[1500],b[1500];
int map[1500][1500];
int main() 
{
	while(scanf("%s%s",&a,&b)!=EOF)
	{
		memset(map,0,sizeof(map));
		int lena=strlen(a);
		int lenb=strlen(b);
		for(int i=1; i<=lena; i++)
		{
			for(int j=1; j<=lenb; j++)
			{
				if(a[i-1]==b[j-1])
					map[i][j]=map[i-1][j-1]+1;
				else
					map[i][j]=max(map[i-1][j],map[i][j-1]);
			}
		}
		printf("%d\n",map[lena][lenb]);
	}
	return 0;
}

A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, … shows the first 20 humble numbers.

Write a program to find and print the nth element in this sequence

Input
The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line saying “The nth humble number is number.”. Depending on the value of n, the correct suffix “st”, “nd”, “rd”, or “th” for the ordinal number nth has to be used like it is shown in the sample output.
Sample Input
1
2
3
4
11
12
13
21
22
23
100
1000
5842
0

Sample Output
The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.

AC:

#include<stdio.h>
#define min(a,b) ((a)<(b)?(a):(b))
int num[5850];
int main()
{
	int  m=1,n = 0,a2 = 1,a3 = 1,a5 = 1,a7 = 1;
	num[1]=1;
	while(m<=5842)
	{
		num[++m]=min(min(2*num[a2],3*num[a3]),min(5*num[a5],7*num[a7]));
		if(num[m]==2*num[a2])
			a2++;
		if(num[m]==3*num[a3])
			a3++;
		if(num[m]==5*num[a5])
			a5++;
		if(num[m]==7*num[a7])
			a7++;    
	}
	while(~scanf("%d",&n),n)
	{
		printf("The %d",n);
		m=n/10%10;
		if(n%10==1&&m!=1)
			printf("st ");
		else if(n%10==2&&m!=1)
			printf("nd ");
		else if(n%10==3&&m!=1)
			printf("rd ");
		else
			printf("th ");
		printf("humble number is %d.\n",num[n]);
	}
	return 0;
}

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.

Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either *', representing the absence of oil, or@’, representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

Sample Input
1 1
*
3 5
@@*
@
@@*
1 8
@@***@
5 5
****@
@@@
@**@
@@@
@
@@**@
0 0
Sample Output
0
1
2
2

AC:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>

using namespace std;
char mp[200][200];
int vis[200][200];
int n,m;
int xx[8]={-1,-1,0,1,1,1,0,-1};
int yy[8]={0,1,1,1,0,-1,-1,-1};
void dfs(int x,int y)
{
	for(int i=0;i<8;i++)
	{
		int dx=x+xx[i];
		int dy=y+yy[i];
		if(dx>=0&&dx<n&&dy>=0&&dy<m&&mp[dx][dy]=='@'&&!vis[dx][dy])
		{
			vis[dx][dy]=1;
			dfs(dx,dy);
		}
	}
}
int main()
{
	int i = 0,j = 0,ans = 0;

	while(~scanf("%d %d",&n,&m))
	{
		ans = 0;
		if(n==0&&m==0) 
			break;
		memset(vis,0,sizeof(vis));
		for(i=0;i<n;i++)
			scanf("%s",mp[i]);

		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
				if(mp[i][j]=='@'&&!vis[i][j])
				{
					dfs(i,j);
					ans ++;
				}
				printf("%d\n",ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值