西工大C语言POJ作业

更新时间:2023-08-09 13:49:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

西工大C语言POJ作业

T071、大数乘法

#include <stdio.h> #include <string.h> #include <stdlib.h>

void cheng(char a[],char b[]) {

int i,j,ca,cb,*s; ca=strlen(a); cb=strlen(b);

s=(int *)malloc(sizeof(int)*(ca+cb)); //分配存储空间 for (i=0;i<ca+cb;i++) s[i]=0; // 每个元素赋初值0

for (i=0;i<ca;i++)

for (j=0;j<cb;j++)

s[i+j+1]+=(a[i]-'0')*(b[j]-'0');

for (i=ca+cb-1;i>=0;i--) // 这里实现进位操作 if (s[i]>=10) {

s[i-1]+=s[i]/10; s[i]%=10; } i=0;

while(s[i]==0) i++; // 跳过头部0元素

西工大C语言POJ作业

for (;i<ca+cb;i++) printf("%d",s[i]); printf("\n"); free(s); }

int main() {

char a[100],b[100]; gets(a),gets(b);

if(a[0]=='-'&&b[0]!='-') {

printf("-");

cheng(&a[1],b); }

else if(a[0]=='-'&&b[0]=='-') {

cheng(&a[1],&b[1]); }

else if(a[0]!='-'&&b[0]=='-') {

printf("-");

cheng(a,&b[1]); } else

cheng(a,b); return 0; }

T072、创建与遍历职工链表

西工大C语言POJ作业

#include <stdio.h> #include <stdlib.h> struct member{ int data;

struct member*next; };

struct member *creat(struct member*head) {

struct member *p1,*p2=NULL; int i,n;

scanf("%d",&n); for(i=0;i<n;i++) {

p1=(struct member*)malloc(sizeof(struct member)); scanf("%d",&p1->data); if(i==0)

head=p1; else

p2->next=p1; p2=p1; }

p2->next=NULL; return head; }

西工大C语言POJ作业

void list(struct member*head) {

struct member *p=head; while(p!=NULL) {

printf("%d ",p->data); p=p->next; }

printf("\n"); }

int main() {

struct member *head; head=creat(head); list(head); return 0; }

T073、幸运儿

西工大C语言POJ作业

#include <stdio.h> int main() {

int a[51]={0},b[20],x,count=0,num=0,j=0,n,i,flag=0; scanf("%d%d",&n,&x); for(i=0;i<20;i++) scanf("%d",&b[i]); while(1) {

for(i=1;i<=n;i++) {

if(a[i]==0) num++; if(num==b[j]) {

a[i]=1; num=0; count++; }

if(count==n-x) {

flag=1; break; } }

if(flag==1) break; j++; num=0; }

for(i=1;i<=n;i++) if(a[i]==0)

printf("%d ",i);

西工大C语言POJ作业

printf("\n"); return 0; }

T074、插入链表节点

#include <stdio.h> #include <stdlib.h> struct member{ int data;

struct member*next; };

struct member *creat(struct member*head) {

struct member *p1,*p2=NULL; int i,n;

scanf("%d",&n); for(i=0;i<n;i++) {

p1=(struct member*)malloc(sizeof(struct member)); scanf("%d",&p1->data); if(i==0)

head=p1; else

p2->next=p1; p2=p1; }

西工大C语言POJ作业

p2->next=NULL; return head; }

struct member *insert(struct member*head) {

struct member *p0=head,*p1;

p1=(struct member *)malloc(sizeof(struct member)); scanf("%d",&p1->data); while(p0->next!=NULL) {

if(p0->data<p1->data&&p0->next->data>p1->data) {

p1->next=p0->next; p0->next=p1; }

p0=p0->next; }

return head; }

void list(struct member*head) {

struct member *p=head; while(p!=NULL) {

printf("%d ",p->data); p=p->next; }

printf("\n"); }

int main() {

struct member *head; head=creat(head); head=insert(head); list(head); return 0; }

T075、毕业设计论文打印

西工大C语言POJ作业

#include <stdio.h> int main() {

int n,m,a[100]={0},count=0,flag=0,i,j; scanf("%d%d",&n,&m); for(i=0;i<n;i++)

scanf("%d",&a[i]); while(1) {

for(i=0;i<n&&a[i]!=0;i++) {

for(j=i;j<n;j++) if(a[j]>a[i]) break; if(j==n) {

西工大C语言POJ作业

a[i]=0; count++; if(i==m) {

flag=1; break; } } }

if(flag==1) break; }

