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

算法之美[从菜鸟到高手演练]之一些个小算法

2013年08月10日 ⁄ 综合 ⁄ 共 5428字 ⁄ 字号 评论关闭
1、10000以内的完数
/*
* 问题描述:求10000以内的完数。
* 完数即:一个数等于它的不同因子的和
* 如6=1+2+3.8!=1+2+4
* xtfggef 2012/5/16
*/
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

/*
* 思路:先把10000以内的完数算出来,放在
* 一个vector里,再和给定的数比较,如果小于
* 给定的数,就打印出来。还有一个思路:就是
* 直接计算小于给定的数的完数,这样可以
* 少计算点儿。
*/
int main(int argc, char * argv[])
{
	//此处引入文件操作
	ifstream cin("seed.txt");
	int n;
	vector<int> a;
	/*
	* 此处需要说明一下,为什么下面的for
	* 循环为i=i+2,这说明完数只有在偶数里
	* 有,奇数在一个小的范围内没有完数
	* 具体在哪个范围,数学界还没有证明出来
	*/
	for(int i=2; i<10000; i=i+2)
	{
		int sum = 1;
		for(int j=2; j<=i/2; j++)
		{
			//如果模除为0,则是它的因子,所以加上去
			if(i%j==0) sum=sum+j;
		}
		if(sum==i) a.push_back(i);
	}
	while(cin>>n)
	{
		cout << n << ":";
		for(int k=0; k<a.size(); k++)
		{
			if(a[k]<=n)
				cout << " " << a[k];
		}
		 cout << endl;
	}
	return 0;
}

百度百科完数:http://baike.baidu.com/view/640632.htm

2、求三位数对称素数

/*
* 问题描述:找出3位数对称素数
* 如101就是,787也是,896不是
* 是的话输入YES,否则输出NO
* xtfggef 2012/5/16
*/
#include<iostream>
#include<cmath>
using namespace std;

bool isPrime(int);
int main(int argc, char * argv[])
{
	int n;
	cin >> n;
	//核心
	cout << (n>100&&n<1000&&n/100==n%10&&isPrime(n)?"YES\n":"NO\n");
	return 0;
}
/*
* 判断是否是素数
*/
bool isPrime(int n)
{
	int sqr = sqrt(n*1.0);
	for(int i=2; i<=sqr; i++)
	{
		if(n%i==0)
			return false;
	}
	return true;
}

3、求两个数的最大公约数

/*
* 求两个数的最大公约数
* xtfggef 2012/5/16
*/
#include<stdio.h>

int gcd(int,int);
void main()
{
	int a = 5;
	int b = 8;
	int c = gcd(a,b);
	printf("%d\n",c);
}
int gcd(int a, int b)
{
	while(a!=b)
	{
		if(a>b)
			a=a-b;
		else
			b=b-a;
	}
	return a;
}

最小公倍数类似,借助于最小公倍数=X*Y/gcd(X,Y);就OK了。

此处注意:为了使计算不超出范围,最好写成:X/gcd(X,Y)*Y

4、取代求模运算法

 k = (j + rotdist)%n
可以写为:
k = j + rotdist;
if(k>n)
k -= n;

5、求相邻子向量的最大和(出自编程珠玑)

/*
* 找出任何相邻子向量的最大和
* xtfggef 2012/5/7
*/
#include<stdio.h>

int max(int a,int b)
{
	return (a>b)?a:b;
}
void main()
{

	int a[10] = {31,-41,59,26,-53,58,97,-93,-23,84};
	int maxsofar = 0;
	int maxendinghere = 0;
	for(int i=0; i<10; i++)
	{
		//遇到负数,直接赋0,相当于跳过
		//遇到大一点的数,从该数开始
		maxendinghere=max(maxendinghere+a[i],0);
		maxsofar=max(maxsofar,maxendinghere);
	}
	printf("%d\n",maxsofar);
}

6、 给定一个整型数组a,元素个数为n,求在a中出现n/2次以上的元素,并打印出来。

package com.xtfggef.hashmap;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 打印在数组中出现n/2以上的元素
 * 利用一个HashMap来存放数组元素及出现的次数
 * @author erqing
 *
 */
public class HashMapTest {
	
	public static void main(String[] args) {
		
		int [] a = {2,3,2,2,1,4,2,2,2,7,9,6,2,2,3,1,0};
		
		Map<Integer, Integer> map = new HashMap<Integer,Integer>();
		for(int i=0; i<a.length; i++){
			if(map.containsKey(a[i])){
				int tmp = map.get(a[i]);
				tmp+=1;
				map.put(a[i], tmp);
			}else{
				map.put(a[i], 1);
			}
		}
		Set<Integer> set = map.keySet();
		for (Integer s : set) {
			if(map.get(s)>=a.length/2){
				System.out.println(s);
			}
		}
	}
}

