NOIP(八)

 1 #include <fstream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 /**
 7 输入: 
 8     4
 9     Kitty 80
10     Hanmeimei 90
11     Joey 92
12     Tim 28
13 输出:
14     Joey 92
15     Hanmeimei 90
16     Kitty 80
17     Tim 28 
18 */
19 ifstream cin("cjphin.txt");
20 ofstream cout("cjphout.txt");
21 //结构体
22 struct student{
23     string name;
24     int score;
25 }s[21]; 
26 //排序规则
27 bool comp(student a,student b){
28     if(a.score > b.score){
29         return true;
30     }
31     if((a.score == b.score) && (a.name < b.name)){
32         return true;
33     }
34     return false;
35 } 
36 int main(){
37     int n;
38     //输入 
39     cin >> n;
40     for(int i = 0;i < n;i++){
41         //将数据存储到结构体中
42         cin >> s[i].name >> s[i].score;
43     }
44     //算法
45     sort(s,s + n,comp);
46     //输出 
47     for(int i = 0;i < n;i++){
48         //输出到文件中 
49         cout << s[i].name << " " << s[i].score << endl;
50     }
51     return 0;
52 } 
 1 #include <fstream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 /**
 7 病人看病 
 8 输入: 
 9     5
10     021075 40
11     004003 15
12     010158 67
13     021033 75
14     102012 30
15 输出:
16     021033
17     010158
18     021075
19     004003
20     102012
21 */
22 ifstream cin("brkbin.txt");
23 ofstream cout("brkbout.txt");
24 //结构体
25 struct bingren{
26     string id;
27     int age;
28     bool theOld;
29 }s[11]; 
30 //排序规则
31 //const define常量 
32 bool comp(bingren a,bingren b){
33     //老年人比非老年人先排队
34     if(a.theOld == true && b.theOld == false){
35         return true;
36     }
37     //老年人排序
38     if(a.theOld && b.theOld && a.age > b.age){
39         return true;
40     }
41     return false;
42 } 
43 int main(){
44     int n;
45     //输入 
46     cin >> n;
47     for(int i = 0;i < n;i++){
48         s[i].theOld = false;//非老年人 
49         //将数据存储到结构体中
50         cin >> s[i].id >> s[i].age;
51         if(s[i].age >= 60){
52             s[i].theOld = true;
53         }
54     }
55     //算法
56     sort(s,s + n,comp);
57     //输出 
58     for(int i = 0;i < n;i++){
59         //输出到文件中 
60         cout << s[i].id << endl;
61     }
62     return 0;
63 } 
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 /**
 5     指针练习 
 6 */ 
 7 int main(){
 8     int a,b,s,t,*pa,*pb;
 9     cin >> a >> b;
10     pa = &a;
11     pb = &b;
12     s = *pa + *pb;
13     t = *pa * *pb;
14     cout << s << " " << t << endl; 
15     return 0;
16 } 
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 /**
 5     指针变量的+,-运算 
 6     4
 7     2 1 6 0
 8     
 9     2 1 6 0
10     a b c d
11     
12     逆序输出一个三位数
13     123 
14     321 
15 */ 
16 int a[101],n;
17 int main(){
18     scanf("%d",&n);
19     for(int i = 1;i <= n;i++){
20         scanf("%d",&a[i]);
21     }
22     int *p = &a[1];
23     for(int i = 1;i <= n;i++){
24         printf("%d ",*p);
25         //p++不是值或者地址增加,而是往后遍历一个整数的空间到下一个地址 
26         p++;//指针指向下一个数 
27     }
28     return 0;
29 } 
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 /**
 5     无类型指针 
 6 */ 
 7 int a = 10;
 8 double b = 3.5;
 9 void *p;
