1 //2023.7.7 2 //PTA 3 #include<iostream> 4 #include <iomanip> 5 #include<cstdio> 6 #include<cmath> 7 #include<cstring> 8 using namespace std; 9 void test01() 10 { 11 string s; 12 int tel[12] = {0}; 13 int arr[11] = {0}; 14 int index[12] = {0}; 15 int num[10] = {0}; 16 cin >> s; 17 for (int i = 0; i < 11; i++) 18 { 19 tel[i] = s[i] - '0'; 20 } 21 for (int i = 0; i < 11; i++) 22 { 23 switch (tel[i]) 24 { 25 case 0: 26 num[0]++; 27 break; 28 case 1: 29 num[1]++; 30 break; 31 case 2: 32 num[2]++; 33 break; 34 case 3: 35 num[3]++; 36 break; 37 case 4: 38 num[4]++; 39 break; 40 case 5: 41 num[5]++; 42 break; 43 case 6: 44 num[6]++; 45 break; 46 case 7: 47 num[7]++; 48 break; 49 case 8: 50 num[8]++; 51 break; 52 case 9: 53 num[9]++; 54 break; 55 default: 56 break; 57 } 58 }//找出电话号码中所包含的数字 59 int n = 0; 60 for (int i = 9; i >=0; i--) 61 { 62 if (num[i] != 0) 63 { 64 arr[n] = i; 65 n++; 66 } 67 }//将电话号码中的数字,按照降序排列在数组arr中 68 int m = 0; 69 for (int i = 0; i < 12; i++) 70 { 71 for (int j = 0; j < n; j++) 72 { 73 if (tel[i] == arr[j]) 74 { 75 index[m] = j; 76 m++; 77 } 78 } 79 }//将电话号码中数字对应arr的下标赋值给index中 80 int a = 0; 81 int b = 0; 82 cout << "int[] arr = new int[]{"; 83 for (int i = 0; i < n; i++) 84 { 85 cout << arr[i]; 86 a++; 87 if (a == n) 88 { 89 cout << "};"; 90 } 91 else 92 { 93 cout << ","; 94 } 95 } 96 cout << endl; 97 cout << "int[] index = new int[]{"; 98 for (int i = 0; i < 11; i++) 99 { 100 cout << index[i]; 101 b++; 102 if (b == 11) 103 { 104 cout << "};"; 105 } 106 else 107 { 108 cout << ","; 109 } 110 } 111 cout << endl; 112 } 113 int main() 114 { 115 test01(); 116 return 0; 117 }
标签:case,++,break,int,num,2023.7,include From: https://www.cnblogs.com/muzhaodi/p/17535952.html