现在的位置: 首页 > 综合 > 正文

【ACM-Steps1.2】简单字符串处理、简单数学题、进制转换系列8题(一)

2013年01月31日 ⁄ 综合 ⁄ 共 2161字 ⁄ 字号 评论关闭

1、HDU 2104 hide handkerchief(数学),

小孩N个围成一圈,一次跳M个来数数,问能否遍历到所有小孩,辗转相除法求公约数。

#include <iostream>
using namespace std;

int gcd(int a,int b)
{
      if(b==0)
             return a;
      return gcd(b,(a%b));
}

int main()
{
	int N,M;
	while(cin>>N>>M)
	{
		if(N==-1 && M==-1)
			break;
		else
		{
			int p=gcd(N,M);
			if(p==1)
			{
				cout<<"YES"<<endl;
			}
			else
			{
				cout<<"POOR Haha"<<endl;
			}
		}
	}
	return 0;
}

2、HDU 2095——find your present (2)(简单数据结构)

找出只出现了一次的数。数组会超内存(只有1024K),用MAP比较好。

#include <iostream>
#include <stdio.h>
#include <map>
#include <cstring>
using namespace std;

int main()
{
	int testcase;
	while(cin>>testcase && testcase!=0)
	{
		map<int,int> numpack;
		
		for(int i=0;i<testcase;i++)
		{
				int temp;
				scanf("%d",&temp);
				numpack[temp]++;
		}
		
		map<int,int>::iterator it;
		for(it=numpack.begin();it!=numpack.end();it++)
		{
			if((*it).second==1)
			{
				cout<<(*it).first<<endl;
			}
		}
	
		
	}
	return 0;
}

3、HDU 2393——Higher Math(水)

判断是不是直角三角形

#include <iostream>
using namespace std;


int istri(int a,int b,int c)
{
	int big,other1,other2;
	if(a>b && a>c)
	{
		big=a;
		other1=b;
		other2=c;
	}
	else if(b>c && b>a)
	{
		big=b;
		other1=a;
		other2=c;
	}
	else if(c>a && c>b)
	{
		big=c;
		other1=a;
		other2=b;
	}
	else
		return false;
	if((other1*other1)+(other2*other2)==big*big)
		return true;
	else
		return false;
	
}

int main()
{
	int testcase;
	cin>>testcase;
	for(int i=1;i<=testcase;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		if(istri(a,b,c))
		{
			cout<<"Scenario #"<<i<<":"<<endl;
			cout<<"yes"<<endl;
			cout<<endl;
		}
		else
		{
			cout<<"Scenario #"<<i<<":"<<endl;
			cout<<"no"<<endl;
			cout<<endl;
		}
	}
	return 0;
}

4、HDU 2106——decimal system(进制转换)

N进制转10进制,注意字符串处理。

#include <iostream>
#include <stack>
#include <string>
#include <stdlib.h>
#include <cmath>
#include <math.h>
using namespace std;

int numpack[1005];

int my_pow(int a,int b)
{
	int resultwe=1;
	for(int i=1;i<=b;i++)
	{
		resultwe*=a;
	}
	return resultwe;
}


int decrov(string tarnum,string idxstr)
{
	int idx=atoi(idxstr.c_str());
	int tarnumer=atoi(tarnum.c_str());
	int result=0;
	
	if(tarnumer<idx || idx==10)
	{
		return tarnumer;
	}
	
	for(int i=0;i<tarnum.size();i++)
	{
		//cout<<(tarnum[i]-48)<<(tarnum.size()-1)-i<<endl;
		result+=((tarnum[i]-48)*my_pow(idx,(tarnum.size()-1)-i));
	}
	
	return result;
	
}



int main()
{
	int testcase;
	while(cin>>testcase)
	{
		int ot=0;
		for(int i=0;i<testcase;i++)
		{
			string a,b,longtar;
			int pos1,pos2;
			cin>>longtar;
			pos1=longtar.find('(',0);
			pos2=longtar.find(')',0);
			for(int i=0;i<pos1;i++)
				a+=longtar[i];
			for(int j=pos1+1;j<pos2;j++)
				b+=longtar[j];
			
			ot+=decrov(a,b);
				
		}
		
		cout<<ot<<endl;
	}
	
	return 0;
}

抱歉!评论已关闭.