1.输入一个长整型的数,从低位起取出奇数位组成一个新的数输出。
#include<stdio.h>
#include<string.h>
char num[100000];
int i,j,l;
void main()
{
while(scanf("%s",&num)!=EOF)
{
getchar();
l=strlen(num);
if(l%2==1)
for(i=0;i<l;i+=2)
printf("%c",num[i]);
else
for(i=1;i<l;i+=2)
printf("%c",num[i]);
printf("\n");
}
}
这是用字符串的方法。
#include<stdio.h>
long int n,k,res;
int i;
void main()
{
while(scanf("%ld",&n)!=EOF)
{
k=1;
res=0;
while(n)
{
res+=n%10*k;
n/=100;
k*=10;
}
printf("%ld\n",res);
}
}
这是直接输入是数字的方法。
2.输入 n 个字符串,将它们按字母由小到大的顺序排列并输出。
#include<stdio.h>
char c[10000];
int n,i,j;
void swap(char *m,char *n)
{
char temp;
temp=*m;
*m=*n;
*n=temp;
}
void bubblesort(char *k,int l)
{
for(i=0;i<l;i++)
for(j=0;j<l-i-1;j++)
if(k[j]>k[j+1])
swap(&k[j],&k[j+1]);
}
void main()
{
while(scanf("%d",&n)!=EOF)
{
getchar();
for(i=0;i<n;i++)
scanf("%c",&c[i]);
bubblesort(&c,n);
for(i=0;i<n;i++)
printf("%c",c[i]);
printf("\n");
}
}
题意不是特别清楚,我默认为是输入含有n个字符的字符串,然后按照ascii码表默认升序。这种方法就是直接排序。