10 int main(){
11     p = &a; 
12     cout << *(int*)p << endl;//将指针p的类型转化成int
13     p = &b;
14     cout << *(double*)p << endl; //将指针p的类型转化成double
15     return 0;
16 } 
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 /**
 5     指针数组 
 6 */ 
 7 int main(){
 8     int a[5],i,*pa = a;
 9     //输入 
10     for(i = 0;i < 5;i++){
11         scanf("%d",a + i);
12     }
13     //输出
14     for(i = 0;i < 5;i++){
15         printf("a[%d]=%d\n",i,*(a + i));
16     } 
17     return 0;
18 } 
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 /**
 5     动态数组--连续的指针地址集 
 6 */ 
 7 int n;
 8 int *a;
 9 int main(){
10     scanf("%d",&n);
11     a = new int[n + 1];//向操作系统申请连续的n+1个int类型的空间
12     //输入 
13     for(int i = 1;i <= n;i++){
14         scanf("%d",&a[i]);
15     }
16     //算法 
17     for(int i = 2;i <= n;i++){
18         a[i] += a[i - 1];
19     }
20     //输出
21     for(int i = 1;i <= n;i++){
22         printf("%d",a[i]);
23     } 
24     return 0;
25 } 
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 /**
 5     兔子问题 
 6     
 7     1 1 2 3 5 8 13....n
 8 */ 
 9 int n;
10 int *a;
11 int main(){
12     scanf("%d",&n);
13     a = new int[n + 1];//向操作系统申请连续的n+1个int类型的空间
14     //算法 
15     for(int i = 3;i <= n;i++){
16         a[1] = a[2] = 1;
17         a[i] += a[i - 1] + a[i - 2]; 
18     }
19     //算法 
20     int sum = 0;
21     for(int i = 1;i <= n;i++){
22         sum += a[i];//求第n个月兔子总数 
23     } 
24     //输出
25     printf("%d",sum);
26     return 0;
27 } 
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 /**
 5    指针 
 6 */ 
 7 int *p;//指针 
 8 int n;
 9 int main(){
10     scanf("%d",&n);//&a表示地址 
11     p = new int[n + 1];    //p可以看成一个连续的地址集=数组 
12     for(int i = 1;i <= n;i++){
13         scanf("%d ",&p[i]);
14     }
15     for(int i = 1;i <= n;i++){
16         //NullPointerException
17         printf("%d ",p[i]);
18     }
19     return 0;
20 }
 1 #include <fstream>
 2 #include <cstdio>
 3 using namespace std;
 4 ifstream cin("lpin.txt");
 5 ofstream cout("lpout.txt");
 6 /**
 7    矩阵行列转化问题--使用动态数组和指针 
 8 */
 9 const int LP = 100001;//可能的最大长度 
