Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24649    Accepted Submission(s): 9281

Problem Description
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105.
 
Input
Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 ... nm where m is the number of integers in the set and n1 ... nm are the integers. All integers will be positive and lie within the range of a 32-bit integer.
 
Output
For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.
 
Sample Input
2
3 5 7 15
6 4 10296 936 1287 792 1
 
Sample Output
105
10296
 
Source
 
题意:求几个数的最小公倍数
思路:前两个数求一次,求得的结果再与下一个数求
 
 //hdu_1019_Least Common Multiple_201310290920-2
#include <stdio.h>
#include <malloc.h> void swap(int* a,int* b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
int gcd(int m,int n)
{
int i;
if(m>n)
swap(&m,&n);
i=m;
while(i)
{
i=n%m;
n=m;
m=i;
}
return n;
}
int main()
{
int N;
scanf("%d",&N);
while(N--)
{
int i,j,n,t;
int *shuzu;
scanf("%d",&n);
shuzu = (int*)malloc(sizeof(int)*n);
for(i=;i<n;i++)
scanf("%d",&shuzu[i]);
for(i=;i<n-;i++)
{
t=gcd(shuzu[i],shuzu[i+]);
shuzu[i+]=shuzu[i]/t*shuzu[i+];
//shuzu[i+1]=shuzu[i]*shuzu[i+1]/t;这样容易造成数据溢出
}
printf("%d\n",shuzu[n-]);
free(shuzu);
}
//printf("%d\n",gcd(5,15));
//while(1);
return ;
}
//ac
 #include <stdio.h>
#include <malloc.h> int main()
{
int N;
scanf("%d",&N);
while(N--)
{
int i,j,n;
int *shuzu;
scanf("%d",&n);
shuzu = (int*)malloc(sizeof(int)*n);
for(i=;i<n;i++)
scanf("%d",&shuzu[i]);
for(i=;i<n-;i++)
for(j=shuzu[i];j<=shuzu[i]*shuzu[i+];j++)
if(j%shuzu[i]==&&j%shuzu[i+]==)
{
shuzu[i+]=j;
break;
}
printf("%d\n",shuzu[n-]);
free(shuzu);
}
return ;
}
//TML
 

hdu_1019_Least Common Multiple_201310290920的更多相关文章

  1. Socket聊天程序——Common

    写在前面: 上一篇记录了Socket聊天程序的客户端设计,为了记录的完整性,这里还是将Socket聊天的最后一个模块--Common模块记录一下.Common的设计如下: 功能说明: Common模块 ...

  2. angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)

    common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...

  3. Common Bugs in C Programming

    There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...

  4. ANSI Common Lisp Practice - My Answers - Chatper - 3

    Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...

  5. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  6. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  7. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  8. 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]

    [题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下:  C++ Code  123456   struct BinaryTreeNode {     int ...

  9. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

随机推荐

  1. js中的slice()、substring()、substr()、split()、join()、indexof()

    在js中字符截取函数有常用的三个slice().substring().substr()了,下面我来给大家介绍slice().substring().substr()函数在字符截取时的一些用法与区别吧 ...

  2. 【Leetcode 3】Longest Substring Without Repeating Characters0

    Description: Given a string, find the length of the longest substring without repeating characters. ...

  3. [转]STL之list容器详解

    List 容器 list是C++标准模版库(STL,Standard Template Library)中的部分内容.实际上,list容器就是一个双向链表,可以高效地进行插入删除元素. 使用list容 ...

  4. Canvas清空

    当canvs与bitmap绑定时,canvas上绘制会导致bitmap改变内容,而且内容时叠加的.这时候需要清空bitmap上的内容,可以用以下做法. Paint paint = new Paint( ...

  5. Ajax——php基础知识(二)

    header header('content-type:text/html; charset= utf-8');//设置编码格式为:utf-8 header('location:http://www. ...

  6. JS——鼠标跟随

    注意事项: 1.pageX.pageY的兼容问题 2.使目标移动鼠标中间位置还必须减去盒子宽度的一半 <!DOCTYPE html> <html lang="en" ...

  7. Python标准库sys

    1.命令行参数sys.argv 我们从Python语言之模块第一部分的例子开始,看看sys.argv中到底存了些什么内容. #Filename: using_sys.py import sys i=0 ...

  8. python 字典 get方法

    在做项目的过程中,遇到了一个问题,数据保存到字典中,后来发现数据不对,排查了字典的构建过程,是OK的,后来怀疑是别的部分共用了这一个字典,排查代码,发现这里应该是有问题的. score = None ...

  9. HDU_1502_dp

    Regular Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  10. Redis 之服务器集群配置

    常见的集群架构如图: redis操作过程中数据同步的函数调用关系: 集群搭建: 1.修改3个redis.config 文件的: 2.启动2个redis服务器 当杀掉redis主进程Master时,由于 ...