复制字符
#include<stdio.h>
void main()
{
int i=0;
char str1[100];
char str2[100];
gets(str1);
gets(str2);
while(str2[i]!=0)
{str1[i]=str2[i];
i++;}
str1[i]=’\0’;
printf("%s\n",str1);
}
比较字符
#include <stdio.h>
int main()
{
int s=0,point=0;
char s1[100];
char s2[100];
gets(s1);
gets(s2);
int i;
for(i=0;s1[i]==s2[i];i++)
{
point++;
}
s = (int)(s1[point]-s2[point]);
printf("%d\n",s);
return 0;
}
译成原文
#include<stdio.h>
void main()
{
int i;
char a[100];
gets(a);
for (i=0;a[i]!=’\0’;i++)
{if(a[i]>=‘a’&&a[i]<=‘z’)
{a[i]=27-a[i]+192;
}
else if(a[i]>=‘A’&&a[i]<=‘Z’)
{a[i]=27-a[i]+128;
}}
printf("%s\n",a);
}
平行四边
#include<stdio.h>
void main()
{
int i;
char a[]={’’,’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’ ‘,’ ‘,’ ‘,’\n’,
’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’ ‘,’ ‘,’\n’,
’ ‘,’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’ ‘,’\n’,
’ ‘,’ ‘,’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’\n’,
’ ‘,’ ‘,’ ‘,’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’’,’ ‘,’*’,’\n’};
for(i=0;i<70;i++)
printf("%c",a[i]);
}
统计字符
#include<stdio.h>
void main()
{
int i,j,a=0,b=0,c=0,d=0,e=0;
char str[3][81];
for(j=0;j<3;j++)
{gets(str[j]);
for(i=0;i<81&&(str[j][i]!=’\0’);i++)
{if(str[j][i]==’ ')
{
d++;
}
else if(str[j][i]>=‘a’&&str[j][i]<=‘z’)
{
b++;
}
else if(str[j][i]>=‘A’&&str[j][i]<=‘Z’)
{
a++;
}
else if(str[j][i]>=‘0’&&str[j][i]<=‘9’)
{
e=++e;
}
else
{
e++;
}}}
printf("%d %d %d %d %d\n",a,b,c,d,e);
}
折半查找
#include<stdio.h>
void main()
{
int a[15],i,n,low,high,mid,flag=0;
for(i=0;i<=14;i++)
{scanf("%d",&a[i]);}
scanf("%d",&n);
low=0;
high=14;
while(low<high)
{mid=(low+high)/2;
if (a[mid]>n)
{high=mid-1;}
if (a[mid]<n)
{low=mid+1;}
if(a[mid]==n)
{flag=1;break;}}
if(flag){printf("%d\n",mid);}
else {printf(“NO”);}}
死亡鞍点
#include <stdio.h>
int main() {
int n, m;
scanf("%d%d", &n, &m);
int a[50][50], temp1[50], temp2[50], i = 0, j = 0, max, min;
for ( i = 0; i < n; i++) {
for ( j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
}
}
for ( i = 0; i < n; i++) {
int max = a[i][0];
for (j = 0; j < m; j++) {
if (a[i][j] > max)
max = a[i][j];
}
temp1[i] = max;
}
for ( j = 0; j < m; j++) {
int min = a[0][j];
for (i = 0; i < n; i++) {
if (a[i][j] < min)
min = a[i][j];
}
temp2[j] = min;
}
int f = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (temp1[i] ==temp2[j]) {
printf("%d %d %d\n", temp1[i], i, j);
f = 1;
}
}
}
if (!f)
printf("NO\n");
return 0;
}
杨辉三角
#include<stdio.h>
void main()
{
int i,j,temp,n;
scanf("%d",&n);
printf(“1\n”);
for(i=2;i<=n;i++,temp=1)
{printf("1 “);
for(j=2;j<i;j++)
{printf(”%d ",temp=temp*(i-j+1)/(j-1));}
printf(“1\n”);
}
}
逆序存放
#include<stdio.h>
void main()
{
int n,i,temp;
int a[50];
scanf("%d",&n);
for(i=0;i<n;i++)
{scanf("%d",&a[i]);
}
for(i=0;i<n/2;i++)
{temp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=temp;}
for(i=0;i<n;i++)
{printf("%d “,a[i]);
}
printf(”\n");
}
插入数组
#include<stdio.h>
void main()
{
int i,n,input;
char a[50];
scanf("%d",&n);
for(i=0;i<n;i++)
{scanf("%d",&a[i]);}
scanf("%d",&input);
for(i=0;i<n;i++)
{
if (a[i]<input)
{
printf("%d “,a[i]);
}else {printf(”%d ",input);break;
}
}
for(;i<n;i++)
printf("%d “,a[i]);
printf(”\n");
}
数组平均
#include<stdio.h>
double matrixAvg(int M,int N,double a[M][N])
{double sum=0;
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
sum+=a[i][j];
return sum/(M*N);
}
int main()
{int M,N;
scanf("%d%d",&M,&N);
double aver,a[M][N];
int i,j;
for(i=0;i<M;i++)
for(j=0;j<N;j++)
scanf("%lf",&a[i][j]);
aver=matrixAvg(M,N,a);
printf("%.2f\n",aver);
return 0;
}
循环右移
#include<stdio.h>
int main(){
int i,n,m;
scanf("%d%d",&n,&m);
int arr[n];
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
arrayShiftRight(arr,n,m);
for(i=0;i<n;i++)
printf("%d ",arr[i]);
return 0;
}
void arrayShiftRight(int a[ ],int n,int m)
{int i,j;
int b[n];
for(i=0;i<n;i++)
b[i]=a[i];
i=m%n;
int c=0;
for(i;i<n;i++,c++)
a[i]=b[c];
for(j=0;j<c%n;j++,c++)
a[j]=b[c];
}
排序数组
#include<stdio.h>
void sort(int a[],int n)
{
int i,j,max;
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1])
{max=a[j];
a[j]=a[j+1];
a[j+1]=max;}
}
int main()
{
int i, n;
scanf("%d",&n);
int a[n];
for( i=0; i<n; i++ )
scanf("%d", &a[i]);
sort(a, n);
printf(“After sorted the array is:”);
for( i = 0; i < n; i++ )
printf(" %d", a[i]);
printf("\n");
return 0;
}
整数逆序
#include<stdio.h>
int reverse(int n)
{
if(n>0)
{int m = n%10;
int i = n/10;
while (i > 0)
{int a = i%10;
m *= 10;
m += a;
i /= 10;}
return m;}
else{n=-n;
int m = n%10;
int i = n/10;
while (i > 0)
{int a = i%10;
m *= 10;
m += a;
i /= 10;
}m=-m;
return m;
}}
int main()
{int n = 0;
int m = 0;
scanf("%d", &n);
printf("%d\n",reverse(n));
return 0;}
平均函数
#include<stdio.h>
double ave(double x[],int N)
{
int i;
double sum=0,aver;
for(i=0;i<N;i++)
{
sum+=x[i];
}
aver=sum/N;
return aver;
}
int main(void){
int N;
scanf("%d",&N);
double x[N],p;
int i;
for(i=0;i<N;i++)
scanf("%lf",&x[i]);
p=ave(x,N);
printf(“ave=%.2f\n”,p);
return 0;
}
复数之积
#include<stdio.h>
double result_real, result_imag;
int main(void)
{
double imag1, imag2, real1, real2;
scanf("%lf %lf", &real1, &imag1);
scanf("%lf %lf", &real2, &imag2);
complex_prod(real1, imag1, real2, imag2);
printf("product of complex is (%f)+(%f)i\n", result_real, result_imag);
return 0;
}
void complex_prod( double x1, double y1, double x2, double y2 )
{
result_real=(x1x2-y1y2);
result_imag=(x1y2+x2y1);
}
两位平方
#include<stdio.h>
#include<math.h>
int main()
{
int number;
scanf("%d",&number);
printf(“count=%d\n”,search(number));
return 0;
}
int search( int n )
{
int i=0,count=0;
int a,b,c;
for(i=101;i<=n;i++)
{double digit=sqrt(i);
a=i/100;
b=i/10%10;
c=i%10;
if((digit==(int)digit)&&(ab||bc||a==c))
count+=1;}
return count;
}
斐波那契
#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
printf("%d\n", f(n));
return 0;
}
int f( int n )
{
int a=0,b=1,c,i;
if(n0){return 0;}
else if(n1){return 1;}
else
for(i=2;i<=n;i++)
{c=a+b;
a=b;
b=c;}
return c;
}
非负阶乘
#include<stdio.h>
int main()
{
int N, NF;
scanf("%d", &N);
NF = Factorial(N);
if (NF)
printf("%d! = %d\n", N, NF);
else
printf(“Invalid input\n”);
return 0;
}
int Factorial( const int N )
{int num=1,i;
if(N<=0){return 0;}
else {for(i=1;i<=N;i++)
{num*=i;}
return num;}
}
整数奇偶
#include<stdio.h>
int main(){
int n;
scanf("%d", &n);
if (even(n))
printf("%d is even.\n", n);
else
printf("%d is odd.\n", n);
return 0;
}
int even( int n )
{
if(n%2==0){return 1;}
else{return 0;}
}
回文字符
#include<stdio.h>
#include<string.h>
#define true 1
#define false 0
int main(){
char s[100];
gets(s);
if(isPalindromicString(s))
printf("Yes");
else
printf("No");
return 0;
}
int isPalindromicString(char s[ ])
{int i,n;
n=strlen(s);
for(i=0;i<n/2;i++)
{if(s[i]!=s[n-i-1])return false;break;}
return true;
}
清华大学
#include <stdio.h>
#include <string.h>
int mod(int x){
return x%20123;
}
int Sumls(char str,int version)
{
int length=strlen(str),curPos=length-1,power=0,iFactor=1;
int iCount=0;
while(curPos>=0)
{
int iLowerNum=0,iCurNum=0,iHigherNum=0;
for(int i=curPos+1;i<length;i++){
iLowerNum=mod(mod(iLowerNum10)+str[i]-‘0’);
}
iCurNum=str[curPos]-‘0’;
for(int i=0;i<curPos;i++){
iHigherNum=mod(mod(iHigherNum10)+str[i]-‘0’);
}
if(iCurNum<version){
iCount=mod(iCount+iHigherNumiFactor);
}
else if(iCurNum==version){
iCount=mod(iCount+iHigherNumiFactor+iLowerNum+1);
}
else{
iHigherNum++;
iCount=mod(iCount+iHigherNumiFactor);
}
–curPos;
iFactor=mod(iFactor*10);
}
return iCount;
}
int main()
{
char str[103];
while(~scanf("%s",str)){
int res1=Sumls(str,1);
int res2=Sumls(str,2);
int res=res1+res2;
printf("%d\n",mod(res));
}
return 0;
}
定位子串
#include<stdio.h>
#include<string.h>
#include<math.h>
#define mod 1000000008
#define pi acos(-1.0)
typedef struct
{
char s[1000];
int len;
}SString;
int Index (SString S,SString T)
{
int pos=0;
int i=0,j=0;
while (i<S.len && j<T.len)
{
if (S.s[i] == T.s[j])
{
printf("%c",S.s[i]);
++i;++j;
}
else
{
printf("%c",S.s[i]);
j=0;
i = ++pos;
}
}
putchar('\n');
if (j==T.len)
return pos;
else
return -1;
}
int main()
{
SString S,T;
int pos;
int n,m,j,k,i;
while (~scanf("%s",S.s))
{
scanf("%s",T.s);
S.len = strlen(S.s);
T.len = strlen(T.s);
int ans = Index(S,T) +1 ;
printf("%d\n",ans);
}
return 0;
}
分段函数
#include<stdio.h>
double fun(double result,double x)
{
if(x>=5)
{result=xx+4x-2;}
else if(x>=0&&x<5)
{result=3*x+2;}
else
{result=0;}
return(result);
}
void main()
{
double x,result;
double fun(result,x);
scanf("%lf",&x);
result=fun(result,x);
printf(“f(%.1lf) = %.1lf”,x,result);
}