数组和字符串
题目号码 | 知识点 |
---|---|
7150 | 字符串按ASC2码由小到大输出 |
7090 | 用字符串接受然后相乘 |
7089 | 各英文小写字母出现的次数 |
7150
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//字符串排序
void sort(char **pp,int n)//**pp等效于*p[]
{
char tmp[100];
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(strcmp(pp[i],pp[j])>0)
{
strcpy(tmp,pp[i]);
strcpy(pp[i],pp[j]);
strcpy(pp[j],tmp);
}
}
void output(char *p[],int n)
{
for(int i=0;i<n;i++)
printf("%s\n",p[i]);
}
int main()
{
void sort1(char **pp,int n);
void output1(char *p[],int n);
char str[50],*cp[20];//cp为char指针数组,
int n;
while(~scanf("%d\n",&n))
{
for(int i=0;i<n;i++)
{
gets(str);
int len=strlen(str);
cp[i]=(char*)malloc(len*sizeof(char));//动态开辟内存
strcpy(cp[i],str);
}
sort(cp,n);
output(cp,n);
}
return 0;
}
7090
#include<stdio.h>
#include<string.h>
#define N 100
char a[N],b[N];
int A[N],B[N],t[N],C[N];
int main()
{
scanf("%s%s",a,b);
int la=strlen(a);
int lb=strlen(b);
for(int i=0;i<la;i++)A[i]=a[la-i-1]-'0';//字符串转数字并逆序
for(int i=0;i<lb;i++)B[i]=b[lb-i-1]-'0';
for(int i=0;i<lb;i++)
for(int j=0;j<la;j++)
C[i+j]+=B[i]*A[j];//先乘起来
for(int i=0;i<=la+lb+1;i++)//同一处理进位
if(C[i]>=10)
{
C[i+1]+=C[i]/10;
C[i]%=10;
}
int lc=N;
while(C[lc]==0&&lc>=1)lc--;//去除前导0
for(int i=lc;i>=0;i--)printf("%d",C[i]);//逆序输出
return 0;
}
7089
#include<stdio.h>
#define N 105
char str[N];
int tj[30];
int main()
{
while(~scanf("%s",str))
{
memset(tj,0,sizeof tj);
for(int i=0;str[i];i++)
if(str[i]>='a'&&str[i]<='z')
tj[str[i]-'a']++;
for(int i=0;i<26;i++)
if(tj[i])printf("%c %d\n",'a'+i,tj[i]);
}
return 0;
}