Thực hành tin học đại cương bài 6
Bài 6.1
#include<stdio.h>
#include<string.h>
int main(){
char s[99],t[99],resstr[30];
int restime[30];
scanf("%s%s",s,t);
int reslen = 0;
for(char i = 'a';i<'z'+1;i++){
int hook=0;
for(int j=0;j<strlen(s);j++){
if(i==s[j]){
hook+=1;
}
}
for(int k=0;k<strlen(t);k++){
if(i==t[k]){
hook+=1;
}
}
if(hook>0){
resstr[reslen]=i;
restime[reslen]=hook;
reslen++;
}
}
printf("%s",resstr);
for(int i=0;i<reslen;i++){
printf("\n%d",restime[i]);
}
return 0;
}
Bài 6.2
#include <stdio.h>
#include <string.h>
int main(){
char titleIn[1000]; //Noi luu chuoi nhap vao
int titleStart=0;
fgets(titleIn,1000,stdin); //lay chuoi vao titleIn tu stdin
for(int i=0;i<strlen(titleIn)-1;i++){
if(titleIn[i]!=' '){ //khi tim thay gia tri khac ' ' dau tien thi bat dau in chuoi
titleStart=1; //muc dich de loai cac dau ' ' o dau tien
}
if(titleStart==1){
if(titleIn[i]==' '&&titleIn[i+1]==' '){ // neu tim thay dau ' ' va dang sau no lai co dau ' '
// thi bo qua khong in
continue;
}else if(titleIn[i]==' '&&i==(strlen(titleIn)-2)){ //neu dau cach o cuoi cung thi bo qua khong in
continue;
}else {
printf("%c",titleIn[i]); //in ra ki tu du dieu kien
}
}
}
return 0;
}
Bài 6.3
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char str[100];
int i, alphabets = 0, digits = 0;
fgets(str, 100, stdin);
for(i=0; i<strlen(str);i++)
{
if(isalpha(str[i]))
alphabets ++;
if(isdigit(str[i]))
digits ++;
}
if(alphabets == digits)
printf("%d",0);
else if (alphabets > digits)
printf("%d",1);
else printf("%d",2);
return 0;
}
Bài 6.4
#include <stdio.h>
#include <string.h>
int ex(int a){
int res=1;
for(int i=0;i<a;i++){
res*=2;
}
return res;
}
int main()
{
char str[100];
int num = 0;
fgets(str, 100, stdin);
for(int i=1; i<strlen(str)-1;i++)
{
if(str[i]!='0'){
num += ex(strlen(str)-i-2);
}
}
if(str[0]!='0')
num -= ex(strlen(str)-0-2);
printf("%d",num);
return 0;
}