UVa 550 - Multiplying by Rotation
摘要:题意:给出一个进制,一个数的最低位,和另外的一个数,比如10进制,第一个数字的最低位是7,第二个数字是4,和规则(XXXXX7 * 4 = 7XXXXX,例子: 179487 * 4 = 717948)求出第一个数字的最小长度。看起来很难,其实动笔写写就明白了。输入k,m,n,原来的数字为s,因为s的最后一位为m,则s*n的最后一位为s*n%k,而s*n%k又是s的倒数第二位,这样又可以计算出ans*n的倒数第二位;以此类推,直到乘积+原来的进位==最低位。代码如下: 1 #include<iostream> 2 3 using namespace std; 4 5 int mai
阅读全文
posted @
2012-07-31 20:06
zrq495
阅读(532)
推荐(0)
UVa 10110 - Light, more light
摘要:设n=a*b,则a趟去的时候打开灯,b趟去的时候关上,相互抵消,当且仅当a=b时,第n个灯只开不关,即n为完全平方数。代码如下: 1 #include<iostream> 2 #include<cmath> 3 4 using namespace std; 5 6 int main() 7 { 8 long long n, m; 9 while(cin >> n, n)10 {11 m=sqrt(n);12 if (m*m == n) cout << "yes" << endl;13 else cout <&
阅读全文
posted @
2012-07-31 17:36
zrq495
阅读(227)
推荐(0)
UVa 575 - Skew Binary
摘要:看懂上图就出题了,注意给出的数字很长,可以用字符数组存放 。代码如下: 1 #include<iostream> 2 #include<cmath> 3 #include<cstring> 4 5 using namespace std; 6 7 int main() 8 { 9 int n, s;10 int i;11 char str[40];12 while(cin >> str, strcmp(str, "0"))13 {14 s=0;15 n=strlen(str);16 for (i=0; ...
阅读全文
posted @
2012-07-31 16:56
zrq495
阅读(185)
推荐(0)
HDU 2098 分拆素数和
摘要:1 #include<iostream> 2 #include<cmath> 3 4 using namespace std; 5 6 int prime(int n) 7 { 8 int i; 9 for (i=2; i<sqrt(n)+1; i++)10 {11 if (n%i == 0) return 0;12 }13 return 1;14 }15 16 int main()17 {18 int n, i;19 int cnt;20 while(cin >> n, n)21 {22 ...
阅读全文
posted @
2012-07-31 14:54
zrq495
阅读(151)
推荐(0)
HDU 1905 Pseudoprime numbers
摘要:题意:给一个 p 和 一个 a,如果这个p 本身就是一个素数,就输出 no,如果不是素数,那么计算( a ^ p) % p 如果结果等于 a 那么输出 yes 否则输出 no。快速幂取模就是在O(logn)内求出a^n mod b的值。算法的原理是(a*b) mod c=(a mod c)*(b mod c)mod c。代码如下: 1 #include<iostream> 2 #include<cmath> 3 4 using namespace std; 5 6 int prime(long long n) 7 { 8 int i; 9 for (i=2; i<
阅读全文
posted @
2012-07-31 14:51
zrq495
阅读(152)
推荐(0)
HDU 1465 不容易系列之一
摘要:错排: 当n个编号元素放在n个编号位置, 元素编号与位置编号各不对应的方法数用M(n)表示, 那么M(n-1)就表示n-1个编号元素放在n-1个编号位置, 各不对应的方法数, 其它类推. 第一步, 把第n个元素放在一个位置, 比如位置k,一共有n-1种方法; 第二步,放编号为k的元素,这时有两种情况. 1,把它放到位置n,那么,对于剩下的n-2个元素,就有M(n-2)种方法; 2,不把它放到位置n,这时,对于这n-2个元素,有M(n-1)种方法; 综上得到 M(n)=(n-1)[M(n-2)+M(n-1)] 特殊地,M(1)=0,M(2)=1最小的几个错排数是:D...
阅读全文
posted @
2012-07-31 14:23
zrq495
阅读(166)
推荐(0)
HDU 2503 a/b + c/d
摘要:1 #include<iostream> 2 3 using namespace std; 4 5 int gcd(int a, int b) 6 { 7 return b?gcd(b, a%b):a; 8 } 9 10 int main()11 {12 int a, b, c, d, m, k;13 int n;14 cin >> n;15 while(n--)16 {17 cin >> a >> b >> c >> d;18 int t=gcd(b,d);19 m=a*d/t+b*c/t;...
阅读全文
posted @
2012-07-31 14:19
zrq495
阅读(114)
推荐(0)
HDU 1713 相遇周期
摘要:求a/b与c/b的最小公倍数。要求最小公倍数,那么结果肯定是分子尽量小,即 求a , c 的最小公倍数, 分母尽量大, 即求 b , d 的最大公约数。、求最大公约数:int gcd(int a, int b){ return b?gcd(b, a%b):a;}代码: 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int gcd(int a, int b) 7 { 8 return b?gcd(b, a%b):a; 9 }10 11 int lcm(int a, int b)12
阅读全文
posted @
2012-07-31 14:13
zrq495
阅读(285)
推荐(0)
POJ 1942 Paths on a Grid
摘要:代码: 1 #include<iostream> 2 3 using namespace std; 4 5 long long comb(long long m, long long n) 6 { 7 long long s=1; 8 m=n-m>m?m:n-m; 9 long long k=1, i;10 for (i=0; i<m; i++)11 {12 s*=n-i;13 while(k <=m && s%k==0)14 {15 s/=k;16 k+...
阅读全文
posted @
2012-07-31 13:59
zrq495
阅读(270)
推荐(0)
HDU 1028 Ignatius and the Princess III
摘要:差不多懂了,解析过程别人写了,代码自己写的。在正整数n的所有不同划分中,将最大加数n1不大于m的划分个数记为q(n,m)。可以建立q(n,m)的如下递归关系:<1> q(n,m) = 1, n >= 1当最大加数n1不大于1时,任何正整数n只有一种划分形式,n = 1 + 1 + 1 +...+ 1<2> q(n,m) = q(n,n), m >= n最大加数n1实际上不能大于n<3> q(n,n) = 1 + q(n,n - 1)正整数n的划分由n1 = n的划分和n1 < n - 1的划分组成<4> q(n,m) = q(n
阅读全文
posted @
2012-07-31 11:40
zrq495
阅读(224)
推荐(0)
UVa 10014 - Simple calculations
摘要:给出一个数列ci(1<=ci<=n),然后给出数列ai中的a0和a(n+1),并给出一个公式ai = ( a(i-1) + a(i+1) ) / 2 - ci。求a1。做了很长时间,也没弄对,最后看的别人的解题报告。。。。。由给出公式得:2ai = a(i-1) + a(i+1) -2ci ,依次使i=1,2......n,写出n个式子,把它们相加,得到:a1 + an = a0 + a - 2(c1 + …… + cn), 再使c的下标i=1......n,写出n个式子,把他们相加。得到:(n + 1)a1 = na0 + an+1 - 2(nc1 + (n-1)c2 + (n-
阅读全文
posted @
2012-07-29 19:36
zrq495
阅读(377)
推荐(0)
UVa 10970 - Big Chocolate
摘要:没啥好说的,输入n,m,输出 n*m-1。。。代码: 1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int n, m; 8 while(cin >> n >> m) 9 cout << n*m-1<< endl;10 return 0;11 }
阅读全文
posted @
2012-07-28 16:03
zrq495
阅读(172)
推荐(0)
UVa 10916 - Factstone Benchmark
摘要:m! <= 2^n ,求最大的m;两边同时取对数。 1 #include<iostream> 2 #include<cmath> 3 4 using namespace std; 5 6 int f[21]; 7 8 int main() 9 {10 int n, i, j, k;11 double sum=0;12 for (i=0, j=0, k=4; i<21; i++, k*=2)13 {14 for (j++; ; j++)15 {16 sum+=log10((double)j);17 ...
阅读全文
posted @
2012-07-28 09:56
zrq495
阅读(173)
推荐(0)
UVa 10719 - Quotient Polynomial
摘要:令q(x)=bmX^m+bm-1X^(m-1)+...b1X+b0,展开 q(x) * (x-k) = X^(m+1)+[-Kbm+b(m-1)]X^m+...(K+b0)x+K。相同次数的项的系数相等,所以推出 b(m-1)=k*bm+a[n-1], r=k*b0+a[0]。代码如下: 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int n, p[10010], q[10010], i, k; 9 while (cin >> k)
阅读全文
posted @
2012-07-27 17:25
zrq495
阅读(147)
推荐(0)
UVa 11044 - Searching for Nessy
摘要:求n*m的网格中有多少九宫格,公式:(n/3)*(m/3)。代码: 1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int n, m; 8 int t; 9 cin >> t;10 while(t--)11 {12 cin >> n >> m;13 cout << (n/3)*(m/3) << endl;14 }15 return 0;16 }
阅读全文
posted @
2012-07-27 15:34
zrq495
阅读(171)
推荐(0)
URAL 1100
摘要:Problem DescriptionOld contest software uses bubble sort for generating final standings. But now, there are too many teams and that software works too slow. You are asked to write a program, which generates exactly the same final standings as old software, but fast.InputThe first line of input conta
阅读全文
posted @
2012-07-27 14:49
zrq495
阅读(187)
推荐(0)
HDU 1715 大菲波数
摘要:大数加法 + 打表。代码: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 6 using namespace std; 7 8 struct node 9 {10 char s[1000];11 };12 13 int main()14 {15 struct node f[1001];16 int i, j, l, n, m;17 int a[1000], b[1000], r[1000];18 int alen, blen, l
阅读全文
posted @
2012-07-27 14:33
zrq495
阅读(214)
推荐(0)
UVa 10790 - How Many Points of Intersection?
摘要:固定下面的个数,依次增加上面点的个数,公式:n*(n-1)*m*(m-1)/4 。代码: 1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 long long int n, m, c=1; 8 while(cin >> n >> m, n||m) 9 cout << "Case " << c++ << ": " << n*(n-1)*m*(m-1)/4 << endl;10 r
阅读全文
posted @
2012-07-26 20:54
zrq495
阅读(207)
推荐(0)
UVa 10499 - The Land of Justice
摘要:球的表面积 :4*π*r*r, 分割后多出的面积 :n*π*r*r,利润 :25*n % 。当n=1时, 利润:0%。 1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 long long n; 8 while(cin >> n, n>0) 9 {10 if (n == 1) cout << "0%" << endl;11 else cout << 25*n << "%" << en
阅读全文
posted @
2012-07-26 19:24
zrq495
阅读(196)
推荐(0)
UVa 846 - Steps
摘要:最小步数 距离 0 01 12 23 44 65 96 12代码: 1 #include<iostream> 2 #include<cmath> 3 4 using namespace std; 5 6 int main() 7 { 8 int T, n, m; 9 int dis, step;10 cin >> T;11 while(T--)12 {13 cin >> n >> m;14 dis=m-n;15 if (dis == ...
阅读全文
posted @
2012-07-26 17:10
zrq495
阅读(272)
推荐(0)
UVa 573 - The Snail
摘要:蜗牛,白天向上爬,晚上向下滑,还有疲劳(常数)。蜗牛不能向下爬,所以蜗牛爬行的距离不能是负数。代码: 1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int flag, day; 8 double h, u, d, p, t; 9 while(cin >> h >> u >> d >> p, h)10 {11 day=0;12 flag=0;13 t=0;14 p=p*u*1.0/100;15 ...
阅读全文
posted @
2012-07-26 14:52
zrq495
阅读(332)
推荐(0)
C/C++头文件一览
摘要:注:本文转自 http://developer.51cto.com/art/200509/3578.htmC、传统 C++#include <assert.h>//设定插入点#include <ctype.h> //字符处理#include <errno.h> //定义错误码#include <float.h> //浮点数处理#include <fstream.h> //文件输入/输出#include <iomanip.h> //参数化输入/输出#include <iostream.h> //数据流输入/输出#
阅读全文
posted @
2012-07-25 19:23
zrq495
阅读(145)
推荐(0)
hdu 1047 Integer Inquiry
摘要:大数加法。注意:输入100000输出0代码如下: 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int main() 5 { 6 int T, i; 7 char a[1000]; 8 int sum[1000], b[1000]; 9 cin >> T;10 while(T--)11 {12 memset(a, 0, sizeof(a));13 ...
阅读全文
posted @
2012-07-25 18:41
zrq495
阅读(302)
推荐(0)
UVa 591 - Box of Bricks
摘要:水题!看懂题马上就能写出来。 1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int T=1, n, i, sum; 6 int a[60]; 7 while(cin >> n, n) 8 { 9 sum=0;10 int ct=0;11 for (i=0; i<n; i++)12 {13 cin >> a[i];14 sum+=a[i];15 }16 ...
阅读全文
posted @
2012-07-24 21:16
zrq495
阅读(228)
推荐(0)
UVa 10025 - The ? 1 ? 2 ? ... ? n = k problem
摘要:当k=0时,n=3;当k!=0时,找最小的n,使sum=1+2+3+....+n >= |k|,且sum为偶数。代码:#include<iostream>using namespace std;int main(){ int T, i, j, k, sum, p; cin >> T; while(T--) { sum=0; cin >> k; if (k < 0) k=-k; if (!k) cout << "3" << endl; else { i=0; ...
阅读全文
posted @
2012-07-24 20:56
zrq495
阅读(337)
推荐(0)
UVa 621 - Secret Research
摘要:水题,简单。 1 #include<iostream> 2 #include<cstring> 3 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 char str[10000];10 cin >> n;11 while(n--)12 {13 cin >> str;14 if (!strcmp(str, "1") || !strcmp(str, "4") || !strcmp(str, "78"))15 cout <<
阅读全文
posted @
2012-07-24 19:19
zrq495
阅读(246)
推荐(0)
UVa 253 - Cube painting
摘要:求两个正方体的带有的颜色是否相同。最少2次旋转就可以得到12种情况。ac代码: 1 #include<iostream> 2 #include<stdio.h> 3 4 using namespace std; 5 6 int main() 7 { 8 int i, j; 9 char c1[7], c2[7], str[13];10 while(scanf("%s", str)!=EOF)11 {12 for (i=0; i<6; i++)13 c1[i]=str[i];14 for (; i<12;...
阅读全文
posted @
2012-07-24 16:04
zrq495
阅读(228)
推荐(0)
UVa 10161 - Ant on a Chessboard
摘要:对角线上的数字满足 an = n * (n - 1) + 1;再通过列或者行的奇偶性以及与对角线数字的关系得到坐标。代码如下: 1 #include<iostream> 2 #include<cmath> 3 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 while(cin >> n, n)10 {11 int c=(int)ceil(sqrt(n));12 int a=c*(c-1)+1;13 if (c & 1)14 {15 ...
阅读全文
posted @
2012-07-24 14:15
zrq495
阅读(358)
推荐(0)
蛋疼的英语
摘要:Problem Descriptionjxust_acm 队的同学都知道Xianbin5的英语特别差,因而他每次比赛都要队友来给他翻译题目,然而现在队友回家了,他为了提高英语,决定看一篇很长的英文文章,文章有N个单词(0<N<=50000),但是他的词汇量只有M个单词(0<M<=5000)。在看文章的过程中,如果遇到他不会的单词,那么他就会去查字典,查过的单词他会了,那么以后遇到就不用再查了。现在给出文章和Xianbin5已经掌握的单词。让求他最小要查字典的次数。Input含多组样例(小于5组),每组样例为:首先给出N和M,用空格分开。然后给出一行英文其中包含N个单词,
阅读全文
posted @
2012-07-23 18:51
zrq495
阅读(248)
推荐(0)
UVa 113 - Power of Cryptography
摘要:相同的代码用C就TLE,用C++就AC了..=_=///代码: 1 #include<stdio.h> 2 #include<math.h> 3 4 int main() 5 { 6 long long n; 7 double p; 8 while(scanf("%lld%lf", &n, &p)==2) 9 {10 printf("%.lf\n", pow(p, 1.0/n));11 }12 return 0;13 }
阅读全文
posted @
2012-07-22 20:44
zrq495
阅读(93)
推荐(0)
堆排序练习 hdu 1425 sort
摘要:不只是堆排序,其他排序也能过,很水的题,拿来练习堆排序的。代码: 1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int a[1000002]; 5 6 void heapadjust(int *a, int k, int n) 7 { 8 int i, j; 9 i=k;10 j=2*i;11 a[0]=a[i];12 while(j<=n)13 {14 if ((j<n) && a[j]>a[j+1])15 j++;16 if (a[0]>a[j])17 {...
阅读全文
posted @
2012-07-21 19:01
zrq495
阅读(256)
推荐(0)
UVa 755 - 487--3279
摘要:开始用字符串TLE,后来改成整数AC。先排序,再计数。代码: 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 5 typedef struct 6 { 7 int num; 8 int count; 9 }node;10 11 int cmp(const void *a, const void *b)12 {13 node *pa=(node *)a;14 node *pb=(node *)b;15 return pa->num-pb->num;16 }17 18 i
阅读全文
posted @
2012-07-18 11:37
zrq495
阅读(245)
推荐(0)
UVa 156 - Ananagrams
摘要:题意:给出一些单词,以#结束,判断这些单词是否是ananagram。如果是按字典序输出。思路:每个大写字母变成小写,把单词中的字母按字典序排,再把单词按字典序排序,然后判断。最后输出没有改变的单词。 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 5 typedef struct 6 { 7 char s1[90]; //存放最初的单词。 8 char s2[90]; //存放小写排序后的单词。 9 int flag; //标记是否ananagram。10 }word...
阅读全文
posted @
2012-07-17 16:32
zrq495
阅读(190)
推荐(0)
UVa 152 - Tree's a Crowd
摘要:题意:求每一颗树距其他树最近的距离,如果小于10,那么相对应的距离点+1,输出距离为0,1,2,3,4,5,6,7,8,9的点的个数。用两个for循环遍历。代码如下: 1 #include<stdio.h> 2 #include<math.h> 3 4 typedef struct 5 { 6 int l, w, h; 7 }node; 8 9 int dis(node a, node b)10 {11 return (int)(sqrt( (double)((a.l-b.l)*(a.l-b.l)+(a.h-b.h)*(a.h-b.h) + (a.w-b.w)*(a.w
阅读全文
posted @
2012-07-17 10:21
zrq495
阅读(299)
推荐(0)