湖南工业大学笔记 其一
fufhaha湖南工业大学刷题笔记
2.1.8
1.最大公约数:
1 2 3 4 5 6 7 8 9 10 11
| int s(int a,int b){ while(a!=0){ int c=a%b; b=a; a=c; } return b; }
int min=(a*b)/s(a,b);
|
3.1.1,2
1.闰年判断条件:
2.冒泡排序:
1 2 3 4 5 6 7 8 9 10 11
| int s[n]; for(int i=0;i<n-1;++i){ for(int j=0;j<n-1-i;++j){ if(s[j]<s[j+1]){ int tmp; tmp=s[j]; s[j]=s[j+1]; s[j+1]=tmp; } } }
|
4.2.5
1.寻找完数:
范围对半除找
5.1.5*
1.亲密数问题:
1 2 3 4 5 6 7 8
| void calculateDivisors() { for (int i = 1; i<n; ++i) { for (int j = i*2; j <n; j += i) { sumDivisors[j] += i; } } }
|
5.2.3
1.拆分数字:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| void s(int n){ while(n!=0){ int a=n%10; n/=0; } } void s(int n){ if(n==0) return; int a=n%10; s(n/10); }
|
6.1.7,5*,4
1.杨辉三角问题:
1 2 3 4 5 6 7 8 9 10 11 12
| int s[n][n]
for(int i=0;i<n;++i){ s[i][0]=1; s[i][i]=1; }
for(int i=2;i<n;++i){ for(int j=1;j<i;++j){ s[i][j]=s[i-1][j]+s[i-1][j-1]; } }
|
2.二分寻找最小编号问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| int fen(int s[n],int n,int t){ int l=0; int r=n-1; int mid,id; while(r>=l){ mid=l+(r-l)/2; if(s[mid]==t){ id=mid+1; r=mid-1; } else if(s[mid]<t){ l=mid+1; } else if(s[mid]>t){ r=mid-1; } } return id; }
|
3.鞍点问题:
6.2.6
1.字符串按照字典大小顺序排序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
int compare(const void*a,const void *b){ const char**str1=(const char **)a; const char**str2=(const char **)b; return strncmp(*str1,*str2,100); }
int main(){ char *s[3]; for(int i=0;i<3;++i){ char buffer[100]; scanf(" %s",buffer); s[i]=strdup(buffer); } qsort(s,3,sizeof(char*),compare); for(int i=0;i<3;++i){ printf("%s\n",s[i]); free(s[i]); } return 0; }
|
7.1.(4*),3
1.约瑟夫问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <stdio.h>
int main() { int n; scanf("%d",&n); int s[n]; for(int i=0;i<n;++i){ s[i]=i+1; } int current=0; int count=0; int remaining=n; while(remaining>1){ if(s[current]!=0){ count++; if(count==3){ s[current]=0; count=0; remaining--; } } current=(current+1)%n;
} for(int i=0;i<n;++i){ if(s[i]!=0){ printf("%d",s[i]); } } return 0; }
|
2.strcmp函数
1 2 3 4 5 6 7 8 9 10 11 12 13
| int strcmp(char *p1, char *p2){ while(*p1!='\0' && *p2!='\0'){ if(*p1>*p2) { return 1; } else if(*p1<*p2){ return -1; } p1++; p2++; } return 0; }
|
9.1.2,1
1.找到in.txt文件并写入helloworld
1 2 3 4 5 6 7 8 9
| #include<stdio.h> void slove(){ FILE *A; A=fopen("in.txt","w"); fprintf(A,"helloworld"); fclose(A); }
|
2.从文件in.txt中读取文件
1 2 3 4 5 6 7 8 9
| #include<stdio.h>
void slove(){ FILE *a=fopen("in.txt","r"); int a,b,c; if(fscanf("%d %d %d",&a,&b,&c)!=3){ fclose(a); } }
|