题目内容:
用结构体定义时钟类型,编程从键盘任意输入两个时间(例如4时55分和1时25分),计算并输出这两个时间之间的间隔。要求不输出时间差的负号。结构体类型定义如下:
#include
#include
typedef struct clock
{
int hour;
int minute;
int second;
}CLOCK;
CLOCK CalculateTime(CLOCK t1, CLOCK t2);
int main(void)
{
CLOCK t1,t2,t3;
printf( "Input time one:(hour,minute):");
scanf("%d,%d",&t1.hour, &t1.minute);
printf( "Input time two: (hour,minute):");
scanf("%d,%d",&t2.hour, &t2.minute);
t3 = CalculateTime(t1,t2);
printf("%dhour,%dminute\n",t3.hour,t3.minute);
return 0;
}
CLOCK CalculateTime(CLOCK t1, CLOCK t2)
{
CLOCK t3;
int min1, min2;
min1 = t1.hour * 60 + t1.minute;
min2 = t2.hour * 60 + t2.minute;
int minute = abs(min1 - min2);
t3.hour = minute/60;
t3.minute = minute - t3.hour*60;
t3.second = 0;
return t3;
}
//自己写吧兄弟
题目内容:
现在要评选最牛群主,已知有3名最牛群主的候选人(分别是tom,jack和rose),有不超过1000人参与投票,最后要通过投票评选出一名最牛群主,从键盘输入每位参与投票的人的投票结果,即其投票的候选人的名字,请你编程统计并输出每位候选人的得票数,以及得票数最多的候选人的名字。候选人的名字中间不允许出现空格,并且必须小写。若候选人名字输入错误,则按废票处理。
#include
#include
#define N 10
int main(void)
{
int n, i, pos = 0,max;
int num[3] = {0};
printf("Input the number of electorates:");
scanf("%d",&n);
char vote[n][N];
char name[3][5] = {"tom","jack","rose"};
char (*p)[N] = vote;
char (*q)[5] = name;
for(i = 0; i < n; i++)
{
printf("Input vote %d:",i+1);
scanf("%s",(p + i));
}
for(i = 0; i < n; i++)
{
for(int j = 0; j < 3; j++)
{
if(strcmp(q[j],p[i]) == 0)
{
num[j]++;
}
}
}
max = num[0];
for(i = 1; i < 3; i++)
{
if(num[i] > max)
{
pos = i;
max = num[i];
}
}
printf("Election results:\n");
for(i = 0; i < 3; i++)
{
printf("%s:%d\n",q[i],num[i]);
}
printf("%s wins\n",q[pos]);
return 0;
}
题目内容:
请输入星期几的第一个字母(不区分大小写)来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母(小写),否则输出“data error”。
#include
#define M 7
#define N 20
int main()
{
char date[M][N] = {"monday\n","tuesday\n","wednesday\n","thursday\n",
"friday\n","saturday\n","sunday\n"};
char ch[2];
char *p = *date;
int flag = 0;
printf("please input the first letter of someday:\n");
scanf(" %c",&ch[0]);
if(ch[0]=='m'||ch[0]=='M'||ch[0]=='w'||ch[0]=='W'||ch[0]=='f'||ch[0]=='F')
{
switch(ch[0])
{
case 'm':
case 'M':
printf("monday\n");
break;
case 'w':
case 'W':
printf("wednesday\n");
break;
case 'f':
case 'F':
printf("friday\n");
break;
default:
break;
}
return 0;
}
else
{
for(int i = 0; i < M; i++)
{
if(ch[0] == p[i*N] || ch[0] == p[i*N] - 32)
flag = 1;
}
if(flag == 0)
{
printf("data error\n");
return 0;
}
else
{
printf("please input second letter:\n");
scanf(" %c",&ch[1]);
for(int i = 0;i < M; i++)
{
for(int j = 0; j < N; j++)
{
if((ch[0] == p[i*N] || ch[0] == p[i*N] - 32) && ch[1] == p[i*N+1])
{
puts(date[i]);
return 0;
}
}
}
}
printf("data error\n");
return 0;
}
}
题目内容:
阿泰和女友小菲用英语短信玩单词接龙游戏。一人先写一个英文单词,然后另一个人回复一个英文单词,要求回复单词的开头有若干个字母和上一个人所写单词的结尾若干个字母相同,重合部分的长度不限。(如阿泰输入happy,小菲可以回复python,重合部分为py。)现在,小菲刚刚回复了阿泰一个单词,阿泰想知道这个单词与自己发过去的单词的重合部分是什么。他们两人都是喜欢写长单词的英语大神,阿泰觉得用肉眼找重合部分实在是太难了,所以请你编写程序来帮他找出重合部分。
#include
#include
#define N 100
int main()
{
char first[N],second[N],temp1[N],temp2[N];
int fir_len, sec_len, i, j;
scanf("%s%s", first, second);
fir_len = strlen(first);
sec_len = strlen(second);
for(i = 0; i < sec_len; i++)
{
memset(temp1, '\0', sizeof(temp1));
memset(temp2, '\0', sizeof(temp2));
strncpy(temp1, second, i + 1);
for(j = fir_len - (i + 1); j <= fir_len - 1; j++)
{
temp2[j - (fir_len - (i + 1))] = first[j];
}
if(strcmp(temp1, temp2) == 0)
{
printf("%s\n",temp1);
break;
}
}
return 0;
}
题目内容:
比较两个分数的大小。人工方式下比较分数大小最常见的方法是:进行分数的通分后比较分子的大小。可以编程模拟手工解决。
具体要求为首先输出(“Input two FENSHU:\n”),然后输入两个分数分子分母的值,格式为("%d/%d,%d/%d"),判断完成后输出("%d/%d<%d/%d\n")或("%d/%d>%d/%d\n")或("%d/%d=%d/%d\n");
#include
int gcd(int a, int b) //求两个参数的最大公约数,在本题中求这个是为了防止整型溢出,扩大输入有效范围
{
int r;
while(b != 0)
{
r = a%b;
a = b;
b = r;
}
return a;
}
int main()
{
int a,b,c,d,fen1,fen2,g;
printf("Input two FENSHU:\n");
scanf("%d/%d,%d/%d", &a, &b, &c, &d);
g = gcd(b,d);
fen1 = a * (d / g);
fen2 = c * (b / g);
if(fen1 > fen2)
printf("%d/%d>%d/%d\n",a,b,c,d);
else if(fen1 == fen2)
printf("%d/%d=%d/%d\n",a,b,c,d);
else
printf("%d/%d<%d/%d\n",a,b,c,d);
return 0;
}
题目内容:
有一天,一位百万富翁遇到一个陌生人,陌生人找他谈一个换钱的计划,陌生人对百万富翁说:“我每天给你10万元,而你第一天只需给我1分钱,第二天我仍给你10万元,你给我2分钱,第三天我仍给你10万元,你给我4分钱……。你每天给我的钱是前一天的两倍,直到满一个月(30天)为止”,百万富翁很高兴,欣然接受了这个契约。请编程计算在这一个月中陌生人总计给百万富翁多少钱,百万富翁总计给陌生人多少钱。程序中浮点数的数据类型均为double。
#include
#include
int main()
{
double richman = 100000, sum = 0;
for(int i = 0; i < 30; i++)
sum += pow(2,i);
printf("to Stranger: %.2f yuan\n",sum);
printf("to Richman: %.2f yuan\n", richman * 30);
return 0;
}
题目内容:
输入一些整数,编程计算并输出其中所有正数的和,输入负数时不累加,继续输入下一个数。输入零时,表示输入数据结束。要求最后统计出累加的项数。
#include
int main(void)
{
int sum = 0, count = 0;
int n;
do
{
printf("Input a number:\n");
scanf("%d", &n);
if(n > 0)
{
sum += n;
count++;
}
}while(n != 0);
printf("sum=%d,count=%d\n", sum, count);
return 0;
}
题目内容:
输出100(n2<=100)以内整数的平方根表,n的值要求从键盘输入,并且满足n2<=100 (即n的平方值在100以内)。
#include
#include
int main(void)
{
int n;
printf("Input n(n<=10):\n");
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
printf("%7d", i);
}
printf("\n");
for(int i = 0; i < n; i++)
{
printf("%d", i);
for(int j = 0; j < n; j++)
{
float add = sqrt(i * 10.0 + j);
printf("%7.3f",add);
}
printf("\n");
}
return 0;
}
题目内容:
按照如下函数原型编写子函数计算正整数a和b的所有公约数。第一次调用,返回最大公约数。以后只要再使用相同参数调用,每次返回下一个小一些的公约数。无公约数时,函数CommonFactors()返回-1,主函数中不输出任何信息。
函数原型: int CommonFactors(int a, int b)
#include
int CommonFactors(int a, int b);
int n = 1;
int main(void)
{
int a, b;
int ret;
printf("Input a and b:\n");
scanf("%d,%d", &a, &b);
ret = CommonFactors(a,b);
if(a > 0 && b > 0)
{
while(ret > 0)
{
printf("Common factor %d is %d\n", n, ret);
n++;
ret = CommonFactors(a,b);
}
}
return 0;
}
int CommonFactors(int a, int b)
{
int i, cnt = 0;
int temp;
if(a < b)
{
temp = a;
a = b;
b = temp;
}
for(i = a; i >= 1; i--)
{
if(a%i == 0 && b%i == 0)
cnt++;
if(n == cnt)
return i;
}
return -1;
}
题目内容:
请编写一个简单的23 根火柴游戏程序,实现人跟计算机玩这个游戏的程序。为了方便程序自动评测,假设计算机移动的火柴数不是随机的,而是将剩余的火柴根数对3求余后再加1来作为计算机每次取走的火柴数。如果剩余的火柴数小于3,则将剩余的火柴数减1作为计算机移走的火柴数。但是计算机不可以不取,剩下的火柴数为1时,必须取走1根火柴。假设游戏规则如下:
1)游戏者开始拥有23根火柴棒;
2)每个游戏者轮流移走1 根、2 根或3 根火柴;
3)谁取走最后一根火柴为失败者。
#include
int main()
{
int remain = 23, n = 0;
int i = 0;
int com = 0;
int player = 0;
printf("Game start!\nNote: the maximum number is 3\n");
while(remain)
{
if(i%2 == 0)
{
if(remain < 3)
{
printf("Please enter the number of matches you are moving:\n");
scanf("%d", &n);
while(n != 1)
{
printf("The number you entered is wrong, please re-enter!\n");
printf("Please enter the number of matches you are moving:\n");
scanf("%d", &n);
}
printf("The number of matches you are moving is:%d\n",n);
remain--;
printf("The number of matches left is:%d\n", remain);
if(remain == 1)
player = 1;
}
else
{
printf("Please enter the number of matches you are moving:\n");
scanf("%d", &n);
while(n < 1 || n > 3)
{
printf("The number you entered is wrong, please re-enter!\n");
printf("Please enter the number of matches you are moving:\n");
scanf("%d", &n);
}
printf("The number of matches you are moving is:%d\n",n);
remain -= n;
printf("The number of matches left is:%d\n", remain);
}
i++;
}
else
{
if(remain < 3)
{
printf("The number of matches that have been moved by the computer is:%d\n", 1);
remain--;
printf("The number of matches left is:%d\n", remain);
if(remain == 1)
com = 1;
}
else
{
printf("The number of matches that have been moved by the computer is:%d\n", remain%3+1);
remain = remain - (remain % 3 + 1);
printf("The number of matches left is:%d\n", remain);
}
i++;
}
}
if(player == 1)
printf("Congratulations! You won!\n");
else
printf("I'm sorry. You lost!\n");
return 0;
}