刷题笔记day1
7-1 最大子列和问题
在线扫描算法
#include<stdio.h> int main() { int k; scanf("%d",&k); int a[100005]; int thissum=0; int maxsum=0; for(int i=0;i<k;i++) { scanf("%d",&a[i]); thissum += a[i]; if(thissum > maxsum) maxsum = thissum; if(thissum < 0) thissum = 0; } printf("%d\n",maxsum); }
7-2 一元多项式的乘法与加法运算
此题可用hash映射的思路,原因在于此题的指数均大于等于0.
#include<stdio.h> #include<stdlib.h> #include<string.h> struct node { int xi; int zhi; }; struct node a[1005]; struct node b[1005]; int jia[1005]; int cheng[2500]; int main() { int m,n; memset(jia,0,sizeof(jia)); memset(cheng,0,sizeof(cheng)); scanf("%d",&m); for(int i=0;i<m;i++) { scanf("%d%d",&a[i].xi,&a[i].zhi); jia[a[i].zhi] += a[i].xi; } scanf("%d",&n); for(int j=0;j<n;j++) { scanf("%d%d",&b[j].xi,&b[j].zhi); jia[b[j].zhi] += b[j].xi; } for(int ii=0;ii<m;ii++) { for(int jj=0;jj<n;jj++) { cheng[a[ii].zhi+b[jj].zhi] += a[ii].xi*b[jj].xi; } } int ans1=0; int ans2=0; for(int q=2499;q>=0;q--) { if(cheng[q]!=0) { if(ans1==1) printf(" "); ans1=1; printf("%d %d",cheng[q],q); } } if(ans1==0)printf("0 0"); printf("\n"); for(int k=1004;k>=0;k--) { if(jia[k]!=0) { if(ans2==1) printf(" "); ans2=1; printf("%d %d",jia[k],k); } } if(ans2==0) printf("0 0"); printf("\n"); }
6-1 单链表逆转 (要复习)
#include <stdio.h> #include <stdlib.h> typedef int ElementType; typedef struct Node *PtrToNode; struct Node { ElementType Data; PtrToNode Next; }; typedef PtrToNode List; List Read(); /* 细节在此不表 */ void Print( List L ); /* 细节在此不表 */ List Reverse( List L ); int main() { List L1, L2; L1 = Read(); L2 = Reverse(L1); Print(L1); Print(L2); return 0; } List Reverse( List L ) { List head = L; L = NULL; List q; while(head) { q = head; head = head->Next; q->Next = L; L = q; } return L; }
【PAT A1059】Prime Factors
只能进行质因子在2000之内的质数分解。可以修改pnum的最大值,以进行更大的质数的分解的实现。
#include<stdio.h> #include<stdlib.h> #include<string.h> int p=0; int pnum[2000]; int isp[2000]={0}; void prime()//生成素数数组 { for(int i=2;i<2000;i++) { if(isp[i]==0) { pnum[p++]=i; for(int j=i+i;j<2000;j+=i) isp[j]=1; } } } struct factor { int x;//质因子 int cnt;//次数 }fac[10]; int main() { prime(); int n; while(scanf("%d",&n)!=EOF) { int temp = n; if(n==1) { printf("1=1\n"); continue; } int f=0; while(n!=1) { for(int i=0;i<p;i++) { int ccount=0; while(n%pnum[i]==0) { ccount++; n /= pnum[i]; } if(ccount>0) { fac[f].x = pnum[i]; fac[f].cnt = ccount; f++; } } } printf("%d=",temp); for(int j=0;j<f;j++) { if(fac[j].cnt==1) printf("%d",fac[j].x); else printf("%d^%d",fac[j].x,fac[j].cnt); if(j!=f-1) printf("*"); } printf("\n"); } }
codeup 5.6.2 高精度加减法 & 高精度和低精度乘除法
#include<stdio.h> #include<string.h> struct bign { int d[1000]; int len; bign() { memset(d,0,sizeof(d)); len=0; } }; int compare(bign a, bign b)//比较大小 { if(a.len>b.len) return 1; //a大 else if(a.len<b.len) return -1;//b大 else { for(int i=a.len-1;i>=0;i--) { if(a.d[i]>b.d[i]) return 1; else if(a.d[i]<b.d[i]) return -1; } return 0; } } //高精度加法 bign add(bign a,bign b) { bign c; int carry = 0; for(int i=0;i<a.len||i<b.len,i++) { int temp = carry+a[i]+b[i]; c.d[c.len++] = temp % 10; carry = temp / 10; } if(carry != 0) { c.d[c.len++] = carry; } return c; } //高精度减法a-b bign sub(bign a,bign b) { bign c; int carry = 0; for(int i=0;i<a.len;i++) { if(a.d[i] < b.d[i]) { a.d[i + 1]--; a.d[i] += 10; } c.d[c.len++] = a.d[i] - b.d[i]; while(c.len-1 > =1 && c.d[c.len - 1]==0) { c.len--; } } return c; } //高精度与低精度的乘法 //!要注意b是int类型,不是bign bign multi(bign a,int b) { bign c; int carry = 0; for(int i=0;i<a.len;i++) { int temp = a.d[i] * b + carry; c.d[c.len++] = temp % 10; carry = temp / 10; } while(carry != 0)//注意此处与高精度加法的区别 { c.d[c.len++] = carry % 10; carry /= 10; } return c; } //高精度与低精度的除法 bign divide(bign a ,int b,int & r) //r为余数 { bign c; c.len = a.len; r = 0; for(int i = a.len - 1;i >= 0;i-- ) { r = r * 10 + a.d[i]; if(r < b) c.d[i] = 0; else { c.d[i] = r / b; r = r % b; } } while(c.len-1 >= 1 && c.d[len-1] == 0) { c.len--; } return c; }
【PAT 1050】螺旋矩阵
/* 思路分为三步: 1. 求解m n 2. 排序 3. 生成螺旋矩阵 四种状态: 1. i++ 2. j++ 3. i-- 4. j-- 两种记步器 1. 横 row = n 2. 竖 col = m-1 */ /*qsort用法*/ /* void qsort( void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *) ); int compareMyType (const void * a, const void * b) { if ( *(MyType*)a < *(MyType*)b ) return -1; if ( *(MyType*)a == *(MyType*)b ) return 0; if ( *(MyType*)a > *(MyType*)b ) return 1; } qsort example #include <stdio.h> #include <stdlib.h> int values[] = { 40, 10, 100, 90, 20, 25 }; int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main () { int n; qsort (values, 6, sizeof(int), compare); for (n=0; n<6; n++) printf ("%d ",values[n]); return 0; } */ #include<stdio.h> #include<stdlib.h> #include<math.h> #include<algorithm> using namespace std; int b[10005][10005]; bool cmp(int a,int b) { return a>b;//降序排列 } int main() { int x; scanf("%d",&x); int a[x]; for(int i=0;i<x;i++) { scanf("%d",&a[i]); } //排序 sort(a,a+x,cmp); //求m n,已知m>=n int m,n; int temp = (int)sqrt(x); for(int t = temp;t >= 1;t--) { if(x % t == 0) { n = t; m = x/n; break; } } /* if(n==1) { for(int i=0;i<m;i++) { printf("%d",a[i]); if(i!=m-1) printf("\n"); } return 0; } */ //printf("m:%d n:%d\n",m,n); //输出 int row = n; int col = m-1; int cnt = 0;//变换状态的次数 int ccount = 0;//已经输出的元素 int ii = 0; int jj = -1; int time; /* for(i=0;i<x;i++) { printf("%d ",a[i]); } printf("\n"); */ while(ccount != x) { if(cnt % 4 == 0 )//row++ { time = row; //printf("row:%d\n",row); while(time--) { jj++; b[ii][jj] = a[ccount]; //printf("b[%d][%d]:%d\n",ii,jj,b[ii][jj]); ccount++; } row--; cnt++; } else if(cnt % 4 == 1 )//col++ { time = col; //printf("%d",col); while(time--) { ii++; b[ii][jj] = a[ccount]; //printf("b[%d][%d]:%d\n",ii,jj,b[ii][jj]); ccount++; } //printf("ccount:%d\n",ccount); col--; cnt++; } else if(cnt % 4 == 2 )//row-- { time = row; while(time--) { jj--; b[ii][jj] = a[ccount]; ccount++; } row--; cnt++; } else if(cnt % 4 == 3 )//col-- { time = col; while(time--) { ii--; b[ii][jj] = a[ccount]; ccount++; } col--; cnt++; } } //printf("finish\n"); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(j!=0) printf(" "); printf("%d",b[i][j]); } printf("\n"); } }