随笔分类 - C语言
摘要:View Code include<iostream>#include<cstdio>#include<string>using namespace std;int main(){ string a,b; while(cin>>a>>b) { if(a[0]=='+'&&a[1]=='0'&&a.size()==2)a="0"; if(a[0]=='-'&&a[1]=='0'&&a.siz
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 int main() 5 { 6 int n; 7 loop: 8 while(scanf("%d",&n)!=-1) 9 {10 int i,j,k;11 for(i=1;i<100;i++)//题目告诉测试实例不超过10000,也就意味着循环不超过10012 {13 for(j=1;j<100;j++)14 ...
阅读全文
摘要:View Code 1 #include <stdio.h> 2 int main() 3 { 4 int z, p, n; 5 scanf("%d", &z); 6 while (z-- != 0) 7 { 8 scanf("%d", &n); 9 p = 0;10 while (n%2== 0)11 {12 ++p;13 n=n/2;14 }15 printf("%d %d\n", n, p);16 }17...
阅读全文
摘要:杭电1302题意:一个蜗牛在某容器的底部,容器高度为h,蜗牛每次白天行走的距离逐渐减小,减小系数为f;第一天白天爬行的距离为u,以后每次白天爬行的距离都减小原来的u*f/100.0,蜗牛夜间休息会滑落d,最后看看蜗牛是否能爬到顶部;成功失败都输出所需的天数。View Code 1 #include<stdio.h> 2 int main() 3 { 4 float i,h,u,d,f,a,b; 5 int day; 6 scanf("%f%f%f%f",&h,&u,&d,&f); 7 while(h) 8 { 9 day=1;10
阅读全文
摘要:小希的迷宫View Code 1 //杭电1272 2 #include <stdio.h> 3 #include <string.h> 4 5 #define N 100005 6 int father[N],leftt[N],rightt[N]; 7 int find(int x) 8 { 9 if(father[x]==-1)10 return x;11 else12 return find(father[x]);13 }14 void unionset(int x,int y)15 {16 father[y]=x;17 }18 int main(...
阅读全文
摘要:f(x)=5*x^13+13*x^5+k*a*x=x(5*x^12+13*x^4+k*a),这个函数的形式直接就是费马小定理的形式费马小定理是数论中的一个重要定理,其内容为: 假如p是质数,且(a,p)=1,那么 a^(p-1) ≡1(mod p) 假如p是质数,且a,p互质,那么 a的(p-1)次方除以p的余数恒等于1对f(x)=x(5*x^12+13*x^4+k*a)用此定理分析:(1)如果x是65的倍数,那么已经符合65整除f(x)(2)如果x是5的倍数,只要5*x^12+13*x^4+k*a被13整除即可,去掉13的倍数13*x^4,也即5*x^12+k*a被13整除,由费马小定理,5
阅读全文
摘要:View Code 1 //杭电1028Ignatius and the Princess III 2 /* 3 4 = 4; 4 4 = 3 + 1; 5 4 = 2 + 2; 6 4 = 2 + 1 + 1; 7 4 = 1 + 1 + 1 + 1; 8 9 Sample Input10 411 1012 2013 14 15 Sample Output16 517 4218 62719 */20 #include<stdio.h>21 int num[121][121] = {0};22 int q(int m,int n)23 {24 if(m == 0)m =...
阅读全文
摘要:最小公倍数算法View Code 1 View Code 2 /*两个数的最小公倍数*/ 3 #include<stdio.h> 4 int main() 5 { 6 int t,x,y,r,p,b; 7 scanf("%d",&t); 8 while(t--) 9 {10 scanf("%d%d",&x,&y);11 if(x==0||y==0)12 {13 b=0;break;14 }15 if(x<y)16 {17 p=x;x=y;y=p;18 }19 20 b=x;21 while(...
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 int main() 5 { 6 char s[10000]; 7 int i,num,b; 8 while(scanf("%s",s)!=-1) 9 {10 b=atoi(s);11 if(b==0)12 break;13 num=0;14 for(i=0;s[i]!='\0';i++)15 16 ...
阅读全文
摘要:将N转化成R进制View Code 1 /*进制转换 2 将一个数n装化成R进制(2<=R<=16) 3 4 Sample Input 5 7 2 6 23 12 7 -4 3 8 9 Sample output10 11111 1B12 -1113 */14 #include<stdio.h>15 int main()16 {17 int n,r,i,k,t;18 char f[1000],g[18]={"0123456789ABCDEF"};19 while(scanf("%d%d",&n,&r)!=-1)20
阅读全文
摘要:View Code 1 /*填词 2 3 输入m行n列的字符,再输入p行的字符串,找出m*n在p中没有出现的字符,并把这些字符按字母表输出。 4 5 Sample Input 6 3 3 2 7 EBG 8 GEE 9 EGE10 11 BEG12 GEE13 14 Sample Output15 16 EEG17 */18 #include<stdio.h>19 int main()20 {21 int c[26];22 int m,n,p,i,j,k;23 char str1[100],str2[100];24 scanf("%d%d%d",&m,.
阅读全文
摘要:n个数每m个求和取平均值View Code 1 //偶数求和 2 /* 3 有一长度为n的数列(n<100),该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。 4 Sample Input 5 3 2 6 4 2 7 Sample output 8 3 6 9 3 710 */11 #include<stdio.h>12 int main()13 {14 int i,n,m,k;15 while(scanf("%d%d",&n,&m)!=-1)16 {17 k=0;18
阅读全文
摘要:数组法和公式法View Code 1 //杨辉三角 2 /* 3 5 4 5 1 6 1 1 7 1 2 1 8 1 3 3 1 9 1 4 6 4 110 11 */12 //方法一:数组法13 #include<stdio.h>14 int main()15 {16 int i,j,a[32][32],n;17 for(i=1;i<32;i++)//将第一列以及对角线上的元素置118 {19 a[i][1]=1;20 a[i][i]=1;21 }22 for(i=2;i<32;i++)23 {24 ...
阅读全文
摘要:View Code 1 //保留整数 2 #include<stdio.h> 3 #include<string.h> 4 int main() 5 { 6 char s1[100],s2[100]; 7 int i,j,k,c; 8 gets(s1); 9 c=0;k=0;j=0;10 for(i=0;s1[i]!='\0';i++)11 {12 if(s1[i]>='0'&&s1[i]<='9')13 {14 s2[k++]=s1[i];15 c=0;16 }17 if((s1[i]<
阅读全文
摘要:View Code 1 //等值数目 2 #include<stdio.h> 3 int main() 4 { 5 int f[1010],g[1010],i,j,k,m,n,c; 6 scanf("%d%d",&m,&n); 7 c=0;k=1; 8 for(i=1;i<=m;i++) 9 scanf("%d",&f[i]);10 for(j=1;j<=n;j++)11 scanf("%d",&g[j]);12 for(i=1;i<=m;i++)13 for(j=k;j&
阅读全文
摘要:View Code 1 //打印菱形 2 #include<stdio.h> 3 #include<math.h> 4 int main() 5 { 6 int h,n,i,j; 7 while(scanf("%d",&h)!=-1) 8 { 9 n=(h+1)/2;10 11 for(i=1;i<=h;i++)12 {13 for(j=1;j<=abs(n-i);j++)14 printf(" ");15 for(j=(h-abs(n-i));j>=abs(n-i)+1;j--)16 printf(&q
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 char s1[1000],s2[100]; 6 int c,k,i,l; 7 gets(s1); 8 l=strlen(s1); 9 s1[l]=' ';10 c=0;k=0;11 for(i=0;i<=l;i++)12 {13 if(s1[i]!=' ')14 {15 s2[k++]=s1[i];16 c=0;17 }18 if(s1[i]==' ')19 {20 s2[k]
阅读全文