华为机试题六

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*查找非素数
#include<iostream>
using namespace std;
int fun(int m)
{
    int i;
    for(i=2;i<m;i++)
    {
        if(m%i==0)
            return 0;
    }
    return 1;
}
void main()
{
    int m,i;
    cin>>m;
    for (i=0;i<=m;i++)
    {
        if(fun(i)==0)
            cout<<i<<" ";
    }
}
*/
/*  查找二进制0
#include <iostream>
using namespace std;
void main()
{
    int a[30],m;
    int i=0;
    int j,k;
    int count = 0;
    cin>>m;
    while (m)
    {
        a[i]=m&0001;
        i++;
        m=m/2;
    }
    j=i;
    for (i=j-1;i>=0;i--)
    {
        if(a[i]==1)
        {
            k=i;
            break;
        }
    }
    for(i=0;i<k;i++)
    {
        if(a[i] == 0)
            count++;
    }
    cout<<count;
}
*/
/*  最大公约数
#include <iostream>
using namespace std;
void main()
{
    int m,n;
    cin>>m>>n;
    int r=m%n;
    while (r)
    {
      m=n;
      n=r;
      r=m%n;
    }
    cout<<n;
}
*/
/*  字符串中不同字符的个数
#include <iostream> 
using namespace std; 
void main() 
    char a,s[100]; 
    int count = 0; 
    int i,j; 
    gets(s); 
    for (i=0;i<strlen(s)-1;i++) 
    
        for(j=0;j<strlen(s)-1-i;j++) 
            if(s[j+1]<s[j]) 
            
                a = s[j]; 
                s[j] = s[j-1]; 
                s[j-1] = a; 
            
    
    for (i=0;i<strlen(s);i++) 
    
        if(s[i+1] != s[i]) 
        
            count++; 
        
    
    cout<<count; 
}
*/
 
/*  计算整数的位数
#include <iostream>
#include <string.h>
using namespace std;
#define N 6
void main()
{
    char a[10][N];
    int i,j;
    i=0;
    while (scanf("%s",a[i])!= EOF)
    {
        cout<<strlen(a[i])<<" ";
        for (j=strlen(a[i])-1;j>=0;j--)
        {
            cout<<a[i][j];
        }
        cout<<endl;
    }
}
*/
/*   图片整理
#include <iostream>
#include <string.h>
using namespace std;
void main()
{
    char a[1024],s;
    int i,j,k;
    scanf("%s",a);
    for (i=0;i<strlen(a)-1;i++)
    {
       for (j=0;j<strlen(a)-1-i;j++)
       {
           if (a[j+1]<a[j])
           {
               s = a[j];
               a[j] = a[j+1];
               a[j+1] = s;
           }
       }
    }
    puts(a);
}
*/
/*   最高分查询
#include <iostream>
using namespace std;
void main()
{
    int N,M;
    int *a;
    int i,j,k,m,temp;
    char s1;
    cin>>N>>M;
    a = (int *)malloc(N*sizeof(int));
    for(i=0;i<N;i++)   cin>>a[i];
    for(j=0;j<M;j++)
    {
        cin>>s1;
        cin>>k>>m;
        if (s1 == 'Q')
        {
            temp=0;
            for (i=k;i<=m;i++)
            {
               if (a[i]>temp)
               {
                   temp=a[i];
               }
            }
            cout<<temp<<endl;
        }
        else if (s1 == 'U')
        {
            a[k] = m;
        }
    }
    free(a);
}
*/
/*   字符变换
#include <iostream>
#include <string.h>
using namespace std;
void main()
{
    char a[50];
    int i;
    gets(a);
    for (i=0;i<strlen(a);i++)
    {
        if (a[i]>='A' && a[i]<='U')
        {
            a[i] = a[i] + 37;
        }
        else if (a[i] =='V' )
        {
             a[i] = 'a';
        }
        else if (a[i] =='W' )
        {
            a[i] = 'b';
        }
        else if (a[i] =='X' )
        {
            a[i] = 'c';
        }
        else if (a[i] =='Y' )
        {
            a[i] = 'd';
        }
        else if (a[i] =='Z' )
        {
            a[i] = 'e';
        }
    }
    puts(a);
}
*/
/*    尼科彻斯定理
#include <iostream>
using namespace std;
void main()
{
    int m,i;
    cin>>m;
    int m3 = m*m*m;
    int m2=m*m;
    int ans;
    if (m%2 == 0)
    {
        ans = 0;
        for (i=0;i<m/2;i++)
        {
            ans = ans + m2-1-2*i;
            ans = ans + m2+1+2*i;
        }
        if (ans == m3)
        {
            for (i=m/2-1;i>=0;i--)
            {
                cout<<m2-1-2*i<<"+";
            }
            for (i=0;i<m/2-1;i++)
            {
                cout<<m2+1+2*i<<"+";
                 
            }
            cout<<m2+1+2*(m/2-1);
        }
        else
            cout<<-1;
 
    }
    else
    {
       ans = m2;
       for (i=1;i<=m/2;i++)
       {
           ans = m2-2*i+ans;
           ans = m2+2*i+ans;
       }
       if (ans == m3)
       {
           for (i=m/2;i>0;i--)
           {
             cout<<m2-2*i<<"+";
           }
           for (i=0;i<m/2;i++)
           {
               cout<<m2+2*i<<"+";
              
           }
           cout<<m2+2*(m/2);
       }
        else
            cout<<-1;
    
}
*/
#include <iostream>
using namespace std;
void main()
{
    int m,i;
    cin>>m;
    int m3 = m*m*m;
    int k= m*(m-1)+1;
    int sum = 0;
    for (i=0;i<m;i++)
    {
        sum = sum+k+2*i;
    }
    if (sum == m3)
    {
        for (i=0;i<m-1;i++)
        {
            cout<<k+2*i<<"+";
        }
        cout<<k+2*i;
    }
    else
        cout<<-1;
     
}
/*    找零钱
#include <iostream>
using namespace std;
int a[7] = {1,2,5,10,20,50,100};
int fun(int n,int i)
{
    if(n==1 || i==0)
        return 1;
    if(n<0 || i<0)
        return 0;
    return fun(n-a[i],i)+fun(n,i-1);
}
void main()
{
    int n;
    int i;
    cin>>n;
    while (n != 0)
    {
        for (i=6;i>=0;i--)
        {
            if (n>=a[i])
            {
                break;
            }
        }
        cout<<fun(n,i)<<endl;
        cin>>n;
    }
}
*/

 部分初级题集合

posted on   Zincky  阅读(138)  评论(0编辑  收藏  举报

努力加载评论中...

导航

点击右上角即可分享
微信分享提示