10 int n,m,k;//n行,m列,k总计有值的个数 
11 int x[LP],y[LP],d[LP];
12 int c[LP];
13 int *a[LP];
14 int main(){
15     cin >> n >> m >> k;
16      for(int i = 1;i <= k;i++){
17          cin >> x[i] >> y[i] >> d[i];
18          c[y[i]]++;
19     }
20     //申请数组空间 
21     for(int i = 1;i <= m;i++){
22         a[i] = new int[c[i]];
23     }
24     for(int i = 1;i <= k;i++){
25         *a[y[i]] = d[i];
26         a[y[i]]++;//相当于p++ 
27     }
28     for(int i = 1;i <= m;i++){//循环列,列有m个值 
29         a[i] -= c[i];
30         for(int j = 1;j <= c[i];j++,a[i]++){
31             cout << *a[i] << " ";
32         }
33     }
34     return 0;
35 }
 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 /**
 5 输入一个长度最大为100的字符串,以字符数组的方式存储,再将字符串倒序存储并输出。 
 6 */
 7 
 8 /*
 9     交换两个字符的位置 
10 */ 
11 void myswap(char &a,char &b){
12     char t;
13     t = a;
14     a = b;
15     b = t;    
16 }
17 /*
18 */
19 void work(char * str){
20     int len = strlen(str);
21     for(int i = 0;i <= len/2;++i){
22         myswap(str[i],str[len - i - 1]);
23     }
24 } 
25 int main(){
26     char s[110];
27     char * str = s;
28     gets(s);
29     work(str);
30     printf("%s",s);
31     return 0;
32 }
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 /**
 6     倒序输出 
 7 */
 8 int main(){
 9     int n;
10     cin >> n;
11     char a[n + 1];
12     for(int i = 1;i <= n;i++){
13         cin >> a[i];
14     }
15     //指针初始值为数组的最后一项 
16     char *p = &a[n];
17     for(int i = 1;i <= n;i++){
18         cout << *p;
19         p--;
20     }
21     return 0;
22 }
 1 #include <fstream>
 2 #include <cstdio>
 3 using namespace std;
 4 ifstream cin("dgqhin.txt");
 5 ofstream cout("dgqhout.txt");
 6 /**
 7     递归求和 
 8 */
 9 long long sum(int x){
10     if(x != 1){
11         return x + sum(x - 1);
12     }
13 }
14 int main(){
15     int n;
16     cin >> n;
17     cout << sum(n);
18     return 0;
19 }
 1 #include <fstream>
 2 #include <cstdio>
 3 using namespace std;
 4 ifstream cin("zzin.txt");
 5 ofstream cout("zzout.txt");
 6 /**
 7     指针函数 
 8 */
 9 void myswap(int *x,int *y){
10     int t = *x;
11     *x = *y;
12     *y = t;
13 }
14 int main(){
15     int m,n;
16     cin >> m >> n;
17 //    myswap(&m,&n);
18     swap(m,n);
19     cout << m << " " << n;
20     return 0;
21 }
 1 #include <fstream>
 2 #include <cstdio>
 3 using namespace std;
 4 ifstream cin("sxplin.txt");
 5 ofstream cout("sxplout.txt");
 6 /**
 7     三个数升序排列 
 8 */
 9 void myswap(int *x,int *y){
10     int t = *x;
11     *x = *y;
12     *y = t;
13 }
14 void mysort(int *x,int *y,int *z){
15     if(*x > *y){
16         myswap(x,y);
17     }
18     if(*y > *z){
19         myswap(y,z);
20     }
21     if(*x > *z){
22         myswap(x,z);
23     }
24 }
25 int main(){
26     int m,n,k;
27     cin >> m >> n >> k;
28     mysort(&m,&n,&k);
29     cout << m << " " << n << " " << k;
30     return 0;
31 }
 1 #include <fstream>
 2 #include <cstdio>
 3 #include <cmath>
 4 using namespace std;
 5 ifstream cin("functionin.txt");
 6 ofstream cout("functionout.txt");
 7 /**
 8     编写一个函数,用于在一个包含N个整数的数组中找到第一个质数,
 9     若有则返回函数的地址,若无在返回NULL(空指针) 
10     
11     因数=约数:b/a余数为0,a是b的因数或者叫约数
12     质数:一个数除了能被1和他本身整除的数叫质数
13     完全数:除本身以外所有的因数之和等于该数,这样的数叫完全数。 
14 */
15 int n,a[10001];
16 /*
17     是否是质数 
18 */
19 bool isprime(int n){
20     //判断1 
21     if(n < 2){
22         return false;
23     }
24     if(n == 2){
25         return true;
26     }
27     for(int i = 2;i <= sqrt(n);++i){
28         if(n % i == 0){
29             return false;
30         }
31     }
32     return true;
33 }
34 /*
35     寻找质数的函数 
36 */
37 int * find(){
38     for(int i = 1;i <= n;++i){
39         if(isprime(a[i])){
40             return &a[i];//返回存在的质数的地址 
41         }
42     }
43     return NULL;
44 }
45 int main(){
46     cin >> n;
47     for(int i = 1;i <= n;++i){
48         cin >> a[i];
49     }
50     int *p = find();
51     if(p != NULL){
52         cout << p << " " << *p << endl;
53     }else{
54         cout << "找不到质数" << endl;
55     }
56     return 0;
57 }
 1 #include <fstream>
 2 #include <cstdio>
 3 #include <cmath>
 4 using namespace std;
 5 ifstream cin("structin.txt");
 6 ofstream cout("structout.txt");
 7 /**
 8     结构体指针 
 9 */
10 struct student{
11     char name[20];
12     char sex;
13     float score;
14 }s[3];
15 int main(){
16     for(int i = 1;i <= 3;i++){
17         cin >> s[i].name >> s[i].sex >> s[i].score;
18     }
19     cout << "Name   " << "Sex   " << "Score\n";
20     for(int i = 1;i <= 3;i++){
21         cout << s[i].name << "   " << s[i].sex << "   " << s[i].score << endl;
22     }
23     return 0;
24 }

 

posted @ 2019-07-26 17:13  灰灰老师  阅读(140)  评论(0)    收藏  举报