ACM今天学习总结(2015.08.07)
ACM今日学习总结(2015.08.07)
1.
全排列生成方法
Next_permutation:
next_permutation(a,a+2)
Prev_permutation
2.
#include <time.h>
double clock() / CLOCKS_PER_SEC // 输出时间
管道
命令行下
echo 20 | abc
自动将20 输入到 abc这个程序中
3.
IDE或者gdb跟踪调试
4.
printf的特殊用法:对于m.n的格式可以用如下方法表示
char ch[20];
printf("%*.*s\n",m,n,ch);
前边的*定义的是总的宽度,后边的定义的是输出的个数。
分别对应外面的参数m和n 。
这种方法的好处是可以在语句之外对参数m和n赋值,从而控制输出格式。 #include <stdio.h>
int main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%.*lf\n",c,(double)a/b);
return 0;
}
5.
从数组a复制k个元素到数组b
memcpy(b,a,sizeof(type)* k)
全部复制
memcpy(b,a,sizeof(a))
9.
蛇形填数
#include <cstdio>
#include <cstring>
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;
int a[50][50];
int main()
{
int t;
int x,y;
memset(a,0,sizeof(a));
int n;
scanf("%d",&n);
t = a[x = 1][y = n] = 1;
//printf("%d %d\n",t,a[1][4]);
while(t < n * n)
{
while(x < n && !a[x + 1][y])
a[++x][y] = ++t;
while(y > 1 && !a[x][y - 1])
a[x][--y] = ++t;
while(x > 1 && !a[x - 1][y])
a[--x][y] = ++t;
while(y < n && !a[x][y + 1])
a[x][++y] = ++t;
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
printf("%3d",a[i][j]);
puts("");
}
return 0;
}
10.
竖式问题
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <ctime>
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;
int main()
{
int i,ok,abc,de,x,y,z,cnt=0;
char s[20],buf[99];
scanf("%s",s);//读取字符串<CR>结束
for(abc=111;abc<=999;abc++)
for(de=11;de<=99;de++)
{
x=abc*(de%10);y=abc*(de/10);z=abc*de;//de%10得个位,de/10得十位
sprintf(buf,"%d%d%d%d%d",abc,de,x,y,z);//把整数放到字符数组里每个字符一单元(abc是一个整数放在字符数组里是三个字符,像printf在屏幕上输出的一样abcdexyz成一个字符串)
ok=1;
for(i=0;i<strlen(buf);i++)//strlen(buf)buf中字符串长度不包涵最后的“/0”
if(strchr(s,buf[i])==NULL) ok=0;/*strchr(s,buf[i])是指向buf[i]在s中出现位置的指针,没有则返回NULL*/
if(ok)
{
printf("<%d>\n",++cnt);
printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n",abc,de,x,y,z);//一次性输出技巧
}
}
printf("the number of solutions = %d\n",cnt);
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。