博文
POJ2575(2009-12-25 07:42:00)
摘要:#include <stdio.h>#include <math.h>
int main(){ int i; int n; int arr[3000]; int is_jolly(int arr[],int n);
while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) scanf("%d",&arr[i]); if(is_jolly(arr,n)) printf("Jolly\n"); else printf("Not jolly\n"); }
return 0;}
int is_jolly(int arr[],int n){ int *p; int i; int diff;
p=(int *) malloc(sizeof(int)*(n-1)); for(i=0;i<n-1;i++) p[i]=0;
for(i=0;i<n-1;i++) { diff=abs(arr[i]-arr[i+1]); if(diff>0 && diff<n) p[diff-1]++; else return 0; }
for(i=0;i<n-1;i++) { if(p[i]!=1) break; } if(i==n-1) return 1; else return 0;
free(p);}......
POJ1002(2009-12-25 06:14:00)
摘要:#include <stdio.h>#include <string.h>#include <malloc.h>
int main() { int n; int i,j; int iCurr; int count; char **str; char **str1; int no_duplicates=1; int *indices; void sort_string(char **s,int i,int * indices);
scanf("%d",&n);
str=(char**) malloc(sizeof(char*)*n); str1=(char**) malloc(sizeof(char*)*n); indices=(int*) malloc(sizeof(int)*n);
for(i=0;i<n;i++) { *(str+i)=(char*) malloc(sizeof(char)*30); *(str1+i)=(char*) malloc(sizeof(char)*8); }
for(i=0;i<n;i++) scanf("%s",*(str+i));
for(i=0;i<n;i++) { j=0; count=0; while(count<7) { if(str[i][j]!='-') { switch(str[i][j]) { case 'A': case 'B': case 'C':  ......
API写的贪食蛇(2009-12-20 12:19:00)
摘要:这两天闲来无事写了个。
点击下载
这两天闲来没事写了贪食蛇。测试版本,有时间再加点功能。用的是dev c++ 纯API按回车开始。按pause暂停。方向键控制。......
直接把main函数定义成递归实现字符串逆序输出(2009-12-13 11:49:00)
摘要:#include<stdio.h> char s[100];char *p=s;//这是一个递归算法实现多个字符串的逆序输出//以下为例//输入://s1 s2 s3//输出://s3 s2 s1//i表示命令行参数的个数//如果命令行没输入参数,默认参数有一个参数,是程序的路径//所以这里i=1int main(int i,char *q){ //加了参数trace函数的调用的变量 static int count_main=0; static int count_if=0; if(!--i) //第一次判断的时候i=1,--i=0,!0=1,所以满足条件 { gets(s); //读入多个字符串 printf("第%d次进入if",++count_if); //输出第几次进入if语句 while(*p)++p; //循环p向后移动,找到字符串的结尾,注意空格不是结尾 for(;p---s;*p==32?*p=0,main(i,p):0); //循环使p往前移动,直到遇到一个空格  ......
漩涡形矩阵产生(2009-12-11 09:51:00)
摘要:写的有点乱了
#include <iostream>#include <assert.h>#include <iomanip>using namespace std;int main(){ //define the directions const int right=1; const int down=2; const int left=3; const int up=4; //current direction int direction; //size of the matrix int n; //input the size of the matrix cout<<"input size of the matrix:"<<endl; cin>>n; //dynamically create the 2D array int **p=new int*[......
输出蛇形矩阵(2009-12-11 04:27:00)
摘要:练个题目
蛇行矩阵
Problem蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形。 Input本题有多组数据,每组数据由一个正整数N组成。(N不大于100) Output对于每一组数据,输出一个N行的蛇形矩阵。两组输出之间不要额外的空行。 矩阵三角中同一行的数字用一个空格分开。行尾不要多余的空格。 Sample Input5Sample Output1 3 6 10 152 5 9 144 8 137 1211
#include <iostream>#include <assert.h>#include <iomanip>using namespace std;int main(){ int NumLines; cout<<"输入行数:"; cin>>NumLines; assert(NumLines<=100 && NumLines>=2); //动态分配二维数组 int **p=new int* [NumLines]; for(int i=0;i<NumLines;i++) *(p+i)=new int[NumLines]; int output_width=NumLines*(NumLines+1)/2; int num_digits=1;&......
做了下07清华计算机考研上机第三题(2009-12-10 17:05:00)
摘要:
今天试了下,刚好半个小时做完哦
第三题(可执行文件名program3.exe)有若干张邮票,要求从中选取最少的邮票张数凑成一个给定的总值。如,有1分,3分,3分,3分,4分五张邮票,要求凑成10分,则使用3张邮票:3分、3分、4分即可。首先是要求凑成的邮票总值M,M<100然后是一个数N,N〈20,表示有N张邮票。接下来是N个正整数,分别表示这N张邮票的面值,且以升序排列。输出:能够凑成总值M的最少邮票张数。若无解,输出0。样例输入:10 5 1 3 3 3 4样例输出:3
#include <iostream>using namespace std;int main(){ int sum; int num; int num_of_combs=1; int sum_try; int num_need=0; cout<<"输入邮票总值:"; cin>>sum; cout<<"输入邮票张数:"; cin>>num; int *p=new int[num]; int *digits=new int[num]; cout<<"输入邮票面值(从小到大):\n"; for(int i=0;i<num;i++) &......
一道有意思的题目(2009-12-09 14:23:00)
摘要:将0到9这十个数字分成n组,每一组的数字之和等于输入的数,输入的数的个数等于n。例如:输入:40 5则程序需要把这十个数字分成两组,第一组所有数字之和为40,第二组之和为5。所以有:输出:0 1 2 3 4 6 7 8 95
#include <iostream>using namespace std;int main(){ const int N_OF_DIGITS=9; const int TOTAL_COMBS=512; const int MAX_NUM_GROUP=9; int bin[N_OF_DIGITS]={0}; int digits_record[N_OF_DIGITS]={0}; int digits[]={1,2,3,4,5,6,7,8,9}; int sum_of_combs[TOTAL_COMBS]={0}; int groups_sums[MAX_NUM_GROUP]; int comb_indices[MAX_NUM_GROUP]={0}; int n_of_groups; int i,j,k; void dec2bin(int n,int *arr); int IsOverlap(int arr1[],in......
一道ACM题目 练练递归(2009-12-09 06:41:00)
摘要:题目: 一个数字链由数字的每位平方相加而成。 例子:44->32->13->10->1->1 .4*4+4*4=32,3*3+2*2=13 ......... 给你一个数字N计算出N(包括N)以内的所有数据链最后的数是1的个数。1 <=N <=10000所有子函数都写成递归的哦!样式输出input N:10011013192328313244496870798286919497100
#include <stdio.h>#include <conio.h>#include <assert.h>#define SQUARE(a) a*aint main(){ int n; int i; void num2digits(int n, int *digits,int *count); int recursive(int n); puts("input N:"); scanf("%d",&n); for(i=1;i<=n;i++) { if(recursive(i)==1) printf("%d\n",i); } &......
简单练习题 帮网友做下(2009-12-08 22:17:00)
摘要:1、输入n值,输出如图所示的数字金字塔,如n=5的情况如下. 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 12、编程,输出这样一个三位数,该三位数等于其每位数字的阶乘之和。 即: abc = a! + b! + c!3、编程,输入顶行字符c和高n,输出如下例(c=’A’,n=5)所示的图形。 A B B C ......