printf("%d\n",count); return 0; }

T076、链表动态增长或缩短

#include <stdio.h> #include <stdlib.h>

#define LEN sizeof(struct student) int a[10000]; struct student {

int age;

struct student *next;

西工大C语言POJ作业

}; int n;

struct student *creat(int num) {

struct student *head; struct student *p1,*p2; n=0;

p1=p2=(struct student*)malloc(LEN); scanf("%d",&p1->age); head=NULL; while(n<num) {

n=n+1; if(n==1) head=p1; else

p2->next=p1; p2=p1;

p1=(struct student *) malloc(LEN); if(n<num)

scanf("%d",&p1->age);

}

p2->next=NULL; return head; }

struct student *del(struct student *head) {

struct student *p1,*p2,*p3,*p4,*p5; p1=head; p5=head; do {

if(a[p1->age]==1&&p1->next!=NULL) {p2->next=p1->next; p3=p5;

while(p3!=NULL) {

if(p3->age==p1->age) { if(p3==head) head=p3->next; else

西工大C语言POJ作业

p4->next=p3->next;} p4=p3;

p3=p3->next; }}

else if(a[p1->age]==1&&p1->next==NULL) {p2->next=NULL; p3=p5;

while(p3!=NULL) {

if(p3->age==p1->age) {if(p3==head) head=p3->next; else

p4->next=p3->next;} p4=p3;

p3=p3->next; }}

else a[p1->age]=1; p2=p1;

p1=p1->next;

} while(p1!=NULL); return head; }

int main() {

int num;

scanf("%d",&num); struct student *p; p=creat(num); p=del(p); do {

printf("%d ",p->age); p=p->next; }while(p!=NULL); return 0; }

T077、大数减法

西工大C语言POJ作业

#include <stdio.h> #include <string.h>

#define max(a,b) a>b?a:b #define min(a,b) a>b?b:a int sum[101];

void jia(char a[],char b[]) {

int flag=0,i,len1,len2,lenmax,lenmin; len1=strlen(a),len2=strlen(b);

lenmax=max(len1,len2),lenmin=min(len1,len2); for(i=1;i<=lenmax;i++) {

if(i<=lenmin) {

sum[lenmax-i]=a[len1-i]+b[len2-i]-'0'-'0'+flag; if(sum[lenmax-i]>=10) {

sum[lenmax-i]=sum[lenmax-i]-10; flag=1; }

else flag=0; } else{

if(len1>len2)

西工大C语言POJ作业

{

sum[lenmax-i]=a[len1-i]-'0'+flag; if(sum[lenmax-i]>=10) {

sum[lenmax-i]=sum[lenmax-i]-10; flag=1; }

else flag=0; } else {

sum[lenmax-i]=b[len2-i]-'0'+flag; if(sum[lenmax-i]>=10) {

sum[lenmax-i]=sum[lenmax-i]-10; flag=1; }

else flag=0; } } }

if(flag==1) printf("1");

for(i=0;i<lenmax;i++) printf("%d",sum[i]); printf("\n"); }

void jian(char a[],char b[]) {

int flag=0,i; int len1,len2;

len1=strlen(a),len2=strlen(b); for(i=1;i<=len1;i++) {

if(i<=len2) {

if(a[len1-i]-b[len2-i]-flag>=0) {

sum[len1-i]=a[len1-i]-b[len2-i]-flag; flag=0; } else {

sum[len1-i]=a[len1-i]-b[len2-i]-flag+10;

西工大C语言POJ作业

flag=1; } } else {

if(a[len1-i]-flag-'0'>=0) {

sum[len1-i]=a[len1-i]-flag-'0'; flag=0; } else {

sum[len1-i]=a[len1-i]-flag+10-'0'; flag=1; } } }

for(i=0;i<len1&&sum[i]==0;i++); for(;i<len1;i++)

printf("%d",sum[i]); printf("\n"); }

void compare(char a[],char b[]) {

int len1,len2;

len1=strlen(a),len2=strlen(b);

if(len1>len2||(len1==len2&&strcmp(a,b)>0)) jian(a,b);

else if(strcmp(a,b)!=0) {

printf("-"); jian(b,a); } else

printf("0\n"); }

