|   | 
              
                95樓                巨大八爪鱼
                2016-1-17 16:36
                                                  
			  【7-3】#include <stdio.h>
 #include <stdlib.h>
 
 void main()
 {
 int n, *a, j, i, dcount = 0;
 scanf("%d", &n);
 a = (int *)calloc(n, sizeof(int));
 j = 0;
 for (i = 0; i < n; i++)
 a[i] = 0;
 for (i = 1; dcount < n - 1; i++)
 {
 if (i > n)
 i = 1; // 人的编号(1~n)
 if (a[i - 1] == 1)
 continue; // 跳过被划掉的人
 
 j++; // 叫的数(1~3)
 if (j == 4)
 j = 1;
 
 if (j == 3) // 如果叫到了3
 {
 a[i - 1] = 1; // 如果叫到了3,踢掉该人
 dcount++;
 }
 }
 for (i = 0; i < n; i++)
 {
 if (a[i] == 0)
 printf("%d\n", i + 1);
 }
 free(a);
 }
 | 
                
          |   | 
              
                96樓                巨大八爪鱼
                2016-1-17 16:37
                                                  
			  从上一个例子可以看出,写的时候最好不要用i,j这样的变量(除非是明确的矩形的行和列),不然很容易犯低级错误             | 
                
          |   | 
              
                97樓                巨大八爪鱼
                2016-1-17 16:46
                                                  
			  【9-4】#include <stdio.h>
 
 char *strcat(char *str1, char *str2)
 {
 char *s = str1;
 while (*str1 != '\0')
 str1++;
 while (*str2 != '\0')
 *str1++ = *str2++;
 *str1 = '\0';
 return s;
 }
 
 void main()
 {
 char s1[30] = "abc";
 char s2[] = "de";
 strcat(s1, s2);
 printf("%s\n", s1);
 }
 
 | 
                
          |   | 
              
                98樓                巨大八爪鱼
                2016-1-17 16:46
                                                  
			                【9-4】 #include <stdio.h> char *strcat(char *str1, char *str2) {...
			  这个应该是【7-4】             | 
                
          |   | 
              
                99樓                巨大八爪鱼
                2016-1-17 16:49
                                                  
			  【7-5】#include <stdio.h>
 
 void swap(int *a, int *b)
 {
 int temp = *a;
 *a = *b;
 *b = temp;
 }
 
 void main()
 {
 int a, b;
 scanf("%d%d", &a, &b);
 swap(&a, &b); // 注意这里也必须要加上&,别忘了
 printf("%d %d\n", a, b);
 }
 
 | 
                
          |   | 
              
                100樓                巨大八爪鱼
                2016-1-17 17:03
                                                  
			  【试题1】#include <stdio.h>
 #include <string.h>
 
 void main()
 {
 char str[100];
 int an, nn, on, i;
 an = nn = on = 0;
 gets(str);
 for (i = 0; str[i] != '\0'; i++)
 {
 if (str[i] >= '0' && str[i] <= '9')
 nn++;
 else if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))
 an++;
 else
 on++;
 }
 printf("Numbers:%d\nLetters:%d\nOthers:%d\n", nn, an, on);
 }
 
 | 
                
          |   | 
              
                101樓                巨大八爪鱼
                2016-1-17 22:13
                                                  
			  【试题8】#include <stdio.h>
 
 void fun(int a[], int m, int n)
 {
 int i;
 int temp;
 for (i = 0; i < n / 2; i++)
 {
 // 一定要用temp交换两边的值,不要只赋值半边
 // 编写程序的时候一定要明确是复制值,还是要交换
 // 如果是交换的话,一定要用temp变量
 temp = *(a + m - 1 + i);
 *(a + m - 1 + i) = *(a + n + m - 2 - i);
 *(a + n + m - 2 - i) = temp;
 }
 }
 
 void main()
 {
 int a[10];
 int i, m, n;
 printf("Input 10 numbers:\n");
 for (i = 0; i < 10; i++)
 scanf("%d", a + i);
 printf("Specify m and n:\n");
 scanf("%d%d", &m, &n);
 fun(a, m, n); // 不要忘记调用函数
 for (i = 0; i < 10; i++)
 printf("%d ", a[i]);
 }
 
 |