博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法实现c语言--02
阅读量:6209 次
发布时间:2019-06-21

本文共 9854 字,大约阅读时间需要 32 分钟。

  1. 从键盘上输入字符,将小写字母转换成大写字母。输入ctl + z” 结束
#include
#include
//从键盘上输入字符,将小写字母转换成大写字母。输入“ctl + z” 结束 int main(){ char c; while (fflush(stdin),scanf_s("%c", &c) != EOF) { if (c >= 'a'&&c<='z' ) { c = c - 32; printf("%c", c); continue; } if (c >= 'A'&&c < ='Z') { printf("%c", c); continue; } else printf("error,input agian\n"); } printf("\n"); system("pause"); return 0;}

 

  1. 从键盘上输入字符,(1分别统计一下其中字母数字,其他字符的个数 2将统计的字母,数字,其他字符的个数以柱状图的形式打印。例如 

  5

*****

*****     3

*****   *****     2

*****   *****   *****

*****   *****   *****

 alp     num     oth

 

#include
#include
typedef struct { int n; char *name;}elem;elem alp, num, oth, print[3], tmp;void paixu(elem *print){ if ((print[0].n < print[1].n) == 1) { tmp = print[0]; print[0] = print[1]; print[1] = tmp; } if ((print[0].n < print[2].n) == 1) { tmp = print[0]; print[0] = print[2]; print[2] = tmp; } if ((print[1].n < print[2].n) == 1) { tmp = print[1]; print[1] = print[2]; print[2] = tmp; }}int main(){ char c; int cnt[3] = { 0 }; int i = 0, j = 0; int maxc; alp.n = 0; num.n = 0; oth.n = 0; alp.name = "alp"; num.name = "num"; oth.name = "oth"; int flag = 1; while (fflush(stdin), scanf_s("%c", &c) != EOF) { if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z') { alp.n++; printf("%c", c); continue; } else if (c >= 48 && c <= 57) { num.n++; printf("%c", c); continue; } else if (c != '\0') { oth.n++; printf("%c", c); } if (c == '\n')break; } oth.n--; print[0] = alp; print[1] = num; print[2] = oth; paixu(print); for (i = 0; i <= print[0].n; i++) { printf("\n"); if (print[0].n - i <= print[0].n) { if (print[0].n - i == print[0].n) { if (print[0].n == print[1].n) { if (print[1].n == print[2].n) printf("%s\t%s\t%s", print[0].name, print[1].name, print[2].name); else printf("%s\t%s", print[0].name, print[1].name); } else printf("%s", print[0].name); continue; } printf("*****\t"); // if (print[0].n - i <= print[1].n) { if (print[0].n - i == print[1].n) { if (print[1].n == print[2].n) printf("%s\t%s", print[1].name, print[2].name); else printf("%s", print[1].name); continue; } printf("*****\t"); / if (print[0].n - i <= print[2].n) { if (print[0].n - i == print[2].n) { printf("%s", print[2].name); continue; } printf("*****\t"); } } } } printf("\n"); printf("%d\t", print[0].n); printf("%d\t", print[1].n); printf("%d\t", print[2].n); printf("\n"); printf("\n"); system("pause"); return 0;}

 

3.进制转换。

(1) 将十进制数转换成二进制数。输入进制数输出对应的二进制数,输入“ctl + z”结束

 

#include
#include
#define N 8void fun(int num, int k){ int arr[N], i; for (i = 0; i
= 0; i--) printf("%d", arr[i]); printf("\n\n\n");}int main(){ int n; while (fflush(stdin), scanf_s("%d", &n) != EOF) { fun(n, 2); } printf("\n"); system("pause"); return 0;}

 

(2)将二进制转换成十进制数。输入进制数输出对应的十进制数输入“ctl + z”结束

#include
#include
#include
int main(){ char *p, s[6]; int n; p = s; gets(p); n = 0; while (*p != '\0') { n = n * 2 + *p - '0'; p++; } printf("%d", n); system("pause"); return 0;}

(3) 将十进制数转换成十六进制数输入十进制数输出对应的十六进制数。输入ctl + z” 结束

 

#include
#include
#define N 8void fun(int num){ int arr[N], i; for (i = 0; i
= 0; i--) switch (arr[i]) { case 10: printf("A"); break; case 11: printf("B"); break; case 12: printf("C"); break; case 13: printf("D"); break; case 14: printf("E"); break; case 15: printf("F"); break; default: printf("%d", arr[i]); } printf("\n\n\n");}int main(){ int n; while (fflush(stdin), scanf_s("%d", &n) != EOF) { fun(n); } printf("\n"); system("pause"); return 0;}

 

(4) 将十六进制数转换成十进制数。输入十六进制数输出对应的十进制数。输入ctl +  z” 结束

 

#include
#include
#include
int main(){ char *p, s[6]; int n; p = s; gets(p); n = 0; while (*p != '\0') { switch (*p) { case '0':n = 0; break; case '1':n = 1; break; case '2':n = 2; break; case '3':n = 3; break; case '4':n = 4; break; case '5':n = 5; break; case '6':n = 6; break; case '7':n = 7; break; case '8':n = 8; break; case '9':n = 9; break; case 'A':n = 10; break; case 'B':n = 11; break; case 'C':n = 12; break; case 'D':n = 13; break; case 'E':n = 14; break; case 'F':n = 15; break; default:break; } n = n * 16 + *p - '0'; p++; } printf("%d", n); system("pause"); return 0;}

 

4.统计一个整数对应的二进制数的1的个数。输入一个整数(可正可负) 输出该整数的二进制包含1的个数ctl+ z” 结束

 

