针对C语言入门OJ题
(简单)BC130-小乐乐查找数字_牛客题霸_牛客网
参考代码:
//代码一 #include <stdio.h> int main() { int arr[101] = {0}; int n = 0; scanf("%d", &n); int i = 0; //n个数字的读取 for(i=0; i<n; i++) { int m = 0; scanf("%d", &m); arr[m]++; } //读取x int x = 0; scanf("%d", &x); printf("%d", arr[x]); return 0; }
//代码二 #include <stdio.h> int main() { int arr[101] = {0}; int n = 0; int x = 0; int count = 0; scanf("%d", &n); int i = 0; //n个数字的读取 for(i=0; i<n; i++) { scanf("%d", &arr[i]); } //读取x scanf("%d", &x); for(i=0; i<n; i++) { if(arr[i] == x) { count++; } } printf("%d\n", count); return 0; }
(简单)BC131-KiKi学程序设计基础_牛客题霸_牛客网
解析:
要使转义字符不是转义字符。
参考代码:
#include <stdio.h> int main() { printf("printf(\"Hello world!\\n\");\n"); printf("cout << \"Hello world!\" << endl;"); return 0; }
(中等)BC135-KiKi求质数个数_牛客题霸_牛客网
参考代码:
//试除法 #include <stdio.h> int is_prime(int n) { int i = 0; for(i=2; i<n; i++) { //拿2->n-1之间的数字去试除n if(n%i == 0) return 0;//不是素数 } return 1;//是素数 } int main() { int count = 0; int i = 0; for(i=100; i<=999; i++) { //判断i是否为质数 if(is_prime(i) == 1) { count++; } } printf("%d\n", count); return 0; }
(中等)BC136-KiKi去重整数并排序_牛客题霸_牛客网
参考代码:
//方法1 //暴力求解: #include <stdio.h> int main() { int n = 0; int arr[1000]; scanf("%d", &n);//接收n个数字 int i = 0; for (i = 0; i<n; i++) { scanf("%d", &arr[i]); } //排序(冒泡法) - 趟数 for (i = 0; i<n - 1; i++) { int j = 0; for (j = 0; j<n - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } //去重-比较n-1对 for (i = 0; i<n-1; i++) { //把i+1后边的元素往前移动 if (arr[i] == arr[i + 1]) { //把i+1下标往后的数字全部往前覆盖 int k = 0; for (k = i; k<n - 1; k++) { arr[k] = arr[k + 1]; } n--; i--; } } //打印 for (i = 0; i<n; i++) { printf("%d ", arr[i]); } return 0; }
//方法2: int main() { int n = 0; int arr[1001] = {0};//下标有1000 scanf("%d", &n); int i = 0 ; int m = 0; for(i=0; i<n; i++) { scanf("%d", &m); arr[m] = m; } for(i=0; i<1001; i++) { if(arr[i] != 0) printf("%d ", arr[i]); } return 0; }
补:
(简单)BC138-KiKi学结构体和指针_牛客题霸_牛客网
参考代码:
#include <stdio.h> #include <stdlib.h> //链表结点的定义 struct Node { int data;//数据域 struct Node* next;//指针域 }; int main() { int n = 0; struct Node* list = NULL;//指向链表的指针 struct Node* tail = NULL;//指向链表尾部元素的指针 //输入结点 int i = 0; scanf("%d", &n);//输入n //接收n个数字并尾插到链表中 for (i = 0; i<n; i++) { int data = 0; scanf("%d", &data); struct Node *pn = (struct Node*)malloc(sizeof(struct Node)); pn->data = data; pn->next = NULL; //插入第一个元素 if (list == NULL) { list = pn; tail = list; } else { tail->next = pn; tail = pn; } } //接收要删除的元素 int del = 0; scanf("%d", &del); //删除链表中指定的元素 struct Node *cur = list; struct Node *prev = NULL; while (cur) { //找到了要删除的元素 if (cur->data == del) { struct Node* pd = cur; if (cur == list) { //如果删除的是第一个结点 list = list->next; cur = list; } else { //删除的不是第一个结点 prev->next = cur->next; cur = prev->next; } n--; free(pd); } //找不到 else { prev = cur; cur = cur->next; } } cur = list; printf("%d\n", n);//打印 while (cur) { printf("%d ", cur->data); cur = cur->next; } //释放链表 cur = list; //struct Node* del = NULL; while (cur) { del = cur; cur = cur->next; free(del); } //list = NULL; return 0; }
补:
(入门)BC139-KiKi定义电子日历类_牛客题霸_牛客网
参考代码:
#include <iostream> using namespace std; class TDate { private: int Year;//年 int Month;//月 int Day;//日 public: TDate(int year=0, int month=0, int day = 0) { Year = year; Month = month; Day = day; } void Print() { cout<<Day<<'/'<<Month<<'/'<<Year<<endl; } }; int main() { int year = 0; int month = 0; int day = 0; //输入 cin>>year>>month>>day; TDate d(year, month, day); //输出 d.Print(); return 0; }