算法
zhou554291911
这个作者很懒,什么都没留下…
展开
-
开灯问题
有n盏灯,编号1~n。第1个人把所有灯打开,第2个人按下所有编号为2的倍数的开关(这些灯将被关掉),第3个人按下所有编号为3的倍数的开关(其中关掉的灯将被打开,开着的灯将被关闭),依次类推。一共有k个人,问最后由哪些灯开着? 输入n和k,1≤k≤n≤1000。输出开着的灯编号。 样例输入: 7 3 样例输出: 1 5 6 7 分析:使用memset函数,将数组中一定长度中的内容替换成原创 2015-06-16 22:44:55 · 657 阅读 · 0 评论 -
倒三角形(triangle)
输入正整数n<=20,输出一个n层的倒三角形。例如,n=5时输出如下:######### ####### ##### ### ##include "stdafx.h"#include <math.h>int main(int argc, char* argv[]){ int n; scanf("%d", &n); for (int i = n; i >原创 2015-06-14 22:31:57 · 4582 阅读 · 1 评论 -
水仙花数(daffodil)
输出100-999中的所有水仙花数。若3位数ABC满足ABC=A^3+B^3+C^3,则称其为水仙花数。#include "stdafx.h"#include <math.h>int main(int argc, char* argv[]){ int a, b, c, abc; for (abc = 100; abc<= 999; abc++) { a = abc原创 2015-06-14 21:41:21 · 451 阅读 · 0 评论 -
子序列的和(subsequence)
输入两个正整数n#include <stdio.h>int main() { int n, m, i; double sum = 0; while(scanf("%d%d", &n, &m) != EOF){ for(i = n; i <= m; i++) sum += 1.0/i/i; printf("%.5f\n",原创 2015-06-14 23:15:10 · 288 阅读 · 0 评论 -
分数化小数(decimal)
输入正整数a,b,c,输出a/b的小数形式,精确到小数点后c位。a,b <= 10^6,c <= 100。输入包含多组数据,结束标记为a=b=c=0。 样例输入: 1 6 4 0 0 0 样例输出: Case 1:0.1667 分析:printf(“%.cf”, a/b); 前面的整数和.用a/b输出,后面的小数应该用循环输出,最后一位判断四舍五入。#include <stdio.h原创 2015-06-16 00:03:33 · 450 阅读 · 0 评论 -
数数字(Digit Counting, ACM/ICPC Danang 2007, UVa 1225)
把前n(n<=10000)个整数顺次写在一起:123456789101112…数一数0~9各出现多少次(输出10个整数,分别是0, 1, …, 9出现的次数)。#include <stdio.h>#include <string.h>#define maxn 10000char s[maxn];int act[10];int main(){ int i; while(scanf原创 2015-06-18 17:50:18 · 933 阅读 · 0 评论 -
周期串(Periodic Strings, UVa455)
A character string is said to have period k if it can be formed by concatenating one or more repetitions of another string of length k. For example, the string “abcabcabcabc” has period 3, since it is原创 2015-06-20 00:13:21 · 705 阅读 · 0 评论 -
得分(Score, ACM/ICPC Seoul 2005, UVa 1585)
给出一个由O和X组成的串(长度为1~80),统计得分。每个O的分数为目前连续出现的O的个数,X的得分为0。例如,OOXXOXXOOO的得分为1+2+0+0+1+0+0+1+2+3。 分析:此题很简单,两个变量一个用于记录当前O的分数,另外一个变量用来累加分数。#include <stdio.h>#include <string.h>#define maxn 80char s[maxn];int原创 2015-06-18 17:02:41 · 1102 阅读 · 1 评论 -
生成元(Digit Generator,ACM/ICPC Seoul 2005, UVa1583)
For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M .For example, the digit-sum of 245 is 256 (= 245原创 2015-06-17 19:03:44 · 544 阅读 · 0 评论 -
分子量(Molar Counting, ACM/ICPC Seoul 2007, UVa1586)
An organic compound is any member of a large class of chemical compounds whose molecules contain carbon. The molar mass of an organic compound is the mass of one mole of the organic compound. The molar原创 2015-06-19 00:21:31 · 558 阅读 · 0 评论 -
统计字符1的个数
下面的程序意图在于统计字符串中字符1的个数,可惜有瑕疵:#include <stdio.h>#define maxn 10000000+10int main(){ char s[maxn]; scanf("%s", s); int tot = 0; for(int i = 0; i < strlen(s); i++) if(s[i] == 1)原创 2015-06-18 00:18:50 · 1173 阅读 · 0 评论 -
猜数字游戏的提示(Master-Mind Hints , UVa 340)
MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the原创 2015-06-17 18:10:59 · 559 阅读 · 1 评论 -
回文词(Palindromes, UVa 401)
输入一个字符串,判断它是否为回文串以及镜像串。输入字符串保证不含数字0。所谓回文串,就是反转以后与原串相同,如abba和madam。所谓镜像串,就是左右镜像之后和原串相同,如2S和3AIAE。注意,并不是每个字符在镜像之后都能得到一个合法字符。(空白项表示该字符镜像后不能得到一个合法字符。)Character Reverse Character Reverse Character Reverse原创 2015-06-17 17:46:52 · 592 阅读 · 0 评论 -
TeX中的引号(TeX Quote, UVa 272)
(UVa代表弗吉尼亚大学,272是其OJ上的第272题) 在TeX中,左引号是”””,右引号是““”。输入一篇包含双引号的文章,你的任务是把它转换成TeX的格式。 样例输入; “To be or not to be,”quoth the Bard,”that is the question”. 样例输出: ”To be or not to be,“quoth the Bard,”that原创 2015-06-17 00:09:15 · 842 阅读 · 0 评论 -
蛇形填数
在n*n的方阵里填入1,2,…,n*n (n<=8),要求填成蛇形。 例如n=4时的方阵为: 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 上面的方阵中,多余的空格只是为了便于观察规律,不必严格输出。n<=8。 分析: 1. 轨迹为:(向下,向左,向上,向右)循环 2. 赋值x=0和y=n-1后立马作为数组a的原创 2015-06-16 23:16:22 · 834 阅读 · 0 评论 -
竞赛中常见的输入输出格式
样例输入: 8 2 8 3 5 1 7 3 6 4 -4 6 10 0 样例输出: Case 1: 1 8 4.375 Case 2: -4 10 3.000#include <stdio.h>#define INF 1000000000int main(){ int x, n = 0, ***kase = 0;*** while(scanf("%d", &n) =原创 2015-06-16 13:19:08 · 459 阅读 · 0 评论 -
WERTYU(UVa 10082)
Problem Description WERTYU A common typing error is to place the hands on the keyboard one row to the right of the correct position. So “Q” is typed as “W” and “J” is typed as “K” and so on. You are原创 2015-06-17 14:54:01 · 411 阅读 · 0 评论 -
排列(permutation)
用1,2,3……,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要求abc:def:ghi=1:2:3。按照“abc def ghi”的格式输出所有解,每行一个解。提示:不必太动脑筋。 分析:最大的数ghi是987,对应的abc是329。最小的数是123。所以for循环只需要控制abc从123到329就好。1-9相加是45,相乘是3628800。是否输出,只需判断和与积即可。原创 2015-06-16 00:52:05 · 289 阅读 · 0 评论