#include
#include
#define N 8void fun(int num, int k){ int arr[N], i, cnt = 0; for (i = 0; i
= 0; i--) { if (arr[i] == 1)cnt++; printf("%d", arr[i]); } printf("共有%d个1",cnt); printf("\n\n\n");}int main(){ int n; while (fflush(stdin), scanf_s("%d", &n) != EOF) { fun(n, 2); } printf("\n"); system("pause"); return 0;}

 

5.有101个整数,其中有50个数出现了两次,1个数出现了一次, 找出出现了一次的那个数。

#include
#include
#include
int main(){ int a[101]; int i, ret = 0; for (i = 0; i < 50; i++) a[i] = i + 1; for (i = 51; i <= 100; i++) a[i] = i - 50; scanf_s("%d", &a[50]); for (i = 0; i <= 100; ++i) { ret = ret ^ a[i]; } printf("%d\n", ret); printf("\n"); system("pause"); return 0;}

6.

1输入年月日输出该日期是当年的第几天。

 

#include
#include
int main(){ int a[12] = {
31,28,31,30,31,30,31,31,30,31,30,31 }; int year, mon, day,sum=0; scanf_s("%d%d%d", &year, &mon, &day); for (int i = 0; i < mon - 1; i++)sum += a[i]; sum += day; if (mon > 2)sum =sum+(year % 4 && year % 100 || year % 400); printf("%d", sum); printf("\n"); system("pause"); return 0;};

 

(2)输入两个日期( 日) 输出这两个日期之间差多少天

 

 

#include
#include
int isleap(int year){ return year % 4==0 && year % 100==0 || year % 400==0;}int main(){ int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; int year1 = 0, mon1 = 0, day1 = 0, year2 = 0, mon2 = 0, day2 = 0, sum1 = 0, sum2 = 0, sum = 0, gap = 0; scanf_s("%d %d %d", &year1, &mon1, &day1); scanf_s("%d %d %d", &year2, &mon2, &day2); for (int i = 0; i < mon1 - 1; i++)sum1 += a[i]; sum1 += day1; if (mon1 > 2)sum1 = sum1 + isleap(year1); printf("%d天", sum1); for (int i = 0; i < mon2 - 1; i++)sum2 += a[i]; sum2 += day2; if (mon2 > 2)sum2 = sum2 + isleap(year2); printf("%d天", sum2); gap = abs(year2 - year1); sum = sum2 - sum1 + gap * 365 + gap / 4 ; printf("%d天", sum); printf("\n"); system("pause"); return 0;}

 

(3)输入一个日期,输出该日期是星期几。

 

 

#include
#include
int isleap(int year){ return year % 4==0 && year % 100==0 || year % 400==0;}int main(){ int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; int year1 = 2018, mon1 = 3, day1 = 4, year2 =0 , mon2 = 0, day2 = 0, sum1 = 0, sum2 = 0, sum = 0, gap = 0; int week = 0; scanf_s("%d %d %d", &year2, &mon2, &day2); for (int i = 0; i < mon1 - 1; i++)sum1 += a[i]; sum1 += day1; if (mon1 > 2)sum1 = sum1 + isleap(year1); for (int i = 0; i < mon2 - 1; i++)sum2 += a[i]; sum2 += day2; if (mon2 > 2)sum2 = sum2 + isleap(year2); gap = abs(year1 - year2); sum = sum2 + gap * 365 + gap / 4 - sum1; week = sum; while (week /7!=0) { week %= 7; } if(week==0||week==7) printf("星期日", week); else printf("星期%d", week); printf("\n"); system("pause"); return 0;}

 

4.输入 一个日期 一个整数 n输出从该日期经过n天以后日期。

 -> date -> tomorrow of the date

 

 

#include
#include
int isleap(int year){ return year % 4 == 0 && year % 100 == 0 || year % 400 == 0;}int main(){ int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; int year1 = 2018, mon1 = 3, day1 = 5, year2 = 0, mon2 = 0, day2 = 0, sum1 = 0, sum2 = 0, sum = 0, gap = 1, days = 0; int week = 0; int i; scanf_s("%d", &days); for (i = 0; i < mon1 - 1; i++)sum1 += a[i]; sum1 += day1; if (mon1 > 2)sum1 = sum1 + isleap(year1); sum = sum1 + days; year2 = year1; while (sum > 365 + isleap(year2)) { year2 = year2 + gap; sum -= 365 - isleap(year2); } ; if (sum < 365 + isleap(year2)) { for (i = 0; sum > a[i]; i++) { sum -= a[i]; } i++; mon2 = i; day2 = sum; printf("%d年%d月%d日", year2, mon2, day2); } printf("\n"); system("pause"); return 0;}

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/cthon/p/8885999.html

你可能感兴趣的文章
canvas编写的dota版连连看 demo放出
查看>>
安装部署MONGODB分片集群
查看>>
为mysql添加用户和修改密码
查看>>
使用ApexSQL Log 分析数据库在线日志及数据库备份
查看>>
DNS的转发与委派
查看>>
老男孩为网友工作疑难问题解答一例
查看>>
职场思想分享005 | 别让背后抱怨说别人坏话成为聊天习惯
查看>>
火烧服务器,你准备好没有?
查看>>
20145328《信息安全系统设计基础》第12周学习总结
查看>>
SUSE 服务启动概念
查看>>
秒开缓存系统支持的硬件阵列卡
查看>>
C# 环境
查看>>
WinXP、Win7脚本自动加域及用户资料迁移
查看>>
路由表基本知识
查看>>
2011年9月1日:一个值得纪念的日子
查看>>
Always on 孤立用户问题
查看>>
DNS域名服务之:windows server 2008活动目录中的DNS
查看>>
注册表方式开启或关闭本地输入法的解决方案
查看>>
AOP的XML实现方式
查看>>
Office技巧之二 EXCEL设置勾选框
查看>>