当前位置:首页 » 编程语言 » c语言编程题求助
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言编程题求助

发布时间: 2022-08-20 18:15:19

‘壹’ c语言编程题求助

感觉你的两个数组用得有点乱,下面做个参考
int cnt,i,j,n;
scanf("%d",&cnt); //测试数据个数

int cases[cnt]; //用于每个case的n值

for(i=0;i<cnt;i++) { //输入每个case的n值
scanf("%d",&cases[i]);
}

for(n=0;n<cnt;n++) { //按测试数据个数循环计算每个case
int sum = 0;
for(j=0;j<=cases[n];j++) { //按每个case的n值求和
sum += j;
}
printf("Case %d:%d\n",n+1,sum);
}

‘贰’ C语言编程题,求助大佬解答

摘要 先应阐明c语言的由来,才能更清楚它的用途。众所周知,c语言是计算机高级语言,用它可以比机器语言和汇编语言更快捷地编写程序,而且可读性更强(即人更容易理解)。其次就是它能实现的功能,语言就是如何使实现计算机能按照人的要求自动地执行,并返回结果,用c语言编程是能够达到这一目的的,如开发大型的UNIX操作系统,计算器应用软件,还用其他生活中的图书管理系统,超市管理系统等。

‘叁’ C语言编程题,求助大神

#include <iostream>

#include <time.h>

unsigned int CalcDayOfMonth(unsigned int year, unsigned int month);

bool IsLeapYear(unsigned int year);

struct Time

{

unsigned int year;

unsigned int month;

unsigned int day;

void setTime(unsigned int y = 1900, unsigned int m = 1, unsigned int d= 1)

{

year = y; month = m;

if (d > CalcDayOfMonth(year, month))

day = CalcDayOfMonth(year, month);

else

day = d;

}

Time operator-(Time& t)

{

Time tmp;

if (t > * this)

{

tmp = t;

t = *this;

*this = tmp;

}

else

tmp = *this;

if (tmp.day >= t.day)

{

tmp.day -= t.day;

}

else

{

tmp.day = CalcDayOfMonth(tmp.year, tmp.month-1) + tmp.day-t.day;

tmp.month--;

}

if (tmp.month >= t.month)

tmp.month -= t.month;

else

{

tmp.month = tmp.month + 12 - t.month;

tmp.year--;

}

tmp.year -= t.year;

Time tmp1 = t;

int d=0;

while (!(*this==tmp1))

{

tmp1.day++;

d++;

if (CalcDayOfMonth(tmp1.year, tmp1.month) < tmp1.day)

{

tmp1.day = 1;

tmp1.month++;


}

if (tmp1.month > 12)

{

tmp1.month = 1;

tmp1.year++;

}

}

printf("累计相差:%d天 ", d);

return tmp;

}

bool operator==(Time& t)

{

bool b=this->day == t.day && this->month == t.month && this->year == t.year;

return b;

}

bool operator>(Time& t)

{

if (this->year > t.year)

return true;

else if (this->year == t.year)

{

if (this->month > t.month)

return true;

else if (this->month == t.month)

{

if (this->day > t.day)

return true;

else

return false;

}

else

return false;

}

else

return false;

}

};

void show(Time t)

{

printf_s("%d年-%u月-%u日 ", t.year, t.month, t.day);

}


bool IsLeapYear(unsigned int year)

{

if ((year % 400 == 0) || (year % 100 != 0) && (year % 4 == 0))

return true;

return false;

}


unsigned int CalcDayOfMonth(unsigned int year, unsigned int month)

{

switch (month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

return 31;

case 4:

case 6:

case 9:

case 11:

return 30;

case 2:

return IsLeapYear(year) ? 29 : 28;

}

return 0;//返回0则表示月份输入不正确

}


int main()

{

Time t1, t2;

printf_s("请输入两个时间(格式:年 月 日): ");

scanf_s("%u %u %u", &t1.year, &t1.month, &t1.day);

scanf_s("%u %u %u", &t2.year, &t2.month, &t2.day);

show(t1 - t2);

}

‘肆’ C语言编程题,求助大佬解答

#include <iostream>
using namespace std;

struct Node
{
int elem;
Node* next;
};

struct List
{
Node* head = NULL;
};

//查找前驱节点
Node* find_pre(List list, int elem)
{
if (!list.head) return NULL;
Node* node = list.head;
while (node->next && node->next->elem < elem) node = node->next;
return node;
}

//插入节点
void ins_elem(List &list, int elem)
{
Node* pre = find_pre(list, elem);
if (!pre)
{
list.head = (Node*)malloc(sizeof(Node)); //头结点暂不存放值
list.head->next = (Node*)malloc(sizeof(Node));
list.head->next->elem = elem;
list.head->next->next = NULL;
return;
}

Node* new_node = (Node*)malloc(sizeof(Node));
new_node->elem = elem;
new_node->next = pre->next;
pre->next = new_node;
}

//删除节点
void del_elem(List &list, int elem)
{
Node* pre = find_pre(list, elem);
if (!pre) return;
while (pre->next->elem == elem)
{
Node* next = pre->next;
pre->next = next->next;
free(next);
}

}

//遍历链表
void traverse(List list)
{
if (!list.head) return;
Node* node = list.head->next;
while (node)
{
cout << node->elem << " ";
node = node->next;
}
cout << endl;
}

//销毁链表
void destory(List &list)
{
if (!list.head) return;
Node* node = list.head->next;
while (node)
{
Node* next = node->next;
free(node);
node = next;
}
}

int main()
{
int arr[5] = { 1, 2, 2, 4, 5 };
List list;//声明链表
for (int i = 0; i < 5; i++)
{
ins_elem(list, arr[i]);
}
traverse(list);
del_elem(list, 2);
traverse(list);
destory(list);
return 0;
}

‘伍’ c语言编程题,求助

‘陆’ [急求助]C语言程序编程题,请高手帮忙解答下!

按照题目要求编写的程序如下(见图)

‘柒’ c语言编程题,求助!

编程代码及结果如图所示。

‘捌’ C语言编程题,求助大神!

#include <stdio.h>

#include <string.h>

#define N 10000

int main()

{

FILE *fp1,*fp2,*fp3;

char str1[N],str2[N];

fp1=fopen("D:\666.txt","r");

fp2=fopen("D:\888.txt","r");

fp3=fopen("D:\999.txt","w");

while(fgets(str1,N,fp1)!=NULL)//将文件中的每一个非空字符储存在数组str1中

while(fgets(str2,N,fp2)!=NULL)//同上

puts(str1);//输出文件的内容

puts(str2);

strcat(str1,str2);//两个字符串连接

fprintf(fp3,"%s",str1);//将连接后的字符串写入第3个文件

fclose(fp1);//关闭文件

fclose(fp2);

fclose(fp3);

return 0;

}