1.通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。
#include "stdio.h" #include "string.h" void string_filter(char *s_in, int len, char *s_out); int main() { char s[100],out[100]; int len; gets(s); len = strlen(s); printf("in: %s\n",s); string_filter(s,len,out); printf("out: %s\n",out); } void string_filter(char *s_in, int len, char *s_out) { unsigned int i,n; unsigned int js; memset(s_out,0,len); s_out[0] = *s_in++; n = 1; while(*s_in) { for(i = 0; i < n; i++) { if(*s_in != s_out[i]) { js++; } } if(js == n) { s_out[n] = *s_in; n++; } js = 0; s_in++; } s_out[n] = 0; }
2.三天打鱼2天晒网
#include "stdio.h" #define Uint32 unsigned int #define Uint8 unsigned char #define WORK_FINSH 0 #define WORK_NET 1 Uint8 month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int get_current_work(Uint32 y, Uint32 m, Uint32 d); void main() { Uint32 y =0,m =0,d =0; Uint8 temp; scanf("%d%d%d",&y,&m,&d); printf("y= %d,m= %d, d= %d\n",y,m,d); temp = get_current_work(y,m,d); if(temp == WORK_FINSH) { printf("今天在打鱼\n"); } else printf("今天在织网\n"); return; } int get_current_work(Uint32 y, Uint32 m, Uint32 d) { Uint32 sum_day; Uint8 temp1,temp2; Uint32 flag_366 =0 ; Uint32 i; Uint8 work_state; for(i = 2000 ; i < y;i=i+4) { if((!(y%4) && (y%100)) || !(y%400)) flag_366++; } sum_day = (y-2000)*365 +flag_366; if((!(y%4) && (y%100)) || !(y%400)) { month[2] = 29; } sum_day = sum_day % 5; for(i = 1; i < m;i++) { sum_day += month[i]; } sum_day += d; sum_day = sum_day%5; if( sum_day>0 && sum_day <4) work_state = WORK_FINSH; else work_state = WORK_NET; return work_state; }
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
#include <stdio.h> #include <string.h> void stringZip(const char *pIn, long len, char *pOut); int main() { char in_s[100]; char out_s[100]; gets(in_s); printf("%s\n",in_s); stringZip(in_s, strlen(in_s), out_s); printf("%s\n",out_s); return 0; } void stringZip(const char *pIn, long len, char *pOut) { int i; int js = 0; int n = 0; char temp; for(i = 0; i < len; ) { temp = pIn[i]; while(temp == pIn[i]) { js++; i++; } if(js !=1) { pOut[n] = js+0x30; pOut[n+1] = temp; n += 2; } else pOut[n++] = temp; js = 0; } pOut[n] = '\0'; }
整数化为2进制数,32位长度。然后逆序输出
#include <stdio.h>
#include <string.h>
int main()
{
int m;
int i = 0;
char bin_32[33];
memset(bin_32,0x30,32);
scanf_s("%d",&m);
while(m/2 != 0)
{
bin_32[31-i] = m % 2 + 0x30;
m /= 2;
i++;
}
bin_32[31-i] = m%2 + 0x30;
bin_32[32] = 0;
printf("%s\n",bin_32);
}
#include <stdio.h>
void int_convert_char(int v,char *str);
int main(int argc,char *argv[])
{
int value;
char str[100];
scanf("%d",&value);
printf("输入%d\n",value);
if(value < 0)
{
str[0] = '-';
value = 0-value;
int_convert_char(value,str+1);
}
else
{
int_convert_char(value,str);
}
printf("输出%s\n",str);
}
void int_convert_char(int v,char *str)
{
int temp = 0;
int i = 1;
while(1){
if(v%10 == 0) //delete last 0
{
v /= 10;
}
else
break;
}
str[0] = 0x30 + v%10;
v /= 10;
do{
temp = 0x30 + v%10;
if(temp != str[i-1]){
str[i] = temp;
i++;
}
v = v/10;
}while(v);
str[i] ='\0'; // add string ending
}
输入两行字符串正整数,第一行是被减数,第二行是减数,输出第一行减去第二行的结果。
备注:1、两个整数都是正整数,被减数大于减数
示例:
输入:1000000000000001
1
输出:1000000000000000
#include <stdio.h> #include <string.h> void sub_alg(char * max, int len0, char * min, int len1); int main(int argc, char *argv[]) { char str_max[100]; char str_min[100]; int len0,len1; gets(str_max); gets(str_min); len0 = strlen(str_max); len1 = strlen(str_min); printf("输入:%s\n",str_max); printf("%s\n",str_min); sub_alg(str_max, len0, str_min, len1); printf("输出:%s\n",str_max); } void sub_alg(char * max, int len0, char * min, int len1) { int i = 1; int j ; int loop = len1; char *p = max; while(loop--)//每一位想减的次数 { if(min[len1-i] > max[len0-i])//被减数小 { j = i+1; max[len0-i] = max[len0-i] + 10 - min[len1-i] + 0x30; while(max[len0-j] == 0x30) { max[len0-j] = 0x30 + 9; j++; } max[len0-j] = max[len0-j] - 1 ; } else //被减数大 { max[len0-i] = max[len0-i] - min[len1-i] + 0x30; } i++; } while(1)//去除前面的0 { if(*p == 0x30) { p++; } else { strcpy(max,p); break; } } }