04 2012 档案
摘要:1. 第一次做位运算的题,参考了这段经典代码(n皇后问题):void Queen(int row, int ld, int rd)
{ int pos, p; int upperlim = (1 > 1); } } else ++sum;
}2. 以下是代码:/*
ID: dollar4
PROG: checker
LANG: C++
*/
#include #include #include #include #include #include #include using namespace std;
ofstream fou...
阅读全文
摘要:1.从数学的角度: 1.首位只能是质数2 3 5 72.其余位只能是1,3,7,93.若n=1,直接输出2,3,5 7直接DFS 1~9,加入当前数末尾,并判断是不是素数,是则递归处理下一位数,不是则回溯,直到depth>n。不会超时。2. 这道题折磨了我好久,一直出现Execution error: Your program (`sprime') exited with signal #11 (segmentation violation [maybe caused by accessing memory out of bounds, array indexing out of
阅读全文
摘要:1. 无数遍TLE的题,快疯了,最后没办法,只能看别人的代码,最好的一个代码,写了下来,但是还有一点地方不太明白,这是一种新的方法,以下是代码/*
ID: dollar4
PROG: pprime
LANG: C++
*/
#include #include #include #include #include using namespace std;
int modify(int x)
{ int t = x; x /= 10; while (x > 0) { t = t * 10 + x % 10; x /= 10...
阅读全文
摘要:1. 动态规划,f[i,j]=Max{f[i+1,j],f[i+1,j+1]}+a[i,j] (1
#include #include #include #include using namespace std;
long long num[1010][1010];
long max(long a, long b)
{ if (a > b) return a; else return b;
}
int main()
{ ofstream fout ("numtri.out"); ifstream fin ("numtri.in"); ...
阅读全文
摘要:1. 用dfs,[A][B][C]为三个状态,且只有6个操作,a倒b,a倒c,b倒a,b倒c,c倒a,c倒b。知道用深搜了,但是还是不知道怎么写,看了别人的代码,才会写dfs()这个函数了。其实确定深搜了之后,写这个函数的时候,不需要考虑递归是怎么进行的,只需要考虑这次和下一次的操作过程即可。2. 以下是代码:/*
ID: dollar4
PROG: milk3
LANG: C++
*/
#include #include #include #include #include using namespace std;
int a, b, c, p = 0, isin[20];
in...
阅读全文
摘要:1. 枚举可以算出来,但是肯定超时,所以要剪枝,以等差数列a+nb说明;2. 几个可以剪枝的地方:b的范围是从1~2*m*m/(n-1); a的范围是0~2*m*m - (n-1) * b然后循环判断是不是pq了剪枝不是很充分,但是最慢3秒多给过了,哈哈3.以下是代码:/*
ID: dollar4
PROG: ariprog
LANG: C++
*/
#include #include #include #include #include using namespace std; int main()
{ ofstream fout ("ariprog.out"); ...
阅读全文
摘要:1. 这道题有好多种解法,看NOCOW,用枚举,DFS,BFS都能求解。我看到这个题的时候,连最基本的枚举怎么做都想不出来,差距呐。。。;2. 我挑选了解题报告中代码最短的读懂然后写出来了:/*
ID: dollar4
PROG: clocks
LANG: C++
*/
#include #include #include #include #include #include #include using namespace std;
#define FOR(x) for (c[x] = 0; c[x] > temp; a[i] = temp / 3 % 4;
...
阅读全文
摘要:1.因为只有4个方块,所以枚举每个方块的选择顺序和放置方向(横放还是纵放),放置方式只有题目给出的6中基本模式,分别算出不同模式下最小的面积,更新最优解。第4、5个在本质上其实是一样。如图,不同模式对应的最小面积如下:设w1,w2,w3,w4表示4个方块的横长,h1,h2,h3,h4表示4个方块的纵长。w,h表示最小。1:w=w1+w2+w3+w4;h=max(h1,h2,h3,h4)2:w=max(w1+w2+w3,w4);h=max(h1,h2,h3)+h43:w=max(w1+w2,w3)+w4;h=max(h1+h3,h2+h3,h4)4:w=w1+w2+max(w3,w4);h=ma
阅读全文
摘要:1. 枚举,注意里边的几个剪枝,标在代码中了2. 我的代码:/*
ID: dollar4
PROG: crypt1
LANG: C++
*/
#include #include #include #include #include using namespace std;
int num[10], n;
bool cmp(int a, int b)
{ return a > n; int i, j, k, l, m, s1, s2, s3, s4, s5; int cnt = 0, mm, nn, mn; for (i = 0; i > num[i]...
阅读全文
摘要:1. 输入带回车符,空格,标点符号的字符串:while (fin >> temp) { str += temp; str += " "; }
unsigned int len = str.size();
str.resize(len - 1);2. 判断是不是回文3. 以下是代码:/*
ID: dollar41
PROG: calfflac
LANG: C++
*/
#include
#include
#include
#define maxn 20010
using namespace std;
char str1[maxn],str2[max...
阅读全文
摘要:1. 这道题想多了,想得很复杂,其实就是把输入的一排数按照从小到大的顺序排列,然后得到相邻两个数之间的差,按从大到小的顺序排序,用这个数组所有元素的和减去前m-1个数,然后再加上m,就得到结果2. 以下是我的代码:/*
ID: dollar4
PROG: barn1
LANG: C++
*/
#include #include #include #include #include using namespace std;
bool cmp1(int a, int b)
{ return a > b;
}
bool cmp2(int a, int b)
{ retur...
阅读全文
摘要:1. 贪心,其他没啥说的2. 我的代码:/*
ID: dollar4
PROG: milk
LANG: C++
*/
#include #include #include #include #include using namespace std;
struct Node
{ int price; int num;
} node[5000];
bool cmp(Node a, Node b)
{ return a.price > n >> m; for (i = 0; i > node[i].price >> node[i].num; sor...
阅读全文
摘要:1. 直接用了上道题的函数,另外,进制转换:string trans(int a)
{ string str1 = ""; int tmp; while (a) { tmp = a % base; if (tmp #include #include #include #include using namespace std;
int n, s;
bool checkp(string str)
{ int len = str.size(); for (int i = 0; i > n >> s; ...
阅读全文
摘要:1. 进制转换+回文判断+int与char之间相互转换;以下是代码:/*
ID: dollar4
PROG: palsquare
LANG: C++
*/
#include #include #include #include #include using namespace std;
int base;
bool checkp(string str)
{ int len = str.size(); for (int i = 0; i > base; for (i = 1; i #include #include #include #include ...
阅读全文
摘要:1. 比较简单,但是用到了文件读写,终于明白了给的test代码中的fout和fin是什么意思了,哈哈;2. 如果按照一般的思路,肯定会超时,所以把dict转换成数字,这样的算法效率是固定的以下是代码:/*
ID: dollar4
PROG: namenum
LANG: C++
*/
#include #include #include #include #include using namespace std;
char get_num(char a)
{ switch (a) { case 'A' : return '2'; cas...
阅读全文
摘要:1. 被这道题虐了无数次,每次编译通过,但是运行会错误(当时用的是string数组), 后来把string数组改成char[][]二维数组就好了;2. 给的参考代码是不对的3. 如果函数的返回类型是数组,只能通过指针来实现,但是参考代码给了一种巧妙的方法,把数组封装成结构体,然后返回结构体以下是我的代码:/*
ID: dollar4
PROG: transform
LANG: C++
*/
#include #include #include #include #include using namespace std;
typedef char array[10][10];
cha...
阅读全文
摘要:1. 算法:把所有的时间按照开始的时间从小到大排序,然后设当前cur为目前的至少有一个工作的时间段,判断下一个时间段是跟这个时间段交叉还是包含在这个时间段之内还是在这个时间段之外,根据这三种不同过的情况,分别得到答案。值得注意的是,最后要得到cur的时间段的值,因为前边的循环当中,没有判断跟这个时间段比较的情况。以下是代码:/*
ID: dollar4
PROG: milk2
LANG: C++
*/
#include #include #include #include #include using namespace std;
const int MAXN = 5000;
st...
阅读全文
摘要:1. 算法:从一个节点开始,假如是r,如果下一个是r或者w,继续,一直到下一个为b为止,然后从b开始看下一个,如果是b或者w,继续,如果是r停止,处理“环”的问题,用了求余,可以使结尾的下一个变成第一个,但是要注意,最后结果不能大于n。2. 这是O(n^2) 的复杂度吧。/*
ID: dollarzhaole
PROG: beads
LANG: C++
*/
#include #include #include using namespace std; int main()
{ ofstream fout ("beads.out"); ifstream fin ("
阅读全文
摘要:1. 蔡勒公式:来自百度百科w:星期; w对7取模得:0-星期日,1-星期一,2-星期二,3-星期三,4-星期四,5-星期五,6-星期六 c:世纪(前两位数) y:年(后两位数) m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算) d:日 [ ]代表取整,即只要整数部分。 下面以中华人民共和国成立100周年纪念日那天(2049年10月1日)来计算是星期几,过程如下: w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1 =49+[49/4]+[20/4]...
阅读全文
摘要:1. 文件输入输出好别扭;2. 第一次没注意到cas--之后for循环就没作用了,得不到结果。/*
ID: dollarzhaole
PROG: gift1
LANG: C++
*/
#include #include #include using namespace std;
struct Node
{ string name; int sendp;//送给几个人 int getm;//收到的钱 int sendm;//送出去的钱 int inim;//刚开始的钱
} node[11];
int cas;
int getname(string s...
阅读全文
摘要:1. 从今天开始做USACO了,系统的学习下;2. 有详细的解题报告,可以看人家的代码,使自己的代码更简洁,更规范。/*
ID: dollar4
PROG: ride
LANG: C++
*/
#include #include #include using namespace std; long getnum(string str)
{ long num = 1; for (unsigned int i = 0; i > str1 >> str2; num1 = getnum(str1); num2 = getnum(str2); if (n...
阅读全文
|