关于OJ1028的参考解法

其中运用了最小公倍数与最大公约数乘积等于两数相乘的定理。

 1 #include <stdio.h>
 2 int main(int argc, char *argv[])
 3 {
 4     int a,b,c,d,e;
 5     scanf("%d %d",&a,&b);
 6     d=a*b;
 7     while(b>0)
 8     {
 9         c=a%b;
10         a=b;
11         b=c;
12     }
13     e=d/a;
14     printf("%d\n",e);
15     return 0;
16 }
时间: 2024-07-29 12:11:28

关于OJ1028的参考解法的相关文章

网易的一道笔试题的参考解法---关于广告牌投放问题

有N个广告牌(N<=10万)可以投放广告,有k个用户(k<10亿)在这些广告牌上投放广告.操作rent(i,j,k)将从i到j块广告牌展示用户k的广告,如果原来有别的广告就覆盖掉. 操作query(i)返回第i个广告牌上现在投放的是哪个广告. rent和query操作出现的频率相等.要求设计一个数据结构和相应的算法,尽可能快的实现这两种操作. 1 package test; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 pu

关于欧几里得算法求最大公约数,即OJ1029的参考解法

1 #include <stdio.h> 2 int main(int argc, char *argv[]) 3 { 4 int a,b,c; 5 scanf("%d %d",&a,&b); 6 while(b>0) 7 { 8 c=a%b; 9 a=b; 10 b=c; 11 } 12 printf("%d\n",a); 13 return 0; 14 }

leetcode Database3

一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ For example, given the above Employee table, the nth hig

逆转c字符串

题目: Write code to reverse a C-Style String (C-String means that “abcd” is represented as five characters, including the null character ) 参考解法:把数组的思维去除,用指针的哨兵来做会更简单一点. 第一遍解: 遍历字符串得出长度后,然后首尾互换. // // main.cpp // reverse // // Created by lexnewgate on 1

leetcode解题笔记-Remove Element

题目要求: 去除所有数组内与所给值相等的元素,并且返回新数组的长度. 个人解法: 1.定义一个int len=nums.length当有nums[i]==value时,len--,但是这个仅仅是返回新数组的长度,并没有做到剔除相等元素 参考解法: 1.核心在于,i的值从前往后,len的值从后往前 2.i在外循环递增,len在内循环递减 3.利用nums[--len]的值来代替与value相等的值 代码: for(int i=0 ;i< len; i++){ while(nums[i]==valu

Leetcode解题笔记-Contains Duplicate &amp;&amp; Contains Duplicate II&amp;&amp;Contain Duplicate III

Contain Duplicate 题目要求: 给定一个数组如果有重复就返回true,如果没有就返回false 个人分析: 这个题比剔除重复的要简单许多,只需要判断就好 代码如下: public static boolean containsDuplicate(int[] nums){ if (nums.length==0) return false; Arrays.sort(nums); for(int i =1; i < nums.length; i++){ if(nums[i-1]==nu

【leetcode 桶排序】Maximum Gap

1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements. You may assume all elements in the array are non-

Leetcode解题笔记-maxArea

题目要求: 给定一个数组a1,a2,a3,a4,ai...作为一个水桶的两侧,找出两个最大的元素,作为水桶的两边,求最大面积解 个人解法: 1.两个指针一个从后往前,一个从前往后,相减两边的距离之后乘以j与i的距离,得出结果与之前结果进行比较 2.然后如此暴力的解法超时了 参考解法: 这个方法比较灵巧,比较左右两边的边界,因为水的最大容量是短板决定的,所以比较之后则移动短板即可,每次移动重新计算面积与最大面积进行比较,得出结果 例如: 左边比右边小则移动左边的,右边比左边小则移动右边的. 相关代

吸血鬼数字算法

吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘得到,而这对数字各包含乘积一半位数的数字,其中从最初数字中选取的数字可以任意排序.以两个0结尾的数字是不允许的.下面是一些吸血鬼数字: 15*93: 139521*60: 126021*87: 1827 写一个程序找出4位数中的所有吸血鬼数字: 下列解法为Java编程思想提供的参考解法 1 int[] startDigit = new int[4]; //start digit 2 int[] productDigit = new int[4];