7、HDOJ Problem 1000 Java实现

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()){    
            int a = in.nextInt();
            int b = in.nextInt();
            int c = sum(a,b);
            System.out.println(c);
        }
    }
    public static int sum(int a,int b){
        return a+b;
    }
}

8、HDOJ Problem 1001 C++实现

#include <iostream>
using namespace std;
int main()
{
    unsigned int num; 
    while (cin >> num)
    {
        cout << (1+num)*num/2 << endl << endl ; 
    }

    return 0;
}

9、HDOJ Problem 1002 Java实现

import java.math.BigInteger;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        Scanner scanf = new Scanner(System.in);
        while(scanf.hasNext())
        {
            int n;
            int i=1,x=0;
            n=scanf.nextInt();
            while(n--!=0)
           {
               if(x++!=0)
                   System.out.println();
               BigInteger a,b,sum;
               a=scanf.nextBigInteger();
               b=scanf.nextBigInteger();
               sum=a.add(b);
               System.out.println("Case"+" "+ i++ +":");
               System.out.print(a+" "+"+"+" "+b+" "+"="+" ");
               System.out.println(sum);
           }
        }
    }
}

10、Java实现矩阵的转置

package com.xtfggef.algo;

/**
 * 矩阵的转置
 * 
 * @author erqing
 * 
 */
public class Matrix {

	public static void main(String[] args) {
		int array[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
		int array2[][] = new int[3][3];
		System.out.println("转置前:");
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array[i].length; j++) {
				System.out.print(array[i][j] + " ");
				array2[j][i] = array[i][j];
			}
			System.out.println();
		}
		System.out.println("转置后:");
		for (int k = 0; k < array2.length; k++) {
			for (int h = 0; h < array2[k].length; h++) {
				System.out.print(array2[k][h] + " ");
			}
			System.out.println();
		}
	}

}

11、Java实现计算时间差

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Tiemp {
    public static void main(String args[]){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String s1 = "20080808";
        String s2 = "20080908";
        try {
            Date d1 = sdf.parse(s1);
            Date d2 = sdf.parse(s2);
            System.out.println((d2.getTime() -  d1.getTime())/(3600L*1000)); 
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

12、判断ip合法性

1、从.的数量来看,不能大于3个

2、每两个点儿之间的数必须在0~255之间

3、每个除.之外的数必须<9且>0

4、第一个、最后一个字符不能是.

#include <stdio.h>

int is_valid_ip(const char *ip)

{
	
    int section = 0; //每一节的十进制值
    int dot = 0; //几个点分隔符
    int last = -1; //每一节中上一个字符

	if(*ip!='.')
	{
		while(*ip)		
		{		
			if(*ip == '.')				
			{				
				dot++;	
				if(dot > 3)					
				{					
					return 0;					
				}				
				if(section >= 0 && section <=255)					
				{					
					section = 0;
				}else{					
					return 0;				
				}
				
			}else if(*ip >= '0' && *ip <= '9')
			{				
				section = section * 10 + *ip - '0';			
				if(last == '0')					
				{					
					return 0;				
				}				
			}else{			
				return 0;				
			}			
			last = *ip;
			ip++;			
		}		
		if(section >= 0 && section <=255)		
		{			
			if(3 == dot)				
			{				
				section = 0;				
				printf("IP address success!\n");
				return 0;				
			}			
		}		
		return 1;
	}
	else
	{
		printf("格式错误!");
		return 0;
	}
}
int main()
{	
    is_valid_ip("23.249.22.123");
	return 0;	
}

13、判断字符串是否是回文

#include <stdio.h>
#include <stdlib.h>

/*判断用户输入的字符串是否为回文
 *回文是指顺读和反读都一样的串
 *例:abccba为回文,abcdab不是回文
*/

int Palindrome(const char *str)
{
	int length = strlen(str);
	for(int i = 0; i <= length/2; i++)
	{
		if(str[i] != str[length-i-1])
		{
			return -1;
		}
	}
	return 1;
}

int main()
{
	char s[100];
	gets(s);
	int result = Palindrome(s);
	if(result == 1)
	{
		printf("字符串是回文");
	}
	else
	{
		printf("字符串不是回文");
	}
}


 

14、字符串转换为整数

#include<iostream>
#include<string>
#include<assert.h>

using namespace std;

int str_2_int(string str)
{
	assert(str.size()>0);
	int pos = 0;
	int sym = 1;

	if(str[pos] == '+')
		pos++;
	else if(str[pos] == '-')
	{
		pos++;
		sym=-1;
	}
	int num =0;
	while(pos<str.length())
	{
		assert(str[pos]>='0');
		assert(str[pos]<='9');
		num = num*10+(str[pos]-'0');
		assert(num>=0);
		pos++;
	}
	num*=sym;
	return num;
}
int main()
{
	string str = "-1024";
	int num = str_2_int(str);
	cout << num << endl;
	return 0;
}

 

 

 

抱歉!评论已关闭.