int main() {

char a[100],b[100]; gets(a); gets(b);

if(a[0]=='-'&&b[0]!='-') {

西工大C语言POJ作业

printf("-"); jia(&a[1],b); }

else if(a[0]=='-'&&b[0]=='-') compare(&b[1],&a[1]); else if(a[0]!='-'&&b[0]=='-') jia(a,&b[1]); else

compare(a,b); return 0; }

T078、大数除法

#include<stdio.h> #include<string.h> int len1,len2; int re[105];

void sub(char s1[],char s2[]) {

int i=0; int j; while(1) {

if(s1[i]=='0') i++;

西工大C语言POJ作业

{ j=i; break; } }

for(;i<len2;i++) {

s1[i]=s1[i]-s2[i]+'0'; }

for(i=len2-1;i>j;i--) //低位开始检测是否小于0 {

if(s1[i]<'0') {

s1[i]+=10; s1[i-1]--; } } }

void chu(char s1[],char s2[]) {

int p,i;

len1=strlen(s1); len2=strlen(s2);

if(len1<len2||(len1==len2&&strncmp(s1,s2,len2)<0)) {

printf("0\n"); return; } p=0; while(1) {

re[p]=0;

while(strncmp(s1,s2,len2)>=0)

//一直进行减法,直到不能减为止 {

sub(s1,s2); re[p]++; } p++;

if(len1==len2) break;

for(i=len2-1;i>=0;i--)

//在s2前面补0,以便进行减法运算

//如果a<b,直接输出0

西工大C语言POJ作业

s2[i+1]=s2[i]; }

s2[0]='0'; len2++;

s2[len2]='\0'; } i=0; while(1) {

if(re[i]==0) i++; else break; }

for(;i<p;i++)

printf("%d",re[i]); printf("\n"); }

int main(void) {

char s1[105]; char s2[105]; gets(s1),gets(s2);

if(s1[0]=='-'&&s2[0]!='-') {

printf("-");

chu(&s1[1],s2); }

else if(s1[0]=='-'&&s2[0]=='-') chu(&s1[1],&s2[1]);

else if(s1[0]!='-'&&s2[0]=='-') {

printf("-");

chu(s1,&s2[1]); } else

chu(s1,s2); return 0; }

T079、链表节点删除

西工大C语言POJ作业

#include <stdio.h> #include <stdlib.h> struct student{ int no;

char name[12]; int age;

struct student *next; };

struct student *deletelist(struct student *head,int n) {

struct student *p=head,*q; if(p->no==n) {

head=p->next; free(p); } else{

while(p->next!=NULL&p->next->no!=n) p=p->next;

if(p->next->no==n) {

q=p->next;

p->next=q->next; free(q);

西工大C语言POJ作业

} }

return head; }

void list(struct student *head) {

struct student *p=head; while(p->next!=NULL) {

printf("%d ",p->no); p=p->next; }

printf("\n"); }

int main() {

int n,i=0;

struct student *head,*p1,*p2;

p1=p2=(struct student *)malloc(sizeof(struct student)); for(i=0;;i++) {

if(i==0) head=p1; else

p2->next=p1; p2=p1;

scanf("%d",&p1->no); if(p1->no==0) break;

scanf("%s %d",p1->name,&p1->age);

p1=(struct student *)malloc(sizeof(struct student)); }

p2->next=NULL; scanf("%d",&n); list(head);

head=deletelist(head,n); list(head); return 0; }

T080、行程编码压缩算法

西工大C语言POJ作业

#include <stdio.h> #include <stdlib.h>

int main(void) {

char line[100],a; int i,k,m; k=1; m=0; gets(line); a=line[0];

for(i=1;line[i]!='\0';i++) {

if(a!=line[i]) {

if(m==0&&k==1) {

printf("1"); m=1;

西工大C语言POJ作业

}

if(a=='1'&&m==1) printf("11"); if(m==1&&a!='1') printf("%c",a); if(k!=1) {

printf("%d%c",k,a); k=1; } } else {

if(m==1) {

printf("1"); m=0; }

if(k==9) {

printf("9%c",a); k=1; }

else k++; }

a=line[i]; }

if(k!=1) {

printf("%d%c",k,a); } else {

if(m==0) {

if(a=='1')

printf("1111"); else

printf("1%c1",a); } else {

if(a=='1')

printf("111"); else

printf("%c1",a);

本文来源:https://www.bwwdw.com/article/rknj.html

Top