『壹』 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;
}